Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 1 | /* ===-- floatuntisf.c - Implement __floatuntisf ---------------------------=== |
| 2 | * |
Chandler Carruth | 9d304d2 | 2019-01-19 10:56:40 +0000 | [diff] [blame^] | 3 | * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | * See https://llvm.org/LICENSE.txt for license information. |
| 5 | * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 6 | * |
| 7 | * ===----------------------------------------------------------------------=== |
| 8 | * |
| 9 | * This file implements __floatuntisf for the compiler_rt library. |
| 10 | * |
| 11 | * ===----------------------------------------------------------------------=== |
| 12 | */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 13 | |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 14 | #include "int_lib.h" |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 15 | |
Joerg Sonnenberger | 0cb7cbc | 2014-02-21 23:53:03 +0000 | [diff] [blame] | 16 | #ifdef CRT_HAS_128BIT |
Chandler Carruth | 18db334 | 2012-06-22 21:09:22 +0000 | [diff] [blame] | 17 | |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 18 | /* Returns: convert a to a float, rounding toward even. */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 19 | |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 20 | /* Assumption: float is a IEEE 32 bit floating point type |
| 21 | * tu_int is a 128 bit integral type |
| 22 | */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 24 | /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 25 | |
Joerg Sonnenberger | 165d633 | 2014-03-01 15:30:50 +0000 | [diff] [blame] | 26 | COMPILER_RT_ABI float |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 27 | __floatuntisf(tu_int a) |
| 28 | { |
| 29 | if (a == 0) |
| 30 | return 0.0F; |
| 31 | const unsigned N = sizeof(tu_int) * CHAR_BIT; |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 32 | int sd = N - __clzti2(a); /* number of significant digits */ |
| 33 | int e = sd - 1; /* exponent */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 34 | if (sd > FLT_MANT_DIG) |
| 35 | { |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 36 | /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx |
| 37 | * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR |
| 38 | * 12345678901234567890123456 |
| 39 | * 1 = msb 1 bit |
| 40 | * P = bit FLT_MANT_DIG-1 bits to the right of 1 |
| 41 | * Q = bit FLT_MANT_DIG bits to the right of 1 |
| 42 | * R = "or" of all bits to the right of Q |
| 43 | */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 44 | switch (sd) |
| 45 | { |
| 46 | case FLT_MANT_DIG + 1: |
| 47 | a <<= 1; |
| 48 | break; |
| 49 | case FLT_MANT_DIG + 2: |
| 50 | break; |
| 51 | default: |
| 52 | a = (a >> (sd - (FLT_MANT_DIG+2))) | |
| 53 | ((a & ((tu_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0); |
| 54 | }; |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 55 | /* finish: */ |
| 56 | a |= (a & 4) != 0; /* Or P into R */ |
| 57 | ++a; /* round - this step may add a significant bit */ |
| 58 | a >>= 2; /* dump Q and R */ |
| 59 | /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 60 | if (a & ((tu_int)1 << FLT_MANT_DIG)) |
| 61 | { |
| 62 | a >>= 1; |
| 63 | ++e; |
| 64 | } |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 65 | /* a is now rounded to FLT_MANT_DIG bits */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | a <<= (FLT_MANT_DIG - sd); |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 70 | /* a is now rounded to FLT_MANT_DIG bits */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 71 | } |
| 72 | float_bits fb; |
Edward O'Callaghan | 42f5e76 | 2009-08-05 19:06:50 +0000 | [diff] [blame] | 73 | fb.u = ((e + 127) << 23) | /* exponent */ |
| 74 | ((su_int)a & 0x007FFFFF); /* mantissa */ |
Daniel Dunbar | d42efbe | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 75 | return fb.f; |
| 76 | } |
| 77 | |
Joerg Sonnenberger | 0cb7cbc | 2014-02-21 23:53:03 +0000 | [diff] [blame] | 78 | #endif /* CRT_HAS_128BIT */ |