+ tmpBuf[pos++] = map[ num % base ]; // Last digit of {number}
+
+ // length of number, minus the sign character
+ PadLength -= pos - sign_is_neg - (SignChar != '\0');
+ if( !bPadRight )
+ {
+ while(PadLength-- > 0)
+ putch_cb(putch_h, PadChar), ret ++;
+ }
+
+ if(sign_is_neg)
+ putch_cb(putch_h, '-'), ret++; // Negative sign character
+ else if(SignChar)
+ putch_cb(putch_h, SignChar), ret++; // positive sign character
+ else {
+ }
+
+ while( Precision-- > 0 )
+ putch_cb(putch_h, '0'), ret++;
+ while(pos--)
+ putch_cb(putch_h, tmpBuf[pos]), ret++; // Reverse the order of characters
+
+ if( bPadRight )
+ {
+ while(PadLength-- > 0)
+ putch_cb(putch_h, PadChar), ret ++;
+ }
+
+ return ret;
+}
+
+int expand_double(double num, uint64_t *Significand, int16_t *Exponent, int *SignIsNeg)
+{
+ // IEEE 754 binary64
+ #if 0
+ {
+ uint64_t test_exp = 0xC000000000000000;
+ double test_value = -2.0f;
+ assert( *((uint64_t*)&test_value) == test_exp );
+ }
+ #endif
+
+ const uint64_t *bit_rep = (void*)#
+
+ *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);