blob: 052c00d14b5b23d1a0c4d659fa3fcfae98b2c62d [file] [log] [blame]
Edward O'Callaghan42f5e762009-08-05 19:06:50 +00001/* ===-- floatuntisf.c - Implement __floatuntisf ---------------------------===
2 *
Chandler Carruth9d304d22019-01-19 10:56:40 +00003 * 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'Callaghan42f5e762009-08-05 19:06:50 +00006 *
7 * ===----------------------------------------------------------------------===
8 *
9 * This file implements __floatuntisf for the compiler_rt library.
10 *
11 * ===----------------------------------------------------------------------===
12 */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000013
Daniel Dunbard42efbe2009-06-26 16:47:03 +000014#include "int_lib.h"
Daniel Dunbard42efbe2009-06-26 16:47:03 +000015
Joerg Sonnenberger0cb7cbc2014-02-21 23:53:03 +000016#ifdef CRT_HAS_128BIT
Chandler Carruth18db3342012-06-22 21:09:22 +000017
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000018/* Returns: convert a to a float, rounding toward even. */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000019
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000020/* Assumption: float is a IEEE 32 bit floating point type
21 * tu_int is a 128 bit integral type
22 */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000023
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000024/* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000025
Joerg Sonnenberger165d6332014-03-01 15:30:50 +000026COMPILER_RT_ABI float
Daniel Dunbard42efbe2009-06-26 16:47:03 +000027__floatuntisf(tu_int a)
28{
29 if (a == 0)
30 return 0.0F;
31 const unsigned N = sizeof(tu_int) * CHAR_BIT;
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000032 int sd = N - __clzti2(a); /* number of significant digits */
33 int e = sd - 1; /* exponent */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000034 if (sd > FLT_MANT_DIG)
35 {
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000036 /* 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 Dunbard42efbe2009-06-26 16:47:03 +000044 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'Callaghan42f5e762009-08-05 19:06:50 +000055 /* 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 Dunbard42efbe2009-06-26 16:47:03 +000060 if (a & ((tu_int)1 << FLT_MANT_DIG))
61 {
62 a >>= 1;
63 ++e;
64 }
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000065 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000066 }
67 else
68 {
69 a <<= (FLT_MANT_DIG - sd);
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000070 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000071 }
72 float_bits fb;
Edward O'Callaghan42f5e762009-08-05 19:06:50 +000073 fb.u = ((e + 127) << 23) | /* exponent */
74 ((su_int)a & 0x007FFFFF); /* mantissa */
Daniel Dunbard42efbe2009-06-26 16:47:03 +000075 return fb.f;
76}
77
Joerg Sonnenberger0cb7cbc2014-02-21 23:53:03 +000078#endif /* CRT_HAS_128BIT */