+/**
+ * Modifies fraction to contain the fractional part of x
+ * Returns position of the first '1' bit
+ */
+size_t Fraction(Real x, uint64_t & fraction)
+{
+ x = abs(x) - (uint64_t)(abs(x));
+ fraction = 0LL;
+ size_t offset = 0;
+ for (size_t i = 0; i < 8*sizeof(fraction); ++i)
+ {
+ x *= 2;
+ if (x >= 1.0)
+ {
+ offset = (offset == 0) ? i : offset;
+ fraction |= (1LL << (8*sizeof(fraction)-1-i));
+ x -= 1.0;
+ }
+ if (x <= 0.0)
+ break;
+ }
+ return fraction;
+}
+
+/**
+ * Copy mantissa and exponent into data
+ */
+template <size_t EMAX, size_t PREC, size_t BASE = 2> void BitsFromRepr(bool sign, uint64_t mantissa, uint64_t exponent, void * data)
+{
+ uint8_t * c = (uint8_t*)(data);
+ size_t i = 0;
+ for (i = 0; i < PREC; ++i)
+ {
+ *c |= ((mantissa >> i) & 1) << (i%8);
+ if ((i+1) % 8 == 0)
+ {
+ ++c;
+ }
+ }
+ for (; i < PREC+EMAX; ++i)
+ {
+ *c |= ((exponent >> (i-PREC)) & 1) << (i%8);
+ if ((i+1) % 8 == 0)
+ {
+ ++c;
+ }
+ }
+ *c |= (sign << (i%8));
+}
+
+
+