Howard Hinnant | 5b791f6 | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 1 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 2 | // Source Licenses. See LICENSE.TXT for details. |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 3 | |
Daniel Dunbar | 7d50478 | 2009-10-27 17:49:50 +0000 | [diff] [blame] | 4 | #include "../assembly.h" |
| 5 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 6 | // 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 |
Saleem Abdulrasool | 310874a | 2014-05-12 15:23:37 +0000 | [diff] [blame] | 22 | .balign 3 |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | |
| 24 | .quad 0x43f0000000000000 |
| 25 | twop64: .quad 0x0000000000000000 |
| 26 | |
| 27 | #define TWOp64 twop64-0b(%ecx,%eax,8) |
| 28 | |
| 29 | .text |
Saleem Abdulrasool | 310874a | 2014-05-12 15:23:37 +0000 | [diff] [blame] | 30 | .balign 4 |
Daniel Dunbar | 9ff9371 | 2009-10-27 17:50:21 +0000 | [diff] [blame] | 31 | DEFINE_COMPILERRT_FUNCTION(__floatundisf) |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 32 | movl 8(%esp), %eax |
| 33 | movd 8(%esp), %xmm1 |
| 34 | movd 4(%esp), %xmm0 |
| 35 | punpckldq %xmm1, %xmm0 |
| 36 | calll 0f |
| 37 | 0: 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 Sonnenberger | 2a10033 | 2014-01-24 14:40:53 +0000 | [diff] [blame] | 45 | END_COMPILERRT_FUNCTION(__floatundisf) |
| 46 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 47 | #endif // __i386__ |
| 48 | |
| 49 | */ |
| 50 | |
| 51 | /* branch-free, x87-free implementation - faster at the expense of code size */ |
| 52 | |
| 53 | #ifdef __i386__ |
| 54 | |
Saleem Abdulrasool | 12ae9a8 | 2014-07-26 21:08:41 +0000 | [diff] [blame^] | 55 | #if defined(__APPLE__) |
| 56 | .const |
| 57 | #elif defined(__ELF__) |
| 58 | .section .rodata |
| 59 | #else |
| 60 | .section .rdata,"rd" |
Edward O'Callaghan | 8f40ca3 | 2009-11-04 23:52:51 +0000 | [diff] [blame] | 61 | #endif |
Saleem Abdulrasool | 12ae9a8 | 2014-07-26 21:08:41 +0000 | [diff] [blame^] | 62 | |
Saleem Abdulrasool | 15a906c | 2014-07-26 21:08:34 +0000 | [diff] [blame] | 63 | .balign 16 |
| 64 | twop52: |
| 65 | .quad 0x4330000000000000 |
| 66 | .quad 0x0000000000000fff |
| 67 | |
| 68 | .balign 16 |
| 69 | sticky: |
| 70 | .quad 0x0000000000000000 |
| 71 | .long 0x00000012 |
| 72 | |
| 73 | .balign 16 |
| 74 | twelve: |
| 75 | .long 0x00000000 |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 76 | |
| 77 | #define TWOp52 twop52-0b(%ecx) |
| 78 | #define STICKY sticky-0b(%ecx,%eax,8) |
| 79 | |
| 80 | .text |
Saleem Abdulrasool | 310874a | 2014-05-12 15:23:37 +0000 | [diff] [blame] | 81 | .balign 4 |
Daniel Dunbar | 9ff9371 | 2009-10-27 17:50:21 +0000 | [diff] [blame] | 82 | DEFINE_COMPILERRT_FUNCTION(__floatundisf) |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 83 | movl 8(%esp), %eax |
| 84 | movd 8(%esp), %xmm1 |
| 85 | movd 4(%esp), %xmm0 |
| 86 | punpckldq %xmm1, %xmm0 |
| 87 | |
| 88 | calll 0f |
| 89 | 0: popl %ecx |
| 90 | shrl %eax // high 31 bits of input as sint32 |
| 91 | addl $0x7ff80000, %eax |
| 92 | sarl $31, %eax // (big input) ? -1 : 0 |
| 93 | movsd STICKY, %xmm1 // (big input) ? 0xfff : 0 |
| 94 | movl $12, %edx |
| 95 | andl %eax, %edx // (big input) ? 12 : 0 |
| 96 | movd %edx, %xmm3 |
| 97 | andpd %xmm0, %xmm1 // (big input) ? input & 0xfff : 0 |
| 98 | movsd TWOp52, %xmm2 // 0x1.0p52 |
| 99 | psrlq %xmm3, %xmm0 // (big input) ? input >> 12 : input |
| 100 | orpd %xmm2, %xmm1 // 0x1.0p52 + ((big input) ? input & 0xfff : input) |
| 101 | orpd %xmm1, %xmm0 // 0x1.0p52 + ((big input) ? (input >> 12 | input & 0xfff) : input) |
| 102 | subsd %xmm2, %xmm0 // (double)((big input) ? (input >> 12 | input & 0xfff) : input) |
| 103 | cvtsd2ss %xmm0, %xmm0 // (float)((big input) ? (input >> 12 | input & 0xfff) : input) |
| 104 | pslld $23, %xmm3 |
| 105 | paddd %xmm3, %xmm0 // (float)input |
| 106 | movd %xmm0, 4(%esp) |
| 107 | flds 4(%esp) |
| 108 | ret |
Joerg Sonnenberger | 2a10033 | 2014-01-24 14:40:53 +0000 | [diff] [blame] | 109 | END_COMPILERRT_FUNCTION(__floatundisf) |
| 110 | |
Daniel Dunbar | fd08999 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 111 | #endif // __i386__ |