Forum Discussion
Altera_Forum
Honored Contributor
14 years agogcc generates incorrect code
Hi, It appears that there is a bug in this version of the nios2 compiler at higher optimisation levels. $ nios2-elf-gcc --version nios2-elf-gcc (Altera 11.1sp2 Build 259) 4.1.2 Copyri...
Altera_Forum
Honored Contributor
14 years agoThanks dabuk for your correction. You are absolutely correct.
BTW, while consuming BW, there was in fact a funny behaviour remaining in this code after it is corrected as debuk suggests (which in fact is the one I was originally pursuing). Do you see it? It resulted from a more subtle programming error (unfortunately still my fault STS). The following is a very bad idea in portable C code. uint32_t mask; if ( l < 32u ) { static const uint32_t one = 1u; const unsigned idx = 32u - l; uint32_t mask = one; mask <<= idx; mask -= one; } This one is correct: uint32_t mask; if ( l > 0 && l < 32u ) { static const uint32_t one = 1u; const unsigned idx = 32u - l; uint32_t mask = one; mask <<= idx; mask -= one; } The problem in the first version is that the C standard says that the result of a left shift is undefined if we specify a shift of more bits than are in the word, and 32 is in fact too many. The nios2 RTL for the SLL instruction is as follows. rC ← rA << (rB4..0) Notice that it uses only 5 bits from the register, shoot self in foot. Ouch