1156cd587Sjoerg// This file is dual licensed under the MIT and the University of Illinois Open 2156cd587Sjoerg// Source Licenses. See LICENSE.TXT for details. 3156cd587Sjoerg 4156cd587Sjoerg#include "../assembly.h" 5156cd587Sjoerg 6156cd587Sjoerg// float __floatundisf(du_int a); 7156cd587Sjoerg 8156cd587Sjoerg// Note that there is a hardware instruction, fildll, that does most of what 9156cd587Sjoerg// this function needs to do. However, because of our ia32 ABI, it will take 10156cd587Sjoerg// a write-small read-large stall, so the software implementation here is 11156cd587Sjoerg// actually several cycles faster. 12156cd587Sjoerg 13156cd587Sjoerg// This is a branch-free implementation. A branchy implementation might be 14156cd587Sjoerg// faster for the common case if you know something a priori about the input 15156cd587Sjoerg// distribution. 16156cd587Sjoerg 17156cd587Sjoerg/* branch-free x87 implementation - one cycle slower than without x87. 18156cd587Sjoerg 19156cd587Sjoerg#ifdef __i386__ 20156cd587Sjoerg 21*ef84fd3bSjoergCONST_SECTION 2261f2f256Sjoerg.balign 3 23156cd587Sjoerg 24156cd587Sjoerg .quad 0x43f0000000000000 25156cd587Sjoergtwop64: .quad 0x0000000000000000 26156cd587Sjoerg 27156cd587Sjoerg#define TWOp64 twop64-0b(%ecx,%eax,8) 28156cd587Sjoerg 29156cd587Sjoerg.text 3061f2f256Sjoerg.balign 4 31156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__floatundisf) 32156cd587Sjoerg movl 8(%esp), %eax 33156cd587Sjoerg movd 8(%esp), %xmm1 34156cd587Sjoerg movd 4(%esp), %xmm0 35156cd587Sjoerg punpckldq %xmm1, %xmm0 36156cd587Sjoerg calll 0f 37156cd587Sjoerg0: popl %ecx 38156cd587Sjoerg sarl $31, %eax 39156cd587Sjoerg movq %xmm0, 4(%esp) 40156cd587Sjoerg fildll 4(%esp) 41156cd587Sjoerg faddl TWOp64 42156cd587Sjoerg fstps 4(%esp) 43156cd587Sjoerg flds 4(%esp) 44156cd587Sjoerg ret 45156cd587SjoergEND_COMPILERRT_FUNCTION(__floatundisf) 46156cd587Sjoerg 47156cd587Sjoerg#endif // __i386__ 48156cd587Sjoerg 49156cd587Sjoerg*/ 50156cd587Sjoerg 51156cd587Sjoerg/* branch-free, x87-free implementation - faster at the expense of code size */ 52156cd587Sjoerg 53156cd587Sjoerg#ifdef __i386__ 54156cd587Sjoerg 55*ef84fd3bSjoergCONST_SECTION 56190e92d8Sjoerg 57190e92d8Sjoerg .balign 16 58190e92d8Sjoergtwop52: 59190e92d8Sjoerg .quad 0x4330000000000000 60156cd587Sjoerg .quad 0x0000000000000fff 61190e92d8Sjoerg 62190e92d8Sjoerg .balign 16 63190e92d8Sjoergsticky: 64190e92d8Sjoerg .quad 0x0000000000000000 65156cd587Sjoerg .long 0x00000012 66190e92d8Sjoerg 67190e92d8Sjoerg .balign 16 68190e92d8Sjoergtwelve: 69190e92d8Sjoerg .long 0x00000000 70156cd587Sjoerg 71156cd587Sjoerg#define TWOp52 twop52-0b(%ecx) 72156cd587Sjoerg#define STICKY sticky-0b(%ecx,%eax,8) 73156cd587Sjoerg 74156cd587Sjoerg.text 7561f2f256Sjoerg.balign 4 76156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__floatundisf) 77156cd587Sjoerg movl 8(%esp), %eax 78156cd587Sjoerg movd 8(%esp), %xmm1 79156cd587Sjoerg movd 4(%esp), %xmm0 80156cd587Sjoerg punpckldq %xmm1, %xmm0 81156cd587Sjoerg 82156cd587Sjoerg calll 0f 83156cd587Sjoerg0: popl %ecx 84156cd587Sjoerg shrl %eax // high 31 bits of input as sint32 85156cd587Sjoerg addl $0x7ff80000, %eax 86156cd587Sjoerg sarl $31, %eax // (big input) ? -1 : 0 87156cd587Sjoerg movsd STICKY, %xmm1 // (big input) ? 0xfff : 0 88156cd587Sjoerg movl $12, %edx 89156cd587Sjoerg andl %eax, %edx // (big input) ? 12 : 0 90156cd587Sjoerg movd %edx, %xmm3 91156cd587Sjoerg andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0 92156cd587Sjoerg movsd TWOp52, %xmm2 // 0x1.0p52 93156cd587Sjoerg psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input 94156cd587Sjoerg orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input) 95156cd587Sjoerg orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input) 96156cd587Sjoerg subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input) 97156cd587Sjoerg cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input) 98156cd587Sjoerg pslld $23, %xmm3 99156cd587Sjoerg paddd %xmm3, %xmm0 // (float)input 100156cd587Sjoerg movd %xmm0, 4(%esp) 101156cd587Sjoerg flds 4(%esp) 102156cd587Sjoerg ret 103156cd587SjoergEND_COMPILERRT_FUNCTION(__floatundisf) 104156cd587Sjoerg 105156cd587Sjoerg#endif // __i386__ 106