64-bit division

Can anyone send me the c code to split 2 64 bit numbers. My compiler only supports 32/32 split.

Thanx and Regards

mani

-3


source to share


5 answers


Are you sure your compiler doesn't support 64-bit division? If your C compiler supports C99, this should work:



#include <stdint.h>
#include <stdio.h>
int main(void)
{
    int64_t numerator = 123;
    int64_t denominator = 10;
    int64_t quotient = numerator / denominator
    printf("%" PRId64 " / %" PRId64 " = %" PRId64 "\n",
           numerator, denominator, quotient);
    return 0;
}

      

+11


source


Code is available on linux, see div64.c . Can you copy this?



+6


source


A more general idea is to use a multi-point library like GMP .

GMP is a free library for arbitrary precision arithmetic that works with signed integers, rational numbers, and floating point numbers. There is practically no limit to the accuracy, other than what is implied by the available memory in the GMP machine. GMP has a rich feature set, and the features have a regular interface.

Floating point division is handled with void mpf_div (mpf_t rop, mpf_t op1, mpf_t op2)

+4


source


More than likely, the partitioning limitation is due to the fact that you are compiling 32-bit instead of 64-bit.

I don't remember seeing an integer division instruction that handles 64-bit for x86. It will multiply 2 32-bit integers and split the results into two registers, but not division as far as I remember ..

+1


source


Elsewhere in the Linux kernel: udivdi3.c

This should be an exact delivery of the functionality used by GCC whenever it encounters 64-bit division.

+1


source







All Articles