0

I am trying to implement IEEE 754 Floating point arithmetic in software without using the FPU. I have been reading a lot and found this solution to 64-bit multiplication on this site. I understand most of it but I am confused on why the person who answered the question is casting values 2 times for the same term, and it doesn't make sense to me. I feel like it has soothing to do with dealing with the High and low bytes of the solution but I don't see how double casting would accomplish that. Here is the code in question. Any help understanding why this was done would be helpful.

uint64_t    a_lo = (uint32_t)a;
uint64_t    a_hi = a >> 32;
uint64_t    b_lo = (uint32_t)b;
uint64_t    b_hi = b >> 32;

uint64_t    a_x_b_hi =  a_hi * b_hi;
uint64_t    a_x_b_mid = a_hi * b_lo;
uint64_t    b_x_a_mid = b_hi * a_lo;
uint64_t    a_x_b_lo =  a_lo * b_lo;

uint64_t    carry_bit = ((uint64_t)(uint32_t)a_x_b_mid +
                         (uint64_t)(uint32_t)b_x_a_mid +
                         (a_x_b_lo >> 32) ) >> 32;

uint64_t    multhi = a_x_b_hi +
                     (a_x_b_mid >> 32) + (b_x_a_mid >> 32) +
                     carry_bit;

return multhi;
CavemanAGz
  • 43
  • 1
  • 9
  • 1
    It sounds like it is masking the lower 32 bits of that 64-bit variable using the casts instead of writing `a_x_b_mid & 0xFFFFFFFF` – Bob__ Oct 30 '17 at 14:42
  • @underscore_d, thank you for the link, this is just the info I needed, it all makes a little more sense now. Bob__ This explanation also helps. I didn't know there were so many different ways of doing the same operation. – CavemanAGz Oct 30 '17 at 15:55

0 Answers0