blob: a1b29667c8673e6c20c934b9ae762b2687e69e85 [file] [log] [blame]
Daniel Dunbarfd089992009-06-26 16:47:03 +00001// This file is distributed under the University of Illinois Open Source
2// License. See LICENSE.TXT for details.
3
4// float __floatundisf(du_int a);
5
6// Note that there is a hardware instruction, fildll, that does most of what
7// this function needs to do. However, because of our ia32 ABI, it will take
8// a write-small read-large stall, so the software implementation here is
9// actually several cycles faster.
10
11// This is a branch-free implementation. A branchy implementation might be
12// faster for the common case if you know something a priori about the input
13// distribution.
14
15/* branch-free x87 implementation - one cycle slower than without x87.
16
17#ifdef __i386__
18
19.const
20.align 3
21
22 .quad 0x43f0000000000000
23twop64: .quad 0x0000000000000000
24
25#define TWOp64 twop64-0b(%ecx,%eax,8)
26
27.text
28.align 4
29.globl ___floatundisf
30___floatundisf:
31 movl 8(%esp), %eax
32 movd 8(%esp), %xmm1
33 movd 4(%esp), %xmm0
34 punpckldq %xmm1, %xmm0
35 calll 0f
360: popl %ecx
37 sarl $31, %eax
38 movq %xmm0, 4(%esp)
39 fildll 4(%esp)
40 faddl TWOp64
41 fstps 4(%esp)
42 flds 4(%esp)
43 ret
44
45#endif // __i386__
46
47*/
48
49/* branch-free, x87-free implementation - faster at the expense of code size */
50
51#ifdef __i386__
52
53.const
54.align 3
55twop52: .quad 0x4330000000000000
56 .quad 0x0000000000000fff
57sticky: .quad 0x0000000000000000
58 .long 0x00000012
59twelve: .long 0x00000000
60
61#define TWOp52 twop52-0b(%ecx)
62#define STICKY sticky-0b(%ecx,%eax,8)
63
64.text
65.align 4
66.globl ___floatundisf
67___floatundisf:
68 movl 8(%esp), %eax
69 movd 8(%esp), %xmm1
70 movd 4(%esp), %xmm0
71 punpckldq %xmm1, %xmm0
72
73 calll 0f
740: popl %ecx
75 shrl %eax // high 31 bits of input as sint32
76 addl $0x7ff80000, %eax
77 sarl $31, %eax // (big input) ? -1 : 0
78 movsd STICKY, %xmm1 // (big input) ? 0xfff : 0
79 movl $12, %edx
80 andl %eax, %edx // (big input) ? 12 : 0
81 movd %edx, %xmm3
82 andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0
83 movsd TWOp52, %xmm2 // 0x1.0p52
84 psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input
85 orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input)
86 orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input)
87 subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input)
88 cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input)
89 pslld $23, %xmm3
90 paddd %xmm3, %xmm0 // (float)input
91 movd %xmm0, 4(%esp)
92 flds 4(%esp)
93 ret
94
95#endif // __i386__