// File: Alcaro[P]bridges/arlib/stringconv-float.cpp
// Function: string tostring_float<double>(double)
string tostring_float(T f)
{
	// to_chars is quite close to my desired output format
	static_assert(std::numeric_limits<T>::is_iec559);
	
	// can be replaced with if (isnan(f)) f = fabs(f); under libstdc++,
	// but it's implementation defined if the weird ones are nan or nan(123), or inf or infinity, so let's just hardcode them
	if (UNLIKELY(isnan(f))) return "nan";
	if (UNLIKELY(isinf(f))) return signbit(f) ? "-inf" : "inf";
	
	bool fixed = ((fabs(f) >= (T)0.0001 && fabs(f) < (T)10000000000000000.0) || f == 0);
	
	char buf[32]; // longest possible is 24, give it a little extra
	char* end;
	if (fixed)
	{
		auto ret = std::to_chars(buf, buf+sizeof(buf), f, std::chars_format::fixed);
		end = ret.ptr;
	}
	else
	{
		auto ret = std::to_chars(buf, buf+sizeof(buf), f, std::chars_format::scientific);
		end = ret.ptr;
		if (end[-2] == '0' && end[-4] == 'e') // can't underflow, shortest possible output in scientific is 1e+01
		{
			end[-2] = end[-1];
			end--;
		}
	}
	return bytesr((uint8_t*)buf, end-buf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0xf8(%rbp)
movq %rdi, %rax
movq %rax, -0xf0(%rbp)
movq %rdi, -0x80(%rbp)
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
callq 0x3fc60
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3f64f
jmp 0x3f68a
movq -0xf8(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq 0x2485c(%rip), %rax     # 0x63ebd
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x5c00
movq -0x100(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x5c20
jmp 0x3f923
movsd -0x88(%rbp), %xmm0
callq 0x3fc80
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3f6a1
jmp 0x3f6fb
movsd -0x88(%rbp), %xmm0
callq 0x3fcc0
movq -0xf8(%rbp), %rcx
movb %al, %sil
leaq 0x247fa(%rip), %rax     # 0x63eb9
leaq 0x247f2(%rip), %rdx     # 0x63eb8
testb $0x1, %sil
cmovneq %rdx, %rax
movq %rcx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x5c00
movq -0x108(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x5c20
jmp 0x3f923
movsd -0x88(%rbp), %xmm0
movaps 0x24766(%rip), %xmm1    # 0x63e70
pand %xmm1, %xmm0
movsd 0x2472a(%rip), %xmm1    # 0x63e40
ucomisd %xmm1, %xmm0
jb 0x3f745
movsd -0x88(%rbp), %xmm1
movaps 0x24745(%rip), %xmm0    # 0x63e70
pand %xmm0, %xmm1
movb $0x1, %al
movsd 0x246ef(%rip), %xmm0    # 0x63e28
ucomisd %xmm1, %xmm0
movb %al, -0x109(%rbp)
ja 0x3f762
movsd -0x88(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
sete %al
setnp %cl
andb %cl, %al
movb %al, -0x109(%rbp)
movb -0x109(%rbp), %al
andb $0x1, %al
movb %al, -0x89(%rbp)
testb $0x1, -0x89(%rbp)
je 0x3f7bd
leaq -0xb0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
addq $0x20, %rsi
movsd -0x88(%rbp), %xmm0
movl $0x2, %edx
callq 0x41b0
movq %rax, -0xc8(%rbp)
movl %edx, -0xc0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x3f844
leaq -0xb0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
addq $0x20, %rsi
movsd -0x88(%rbp), %xmm0
movl $0x1, %edx
callq 0x41b0
movq %rax, -0xd8(%rbp)
movl %edx, -0xd0(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movsbl -0x2(%rax), %eax
cmpl $0x30, %eax
jne 0x3f842
movq -0xb8(%rbp), %rax
movsbl -0x4(%rax), %eax
cmpl $0x65, %eax
jne 0x3f842
movq -0xb8(%rbp), %rax
movb -0x1(%rax), %cl
movq -0xb8(%rbp), %rax
movb %cl, -0x2(%rax)
movq -0xb8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xb8(%rbp)
jmp 0x3f844
leaq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0xb0(%rbp), %rax
subq %rax, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x5d70
movq -0xf8(%rbp), %rax
movq -0xe8(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x38(%rbp)
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x5c00
movq -0x130(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq -0x38(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq %rdx, -0x28(%rbp)
movq %rcx, -0x20(%rbp)
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x62c0
movq %rax, -0x120(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x6290
movq -0x128(%rbp), %rdx
movq -0x120(%rbp), %rcx
movq %rdx, -0x8(%rbp)
movq %rcx, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x36f10
jmp 0x3f923
movq -0xf0(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)