blob: 94c97e25aa8c390e41c0860741b454986b8457a9 [file] [log] [blame]
Howard Hinnant5b791f62010-11-16 22:13:33 +00001// This file is dual licensed under the MIT and the University of Illinois Open
2// Source Licenses. See LICENSE.TXT for details.
Daniel Dunbarfd089992009-06-26 16:47:03 +00003
Daniel Dunbar7d504782009-10-27 17:49:50 +00004#include "../assembly.h"
5
Daniel Dunbarfd089992009-06-26 16:47:03 +00006// float __floatundisf(du_int a);
7
8// Note that there is a hardware instruction, fildll, that does most of what
9// this function needs to do. However, because of our ia32 ABI, it will take
10// a write-small read-large stall, so the software implementation here is
11// actually several cycles faster.
12
13// This is a branch-free implementation. A branchy implementation might be
14// faster for the common case if you know something a priori about the input
15// distribution.
16
17/* branch-free x87 implementation - one cycle slower than without x87.
18
19#ifdef __i386__
20
Saleem Abdulrasool1fe0c182014-12-10 02:36:22 +000021CONST_SECTION
Saleem Abdulrasool310874a2014-05-12 15:23:37 +000022.balign 3
Daniel Dunbarfd089992009-06-26 16:47:03 +000023
24 .quad 0x43f0000000000000
25twop64: .quad 0x0000000000000000
26
27#define TWOp64 twop64-0b(%ecx,%eax,8)
28
29.text
Saleem Abdulrasool310874a2014-05-12 15:23:37 +000030.balign 4
Daniel Dunbar9ff93712009-10-27 17:50:21 +000031DEFINE_COMPILERRT_FUNCTION(__floatundisf)
Daniel Dunbarfd089992009-06-26 16:47:03 +000032 movl 8(%esp), %eax
33 movd 8(%esp), %xmm1
34 movd 4(%esp), %xmm0
35 punpckldq %xmm1, %xmm0
36 calll 0f
370: popl %ecx
38 sarl $31, %eax
39 movq %xmm0, 4(%esp)
40 fildll 4(%esp)
41 faddl TWOp64
42 fstps 4(%esp)
43 flds 4(%esp)
44 ret
Joerg Sonnenberger2a100332014-01-24 14:40:53 +000045END_COMPILERRT_FUNCTION(__floatundisf)
46
Daniel Dunbarfd089992009-06-26 16:47:03 +000047#endif // __i386__
48
49*/
50
51/* branch-free, x87-free implementation - faster at the expense of code size */
52
53#ifdef __i386__
54
Saleem Abdulrasool1fe0c182014-12-10 02:36:22 +000055CONST_SECTION
Saleem Abdulrasool12ae9a82014-07-26 21:08:41 +000056
Saleem Abdulrasool15a906c2014-07-26 21:08:34 +000057 .balign 16
58twop52:
59 .quad 0x4330000000000000
60 .quad 0x0000000000000fff
61
62 .balign 16
63sticky:
64 .quad 0x0000000000000000
65 .long 0x00000012
66
67 .balign 16
68twelve:
69 .long 0x00000000
Daniel Dunbarfd089992009-06-26 16:47:03 +000070
71#define TWOp52 twop52-0b(%ecx)
72#define STICKY sticky-0b(%ecx,%eax,8)
73
74.text
Saleem Abdulrasool310874a2014-05-12 15:23:37 +000075.balign 4
Daniel Dunbar9ff93712009-10-27 17:50:21 +000076DEFINE_COMPILERRT_FUNCTION(__floatundisf)
Daniel Dunbarfd089992009-06-26 16:47:03 +000077 movl 8(%esp), %eax
78 movd 8(%esp), %xmm1
79 movd 4(%esp), %xmm0
80 punpckldq %xmm1, %xmm0
81
82 calll 0f
830: popl %ecx
84 shrl %eax // high 31 bits of input as sint32
85 addl $0x7ff80000, %eax
86 sarl $31, %eax // (big input) ? -1 : 0
87 movsd STICKY, %xmm1 // (big input) ? 0xfff : 0
88 movl $12, %edx
89 andl %eax, %edx // (big input) ? 12 : 0
90 movd %edx, %xmm3
91 andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0
92 movsd TWOp52, %xmm2 // 0x1.0p52
93 psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input
94 orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input)
95 orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input)
96 subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input)
97 cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input)
98 pslld $23, %xmm3
99 paddd %xmm3, %xmm0 // (float)input
100 movd %xmm0, 4(%esp)
101 flds 4(%esp)
102 ret
Joerg Sonnenberger2a100332014-01-24 14:40:53 +0000103END_COMPILERRT_FUNCTION(__floatundisf)
104
Daniel Dunbarfd089992009-06-26 16:47:03 +0000105#endif // __i386__