// File: /workspace/github_c_makefile_star2_O2/FredTheDino[P]Fog/src/engine/util/printf.c
// Function: etoa(void (*)(char, void*, unsigned long, unsigned long), char*, unsigned long, unsigned long, double, unsigned int, unsigned int, unsigned int)
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
  // check for NaN and special values
  if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
    return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
  }

  // determine the sign
  const bool negative = value < 0;
  if (negative) {
    value = -value;
  }

  // default precision
  if (!(flags & FLAGS_PRECISION)) {
    prec = PRINTF_DEFAULT_FLOAT_PRECISION;
  }

  // determine the decimal exponent
  // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
  union {
    uint64_t U;
    double   F;
  } conv;

  conv.F = value;
  int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;           // effectively log2
  conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);  // drop the exponent so conv.F is now in [1,2)
  // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
  int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
  // now we want to compute 10^expval but we want to be sure it won't overflow
  exp2 = (int)(expval * 3.321928094887362 + 0.5);
  const double z  = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
  const double z2 = z * z;
  conv.U = (uint64_t)(exp2 + 1023) << 52U;
  // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
  conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
  // correct for rounding errors
  if (value < conv.F) {
    expval--;
    conv.F /= 10;
  }

  // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
  unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;

  // in "%g" mode, "prec" is the number of *significant figures* not decimals
  if (flags & FLAGS_ADAPT_EXP) {
    // do we want to fall-back to "%f" mode?
    if ((value >= 1e-4) && (value < 1e6)) {
      if ((int)prec > expval) {
        prec = (unsigned)((int)prec - expval - 1);
      }
      else {
        prec = 0;
      }
      flags |= FLAGS_PRECISION;   // make sure _ftoa respects precision
      // no characters in exponent
      minwidth = 0U;
      expval   = 0;
    }
    else {
      // we use one sigfig for the whole part
      if ((prec > 0) && (flags & FLAGS_PRECISION)) {
        --prec;
      }
    }
  }

  // will everything fit?
  unsigned int fwidth = width;
  if (width > minwidth) {
    // we didn't fall-back so subtract the characters required for the exponent
    fwidth -= minwidth;
  } else {
    // not enough characters, so go back to default sizing
    fwidth = 0U;
  }
  if ((flags & FLAGS_LEFT) && minwidth) {
    // if we're padding on the right, DON'T pad the floating part
    fwidth = 0U;
  }

  // rescale the float value
  if (expval) {
    value /= conv.F;
  }

  // output the floating part
  const size_t start_idx = idx;
  idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);

  // output the exponent part
  if (minwidth) {
    // output the exponential symbol
    out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
    // output the exponent value
    idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth-1, FLAGS_ZEROPAD | FLAGS_PLUS);
    // might need to right-pad spaces
    if (flags & FLAGS_LEFT) {
      while (idx - start_idx < width) out(' ', buffer, idx++, maxlen);
    }
  }
  return idx;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %r15d
movl %r8d, %eax
movq %rdx, -0x30(%rbp)
movsd 0x2fa4(%rip), %xmm1     # 0x2ba78
ucomisd %xmm0, %xmm1
ja 0x28c81
ucomisd %xmm0, %xmm0
jp 0x28c81
ucomisd 0x2f90(%rip), %xmm0     # 0x2ba80
ja 0x28c81
movl 0x10(%rbp), %r13d
movapd 0x284e(%rip), %xmm1     # 0x2b350
xorpd %xmm0, %xmm1
maxsd %xmm0, %xmm1
btl $0xa, %r13d
pushq $0x6
popq %r9
movl %eax, %r8d
cmovael %r9d, %r8d
movq %xmm1, %rdx
movabsq $0xfffffffffffff, %r11  # imm = 0xFFFFFFFFFFFFF
andq %rdx, %r11
shrq $0x34, %rdx
andl $0x7ff, %edx            # imm = 0x7FF
addl $0xfffffc01, %edx       # imm = 0xFFFFFC01
movabsq $0x3ff0000000000000, %rbx # imm = 0x3FF0000000000000
cvtsi2sd %edx, %xmm2
mulsd 0x2f46(%rip), %xmm2     # 0x2ba98
orq %r11, %rbx
addsd 0x2f43(%rip), %xmm2     # 0x2baa0
movq %rbx, %xmm3
addsd 0x2f3e(%rip), %xmm3     # 0x2baa8
mulsd 0x2f3e(%rip), %xmm3     # 0x2bab0
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %ebx
cvttpd2dq %xmm3, %xmm2
cvtdq2pd %xmm2, %xmm3
movsd 0x2f2e(%rip), %xmm2     # 0x2bab8
mulsd %xmm3, %xmm2
addsd 0x2e9a(%rip), %xmm2     # 0x2ba30
cvttsd2si %xmm2, %edx
cvttpd2dq %xmm2, %xmm2
cvtdq2pd %xmm2, %xmm2
mulsd 0x2f16(%rip), %xmm2     # 0x2bac0
mulsd 0x2f16(%rip), %xmm3     # 0x2bac8
addsd %xmm2, %xmm3
movapd %xmm3, %xmm2
mulsd %xmm3, %xmm2
addl $0x3ff, %edx            # imm = 0x3FF
movsd 0x2f04(%rip), %xmm5     # 0x2bad0
subsd %xmm3, %xmm5
movapd %xmm2, %xmm6
divsd 0x2efc(%rip), %xmm6     # 0x2bad8
addsd %xmm3, %xmm3
movsd 0x2ef8(%rip), %xmm4     # 0x2bae0
addsd %xmm4, %xmm6
movapd %xmm2, %xmm7
divsd %xmm6, %xmm7
addsd 0x2eec(%rip), %xmm7     # 0x2bae8
divsd %xmm7, %xmm2
addsd %xmm5, %xmm2
divsd %xmm2, %xmm3
addsd 0x2448(%rip), %xmm3     # 0x2b058
shlq $0x34, %rdx
movq %rdx, %xmm2
mulsd %xmm3, %xmm2
xorl %edx, %edx
ucomisd %xmm1, %xmm2
seta %r11b
jbe 0x28c2d
divsd %xmm4, %xmm2
movb %r11b, %dl
subl %edx, %ebx
leal 0x63(%rbx), %edx
cmpl $0xc7, %edx
pushq $0x5
popq %r14
sbbl $0x0, %r14d
btl $0xb, %r13d
jae 0x28cb6
ucomisd 0x2e9e(%rip), %xmm1     # 0x2baf0
jb 0x28c9e
movsd 0x2e9c(%rip), %xmm3     # 0x2baf8
ucomisd %xmm1, %xmm3
jbe 0x28c9e
movl %ebx, %eax
notl %eax
addl %r8d, %eax
xorl %r14d, %r14d
cmpl %ebx, %r8d
cmovlel %r14d, %eax
orl $0x400, %r13d           # imm = 0x400
movl %eax, %r8d
xorl %ebx, %ebx
jmp 0x28cb6
movq -0x30(%rbp), %rdx
movl %eax, %r8d
movl %r15d, %r9d
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28781
testl %r8d, %r8d
je 0x28cb3
decl %eax
btl $0xa, %r13d
cmovael %r9d, %eax
movl %eax, %r8d
jmp 0x28cb6
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r15d, %eax
subl %r14d, %eax
cmovbl %r9d, %eax
testl %r14d, %r14d
cmovel %eax, %r9d
testb $0x2, %r13b
cmovel %eax, %r9d
testl %ebx, %ebx
movl %r15d, -0x38(%rbp)
movq %rbx, -0x40(%rbp)
je 0x28ce2
divsd %xmm2, %xmm1
movapd 0x2666(%rip), %xmm2     # 0x2b350
xorpd %xmm1, %xmm2
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
movapd %xmm0, %xmm3
andnpd %xmm1, %xmm3
andpd %xmm2, %xmm0
orpd %xmm3, %xmm0
movl %r13d, %eax
andl $0xfffff7ff, %eax       # imm = 0xFFFFF7FF
movl %eax, (%rsp)
movq -0x30(%rbp), %rdx
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r12
callq 0x28781
movq %rax, %rdx
testl %r14d, %r14d
je 0x28dc8
xorl %edi, %edi
movl %r13d, -0x34(%rbp)
testb $0x20, %r13b
sete %dil
movq %rdx, %r13
incq %r13
shll $0x5, %edi
orl $0x45, %edi
movq %rbx, %rsi
movq %r15, %rcx
callq *%r12
movq -0x40(%rbp), %r9
movl %r9d, %r8d
negl %r8d
cmovsl %r9d, %r8d
decl %r14d
movl %r14d, 0x10(%rsp)
movl $0x5, 0x18(%rsp)
andl $0x0, 0x8(%rsp)
movq $0xa, (%rsp)
shrl $0x1f, %r9d
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x286b6
movq %rax, %rdx
testb $0x2, -0x34(%rbp)
je 0x28dc8
movq %rbx, %r13
movl -0x38(%rbp), %r14d
negq -0x30(%rbp)
movq -0x30(%rbp), %rax
addq %rdx, %rax
cmpq %r14, %rax
jae 0x28dc8
leaq 0x1(%rdx), %rbx
pushq $0x20
popq %rdi
movq %r13, %rsi
movq %r15, %rcx
callq *%r12
movq %rbx, %rdx
jmp 0x28da7
movq %rdx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq