Niall’s virtual diary archives – Monday 8th April 2013

by . Last updated .

Monday 8th April 2013: 8.06pm. Link shared:

Just spent several hours trying to figure out how to write a floating point double truncation routine which can't use a C library, not even anything from <math.h>. I can't use compiler intrinsics either (don't ask!). I also can't cast to a 64-bit integer, as the double is highly likely to overflow a 64-bit integer and would therefore cause a floating point exception. Why do I need a truncation routine? Because printing a floating point number requires one, and remember, I have no C library.

First off I tried parsing the IEEE 754 format and using fixed point 64-bit integer to reconstitute the number as a whole integer. That proved a bit tricky.

Then I thought while staring at, the mantissa is just another integer really, and like any integer you can overflow it. So I came up with this:

// Niall's C99 trunc() implementation which uses no C library
static double mytrunc(double _v)
   // I really, really don't want the optimiser to elide this code
   volatile double v=_v;
   // There are 52 bits of mantissa stored as 2^N, so make the FP unit truncate the bottom bits for us
   // The FP unit will round to nearest, so we need to inhibit that.
   if(v>=0 && v>_v) v-=1;
   else if(v<0 && v<_v) v+=1;
   return v;

I think that ought to work no matter the input. It certainly seems to work. Anyway, I'm feeling quite proud of myself for such an elegant solution. It's probably old hat to many people, but for me that took a fair bit of thinking.

Go back to the archive index Go back to the latest entries

Contact the webmaster: Niall Douglas @ webmaster2<at symbol> (Last updated: 2013-04-08 20:06:24 +0000 UTC)