
Dear Simon Glass,
In message CAPnjgZ1dQXMVeFk47=Gg5y+TCHG7WTdGk0b1WjSqOBhu2d0xnQ@mail.gmail.com you wrote:
In case it is interesting (which may be unlikely) here is the code generated by my compiler (common code stripped) for your example, and
Which architecture / which compiler is this?
one I added. It seems that % makes it worry about sign.
Indeed! We should use unsigned numbers here. MUCH better now:
unsigned int ufoo(unsigned int i) { return i & (CONFIG_SYS_TMP_CON_BUF_SZ-1); }
For ARM, BUF_SZ = 1024
mov r0, r0, asl #22 mov r0, r0, lsr #22
For PPC, BUF_SZ = 1024
rlwinm 3,3,0,22,31
The following two entries make no sense, just for completeness:
For ARM, BUF_SZ = 1021 (Note: 1021 is a prime number)
and r0, r0, #1020
For PPC, BUF_SZ = 1021
rlwinm 3,3,0,22,29
unsigned int ubar(unsigned int i) { return i % CONFIG_SYS_TMP_CON_BUF_SZ; }
For ARM, BUF_SZ = 1024
mov r0, r0, asl #22 mov r0, r0, lsr #22
For PPC, BUF_SZ = 1024
rlwinm 3,3,0,22,31
For ARM, BUF_SZ = 1021
ldr r2, .L11 mov ip, r0 umull r1, r3, r2, r0 rsb r1, r3, r0 add r3, r3, r1, lsr #1 mov r3, r3, lsr #9 mov r0, r3, asl #10 sub r0, r0, r3, asl #2 add r0, r0, r3 rsb r0, r0, ip
For PPC, BUF_SZ = 1021
lis 0,0xc0 ori 0,0,36973 mulhwu 0,3,0 subf 9,0,3 srwi 9,9,1 add 0,0,9 srwi 0,0,9 mulli 0,0,1021 subf 3,0,3
So indeed we should use unsigned arithmetics, and try to use power-of-two sizes where possible.
Best regards,
Wolfgang Denk