在8位平台上,我由4个8位整数组成一个无符号32位整数,如下所示:
uint8_t buf[4];
uint32_t large = 0;
large |= ((uint32_t)buf[0]) << 24;
large |= ((uint32_t)buf[1]) << 16;
large |= buf[2] << 8;
large |= buf[3] << 0;
如果没有强制转换,编译器抱怨是可以理解的:
bmp.c:100:23: warning: left shift count >= width of type [-Wshift-count-overflow]
100 | large |= (buf[1]) << 16;
| ^~
这些施法费用高吗(我猜是的),这能更有效地完成吗?
以下是我认为的avr-gcc (GCC) 13.2.0
个代码的相关拆解:
000060ee <.L29>:
large |= ((uint32_t)buf[1]) << 16;
60ee: 91 2c mov r9, r1
60f0: a1 2c mov r10, r1
60f2: b1 2c mov r11, r1
000060f4 <.Loc.91>:
large |= buf[3] << 0;
60f4: a9 2a or r10, r25
000060f6 <.Loc.92>:
large |= buf[2] << 8;
60f6: 50 e0 ldi r21, 0x00 ; 0
000060f8 <.Loc.93>:
60f8: 54 2f mov r21, r20
60fa: 44 27 eor r20, r20
60fc: 05 2e mov r0, r21
60fe: 00 0c add r0, r0
6100: 66 0b sbc r22, r22
6102: 77 0b sbc r23, r23
00006104 <.Loc.94>:
large |= buf[3] << 0;
6104: 84 2a or r8, r20
6106: 95 2a or r9, r21
6108: a6 2a or r10, r22
610a: b7 2a or r11, r23
610c: b8 2a or r11, r24
610e: 80 92 04 01 sts 0x0104, r8 ; 0x800104 <large>
6112: 90 92 05 01 sts 0x0105, r9 ; 0x800105 <large+0x1>
6116: a0 92 06 01 sts 0x0106, r10 ; 0x800106 <large+0x2>
611a: b0 92 07 01 sts 0x0107, r11 ; 0x800107 <large+0x3>