c - On embedded platforms, is it more efficient to use unsigned int instead of (implicity signed) int? -
i've got habit of using unsigned integers possible in code, because processor can divides powers of 2 on unsigned types, can't signed types. speed critical project. processor operates @ 40 mips.
my processor has 18 cycle divide, takes longer single cycle barrel shifter. worth using unsigned integers here speed things or bring other disadvantages? i'm using dspic33fj128gp802 - member of dspic33f series microchip. has single cycle multiply both signed , unsigned ints. has sign , 0 extend instructions.
for example, produces code when mixing signed , unsigned integers.
026e4 97e80f mov.b [w15-24],w0 026e6 fb0000 se w0,w0 026e8 97e11f mov.b [w15-31],w2 026ea fb8102 ze w2,w2 026ec b98002 mul.ss w0,w2,w0 026ee 400600 add.w w0,w0,w12 026f0 fb8003 ze w3,w0 026f2 100770 subr.w w0,#16,w14
i'm using c (gcc dspic.)
i think need know lot more peculiarities of processor answer question. why can't divides powers of 2 on signed integers? far remember operation same both. i.e.
10/2 = 00001010 goes 00000101
-10/2 = 11110110 goes 11111011
maybe should write simple code doing unsigned divide , signed divide , compare compiled output.
also benchmarking idea. doesn't need precise. have array of few thousand numbers, start timer , start dividing them few million times , time how long takes. maybe few billion times if processor fast. e.g.
int s_numbers[] = { etc. etc. }; int s_array_size = sizeof(s_numbers); unsigned int u_numbers[] = { etc. etc.}; unsigned int u_array_size = sizeof(u_numbers); int i; int s_result; unsigned int u_result; /* start timer. */ for(i = 0; < 100000000; i++) { i_result = s_numbers[i % s_array_size] / s_numbers[(i + 1) % s_array_size]; } /* stop timer , print difference. */ /* repeat unsigned integers. */
written in hurry show principle, please forgive errors.
it won't give precise benchmarking should give general idea of faster.
Comments
Post a Comment