+
+ uint64_t integer = (uint64_t)(abs(x));
+ uint64_t fraction = 0L;
+ int64_t exponent = 0L;
+
+ int64_t exp_bias = (1LL << (EMAX-1) - 1);
+
+ Real f = Real(abs(x)) - Real(integer);
+ //f = 0.375; integer = 12;
+ // Get active fractional bits
+ for (size_t i = 0; i <= PREC && f > 0; ++i)
+ {
+ //Debug("%d, %lf", i, Float(f));
+ if (f >= 1)
+ {
+ fraction |= (1LL << (PREC-i));
+ f -= 1.0L;
+ //Debug("1");
+ }
+ //else
+ //Debug("0");
+ f *= 2.0L;
+ }
+
+ //Debug("Integer: %lx", integer);
+ int offset = 0; while ((integer >> (offset++) != 0) && (offset < 8*sizeof(integer))); --offset;
+ //Debug("Offset: %d", offset);
+ //Debug("Fraction: %lx", fraction);
+ exponent += offset-1;
+ uint64_t mantissa = fraction;
+ if (offset > 0)
+ {
+ //Debug("Mantissa %.2lx", mantissa);
+ mantissa = (integer << PREC) | fraction;
+ //Debug("Mantissa %.2lx", mantissa);
+ mantissa = mantissa >> (offset-1);
+ //Debug("Mantissa %.2lx", mantissa);
+ //mantissa = mantissa >> (offset-1);
+ }
+ else
+ {
+ mantissa = mantissa << 1;
+ }
+ //Debug("Mantissa %.2lx", mantissa);
+
+ //Debug("Exponent: %lx %li", exponent, exponent);
+ if (exponent < -exp_bias)
+ {
+ mantissa = mantissa >> (-exp_bias - exponent);
+ exponent = -exp_bias;
+ }
+ //Debug("Exponent: %lx %li", exponent, exponent);
+ exponent = exp_bias + exponent;
+ //Real(exponent + 1) - Real(1LL << (EMAX-1))
+ //Debug("Exponent: %lx %li", exponent, exponent);
+ Real M = (exponent != 0);
+ for (size_t i = 1; i < PREC; ++i)
+ {
+ if ((mantissa >> PREC-i) & 1)
+ M += Real(1) / powl(BASE, i);
+ }
+ //Debug("M is %lf", Float(M));
+
+ // Now copy the bits in (fun times)
+ 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 |= ((x < 0) << (i%8));