blob: 39f9dcf115c075d5cb0e732123f4493a538f9cb6 [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
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
21.const
22.align 3
23
24 .quad 0x43f0000000000000
25twop64: .quad 0x0000000000000000
26
27#define TWOp64 twop64-0b(%ecx,%eax,8)
28
29.text
30.align 4
31.globl ___floatundisf
32___floatundisf:
33 movl 8(%esp), %eax
34 movd 8(%esp), %xmm1
35 movd 4(%esp), %xmm0
36 punpckldq %xmm1, %xmm0
37 calll 0f
380: popl %ecx
39 sarl $31, %eax
40 movq %xmm0, 4(%esp)
41 fildll 4(%esp)
42 faddl TWOp64
43 fstps 4(%esp)
44 flds 4(%esp)
45 ret
46
47#endif // __i386__
48
49*/
50
51/* branch-free, x87-free implementation - faster at the expense of code size */
52
53#ifdef __i386__
54
55.const
56.align 3
57twop52: .quad 0x4330000000000000
58 .quad 0x0000000000000fff
59sticky: .quad 0x0000000000000000
60 .long 0x00000012
61twelve: .long 0x00000000
62
63#define TWOp52 twop52-0b(%ecx)
64#define STICKY sticky-0b(%ecx,%eax,8)
65
66.text
67.align 4
68.globl ___floatundisf
69___floatundisf:
70 movl 8(%esp), %eax
71 movd 8(%esp), %xmm1
72 movd 4(%esp), %xmm0
73 punpckldq %xmm1, %xmm0
74
75 calll 0f
760: popl %ecx
77 shrl %eax // high 31 bits of input as sint32
78 addl $0x7ff80000, %eax
79 sarl $31, %eax // (big input) ? -1 : 0
80 movsd STICKY, %xmm1 // (big input) ? 0xfff : 0
81 movl $12, %edx
82 andl %eax, %edx // (big input) ? 12 : 0
83 movd %edx, %xmm3
84 andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0
85 movsd TWOp52, %xmm2 // 0x1.0p52
86 psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input
87 orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input)
88 orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input)
89 subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input)
90 cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input)
91 pslld $23, %xmm3
92 paddd %xmm3, %xmm0 // (float)input
93 movd %xmm0, 4(%esp)
94 flds 4(%esp)
95 ret
96
97#endif // __i386__