The other day I learned a cool trick in Verilog. When you need to do something repeatedly. You can use the shift register to count the amount of increment. Just moving 1 from LSB to MSB, and when it reaches MSB, you are done.
In C, it would be something like this:
for(j=0b1; !(j & (1<<16)); j=j<<1) { /*do a thing 16 times*/ }
I know this is limited use due to bit width, but it is not related to adding, so it is fast. So my question is: Is there this use? Should I use it in C or any other high-level language?
Perhaps in embedded systems where resources are limited.
thank
it does not contain any addition, so it is fast
, ? , , , , ?
?
.
, . , :
uint8_t mask; for(mask = 0x01; mask != 0x00; mask<<=1) { do_something (data & mask); }
for(i=0; i<8; i++) { do_something (data & (1<<i)); }
. , .
, . , , . , , , , , .
-, .
, , /, , . Verilog - , , - . , -: . :
, , . FF (-) FF. LSB MSB, (N) (N - , ). , N FF, log2 (N) FF. , /, N. "" : http://en.wikipedia.org/wiki/Adder_%28electronics%29 , , .
RTL. . "for" verilog , "" . , N . . Verilog, - ( CPU , ). -, , .
( , , C, Verilog, , Verilog.)
, , .
. , , , - , . , , . , " ", ; .
RISC -- , , , ( ) , , ( ) - ( ) -if-zero. , -if-zero.
, for (i = 0; i < N; i++) "count down to 0", - .
for (i = 0; i < N; i++)
- , ; . .
? , ? , MIPS , . , .
, , . , .
- . , , RISC- . .
When you keep your loop code idiomatic, the optimizer can simply expand the loop and make it faster anyway. If you make the loop mechanism "unusual", the optimizer will not be able to optimize it.
In general, if you want to always loop a certain number of times> 0 and minimize the overhead of the loop, then I think this will be the βbestβ:
unsigned i = 16; do { // do something here } while (--i); You might get the same result with: unsigned i = 0x8000; do { // do something here } while (i>>=1);
At this point you will have to look at the assembly.