+ *SignIsNeg = *bit_rep >> 63;
+ *Exponent = ((*bit_rep >> 52) & 0x7FF) - 1023;
+ *Significand = (*bit_rep & ((1ULL << 52)-1)) << (64-52);
+
+// printf("%llx %i %i %llx\n", *bit_rep, (int)*SignIsNeg, (int)*Exponent, *Significand);
+
+ // Subnormals
+ if( *Exponent == -1023 && *Significand != 0 )
+ return 1;
+ // Infinity
+ if( *Exponent == 0x800 && *Significand == 0)
+ return 2;
+ // NaNs
+ if( *Exponent == 0x800 && *Significand != 0)
+ return 3;
+
+ return 0;
+}
+
+/**
+ * Internal function
+ * \return Remainder
+ */
+double _longdiv(double num, double den, int *quot)
+{
+ assert(num >= 0);
+ assert(den > 0);
+// printf("%llu / %llu\n", (long long int)num, (long long int)den);
+
+ *quot = 0;
+ assert(num < den*10);
+ while(num >= den)
+ {
+ num -= den;
+ (*quot) ++;
+ assert( *quot < 10 );
+ }
+// printf(" %i\n", *quot);
+ return num;
+}
+
+size_t _printf_ftoa_hex(printf_putch_t putch_cb, void *putch_h, long double num, int Precision, int bForcePoint, int bForceSign, int bCapitals)
+{
+ uint64_t significand;
+ int16_t exponent;
+ int signisneg;
+ int rv;
+ size_t ret = 0;
+
+ #define _putch(_ch) do{\
+ if(bCapitals)\
+ putch_cb(putch_h, toupper(_ch));\
+ else\
+ putch_cb(putch_h, _ch);\
+ ret ++;\
+ }while(0)
+
+ if( Precision == -1 )
+ Precision = (64-4)/4;
+
+ rv = expand_double(num, &significand, &exponent, &signisneg);
+ switch(rv)
+ {
+ // 0: No errors, nothing special
+ case 0:
+ break;
+ // 1: Subnormals
+ case 1:
+ // TODO: Subnormal = 0?
+ break;
+ // 2: Infinity
+ case 2:
+ _putch('i');
+ _putch('n');
+ _putch('f');
+ return 3;
+ case 3:
+ _putch('N');
+ _putch('a');
+ _putch('N');
+ return 3;
+ }
+
+ // int exp_ofs = exponent % 4;
+ // int exp_16 = exponent - exp_ofs;
+ //uint8_t whole = (1 << exp_ofs) | ((significand >> (64-3)) >> exp_ofs);
+ //significand <<= 3 - exp_ofs;
+ uint8_t whole = (rv != 1) ? 1 : 0;
+
+ if( signisneg )
+ _putch('-');
+ else if( bForceSign )
+ _putch('+');
+ else {
+ }
+ _putch('0');
+ _putch('x');
+ _putch(cDIGITS[whole]);
+ if( significand || bForcePoint )
+ _putch('.');
+ // Fractional
+ while( significand && Precision -- )
+ {
+ uint8_t val = significand >> (64-4);
+ _putch(cDIGITS[val]);
+ significand <<= 4;
+ }
+ _putch('p');
+ //ret += _printf_itoa(putch_cb, putch_h, exp_16, 16, bCapitals, TRUE, '+', 0, 0, '\0', 0);
+ ret += _printf_itoa(putch_cb, putch_h, exponent, 10, bCapitals, TRUE, '+', 0, 0, '\0', 0);
+
+ #undef _putch
+ return ret;
+}
+
+size_t _printf_ftoa(printf_putch_t putch_cb, void *putch_h, long double num, size_t Base, enum eFPN Notation, int Precision, int bForcePoint, int bForceSign, int bCapitals)
+{
+ uint64_t significand;
+ int16_t exponent;
+ int signisneg;
+ int rv;
+ size_t ret = 0;
+
+ #define _putch(_ch) do{\
+ if(bCapitals)\
+ putch_cb(putch_h, toupper(_ch));\
+ else\
+ putch_cb(putch_h, _ch);\
+ ret ++;\
+ }while(0)
+
+ if( Base <= 1 || Base > 16 )
+ return 0;
+
+ rv = expand_double(num, &significand, &exponent, &signisneg);
+ switch(rv)
+ {
+ // 0: No errors, nothing special
+ case 0:
+ break;
+ // 1: Subnormals
+ case 1:
+ // TODO: Subnormal = 0?
+ break;
+ // 2: Infinity
+ case 2:
+ _putch('i');
+ _putch('n');
+ _putch('f');
+ return 3;
+ case 3:
+ _putch('N');
+ _putch('a');
+ _putch('N');
+ return 3;
+ }
+
+ // - Used as 0/1 bools in arithmatic later on
+ bForcePoint = !!bForcePoint;
+ bForceSign = !!bForceSign;
+
+ // Apply default to precision
+ if( Precision == -1 )
+ Precision = 6;
+
+ if( num < 0 )
+ num = -num;
+
+ // Select optimum type
+ if( Notation == FPN_SHORTEST )
+ {
+ //TODO:
+ //int first_set_sig = BSL(significand);
+ // TODO: if( num > pos(Base, 2+Precision+2+log_base(exponent) )
+ Notation = FPN_SCI;
+ }
+
+ double precision_max = 1;
+ while(Precision--)
+ precision_max /= Base;
+
+ // Determine scientific's exponent and starting denominator
+ double den = 1;
+ int sci_exponent = 0;
+ if( Notation == FPN_SCI )
+ {
+ if( num < 1 )
+ {
+ while(num < 1)
+ {
+ num *= Base;
+ sci_exponent ++;
+ }
+ }
+ else if( num >= Base )
+ {
+ while(num >= Base)
+ {
+ num /= Base;
+ sci_exponent ++;
+ }
+ }
+ else
+ {
+ // no exponent
+ }
+ den = 1;
+ }
+ else
+ {
+ while( den < num )
+ den *= Base;
+ den /= Base;
+ }
+
+ // Leading sign
+ if( signisneg )
+ _putch('-');
+ else if( bForceSign )
+ _putch('+');
+ else {
+ }
+
+ int value;
+ // Whole section
+ do
+ {
+ num = _longdiv(num, den, &value);
+ _putch(cDIGITS[value]);
+ den /= Base;
+ } while( den >= 1 );
+
+ // Decimal point (if needed/forced)
+ if( den >= precision_max || bForcePoint )
+ _putch('.');
+ // Decimal section
+ while( den >= precision_max )
+ {
+ num = _longdiv(num, den, &value);
+ _putch(cDIGITS[value]);
+ den /= Base;
+ }
+
+ if( Notation == FPN_SCI )
+ {
+ if( Base == 16 )
+ _putch('p');
+ else
+ _putch('e');
+ ret += _printf_itoa(putch_cb, putch_h, sci_exponent, Base, FALSE, TRUE, '+', 0, 0, '\0', FALSE);
+ }
+
+ #undef _putch
+
+ return ret;