Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of bitshifting by 0

I am reviewing the open source AMD GPU drivers for Linux. I noticed something I haven't seen before, and I would like to know the purpose. On line 1441 of the sid.h file, there are a series of defines that are integers being bitshifted left by 0. Wouldn't this just result in the original integer being operated on?

Here is an excerpt and a link to the head

    #define VGT_EVENT_INITIATOR                      0xA2A4
    #define SAMPLE_STREAMOUTSTATS1                   (1 << 0)
    #define SAMPLE_STREAMOUTSTATS2                   (2 << 0)
    #define SAMPLE_STREAMOUTSTATS3                   (3 << 0)

https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/amd/amdgpu/sid.h#L1441

Also, I am learning to access the performance counter registers of AMD GPUs in order to calculate the GPU load. Any tips on that would be appreciated as well.

like image 450
Michael Honey Avatar asked Dec 23 '22 11:12

Michael Honey


1 Answers

Things like that could be done just for the sake of consistency (not necessarily applicable to your specific case). For example, I can describe a set of single-bit flags as

#define FLAG_1 0x01
#define FLAG_2 0x02
#define FLAG_3 0x04
#define FLAG_4 0x08

or as

#define FLAG_1 (1u << 0)
#define FLAG_2 (1u << 1)
#define FLAG_3 (1u << 2)
#define FLAG_4 (1u << 3)

In the first line of the latter approach I did not have to shift by 0. But it just looks more consistent that way and emphasizes the fact that FLAG_1 has the same nature as the rest of the flags. And 0 acts as a placeholder for a different value, if I some day decide to change it.

You can actually see exactly that in the linked code with shift by 0 in the definitions of DYN_OR_EN and DYN_RR_EN macros.


The approach can be extended to multi-bit fields within a word, like in the following (contrived) example

// Bits 0-3 - lower counter, bits 4-7 - upper counter

#define LOWER_0  (0u << 0)
#define LOWER_1  (1u << 0)
#define LOWER_2  (2u << 0)
#define LOWER_3  (3u << 0)

#define UPPER_0  (0u << 4)
#define UPPER_1  (1u << 4)
#define UPPER_2  (2u << 4)
#define UPPER_3  (3u << 4)

unsigned packed_counters = LOWER_2 + UPPER_3; /* or `LOWER_2 | UPPER_3` */

Again, shifts by 0 bits are present purely for visual consistency. As well as shifts of 0 values.

You can actually see exactly that in the linked code with shift by 0 in the definitions of LC_XMIT_N_FTS and LC_XMIT_N_FTS_MASK macros.

like image 121
AnT Avatar answered Dec 28 '22 08:12

AnT