Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

long long division does not make use of the HW divider

My NIOSII has configured hw divider. When I look into the OBJDUMP of divide operation of 2 integers (32 bits), I see that the operation is proprly done by a single "div" instruction.

However - when the dividend of the operation is long long - for example

int tmp1, tmp2, tmp3;

tmp3 = ((long long) tmp2)/tmp1 ;

Then the generated code is very long, using what seems as a built-in procedure __divdi3 , which has long code. What seems strange is that the built-in procedure don't use the resource of hw dividers at all : the instruction div does not appear in the code.

I want to avoid the long code, in order to reduce instruction memory resource, and speed the application. Are you familiar with this issue?

Thanks, Dani

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The hardware divide only does 32x32 divide.

    So you need something else to perform the 64 bit divide.

    If you really do need the division operation then there is little choice.

    However if you are only dividing by constants, you can arrange to use a 'multiply by reciprocal' sequence instead.

    If the divisor is relatively small (say less than 16 bits) the you might be able to do a series of divisions (like doing long division by hand) where each is less that 32 bits.

    Alternatively, if the performance isn't an issue, do a 64 bit binary long division yourself.

    Provided __divdid3() is compiled with -O3 it may actually be reasonly fast - I haven't looked at the one that Altera provide, but the code might be generating several bits per iteration.