1 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 // See https://llvm.org/LICENSE.txt for license information.
3 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 #include "../assembly.h"
7 // float __floatundisf(du_int a);
9 // Note that there is a hardware instruction, fildll, that does most of what
10 // this function needs to do. However, because of our ia32 ABI, it will take
11 // a write-small read-large stall, so the software implementation here is
12 // actually several cycles faster.
14 // This is a branch-free implementation. A branchy implementation might be
15 // faster for the common case if you know something a priori about the input
18 /* branch-free x87 implementation - one cycle slower than without x87.
25 .quad 0x43f0000000000000
26 twop64: .quad 0x0000000000000000
28 #define TWOp64 twop64-0b(%ecx,%eax,8)
32 DEFINE_COMPILERRT_FUNCTION(__floatundisf)
36 punpckldq %xmm1, %xmm0
46 END_COMPILERRT_FUNCTION(__floatundisf)
52 // branch-free, x87-free implementation - faster at the expense of code size
60 .quad 0x4330000000000000
61 .quad 0x0000000000000fff
65 .quad 0x0000000000000000
72 #define TWOp52 twop52-0b(%ecx)
73 #define STICKY sticky-0b(%ecx,%eax,8)
77 DEFINE_COMPILERRT_FUNCTION(__floatundisf)
81 punpckldq %xmm1, %xmm0
85 shrl %eax // high 31 bits of input as sint32
86 addl $0x7ff80000, %eax
87 sarl $31, %eax // (big input) ? -1 : 0
88 movsd STICKY, %xmm1 // (big input) ? 0xfff : 0
90 andl %eax, %edx // (big input) ? 12 : 0
92 andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0
93 movsd TWOp52, %xmm2 // 0x1.0p52
94 psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input
95 orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input)
96 orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input)
97 subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input)
98 cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input)
100 paddd %xmm3, %xmm0 // (float)input
104 END_COMPILERRT_FUNCTION(__floatundisf)
108 NO_EXEC_STACK_DIRECTIVE