blob: 88e2e81ace9dc56a05d05f207030130d6b9a194b [file] [log] [blame]
Edward O'Callaghan37a6a452009-08-07 20:30:09 +00001/*===-- floatdisf.c - Implement __floatdisf -------------------------------===
2 *
Chandler Carruth7a739a02019-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'Callaghan37a6a452009-08-07 20:30:09 +00006 *
7 *===----------------------------------------------------------------------===
8 *
9 * This file implements __floatdisf for the compiler_rt library.
10 *
11 *===----------------------------------------------------------------------===
12 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000013
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000014/* Returns: convert a to a float, rounding toward even.*/
Daniel Dunbarb3a69012009-06-26 16:47:03 +000015
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000016/* Assumption: float is a IEEE 32 bit floating point type
17 * di_int is a 64 bit integral type
18 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000019
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000020/* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000021
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000022#include "int_lib.h"
23
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000024COMPILER_RT_ABI float
Daniel Dunbarb3a69012009-06-26 16:47:03 +000025__floatdisf(di_int a)
26{
27 if (a == 0)
28 return 0.0F;
29 const unsigned N = sizeof(di_int) * CHAR_BIT;
30 const di_int s = a >> (N-1);
31 a = (a ^ s) - s;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000032 int sd = N - __builtin_clzll(a); /* number of significant digits */
33 int e = sd - 1; /* exponent */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000034 if (sd > FLT_MANT_DIG)
35 {
Edward O'Callaghan37a6a452009-08-07 20:30:09 +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 Dunbarb3a69012009-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 = ((du_int)a >> (sd - (FLT_MANT_DIG+2))) |
53 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
54 };
Edward O'Callaghan37a6a452009-08-07 20:30:09 +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 Dunbarb3a69012009-06-26 16:47:03 +000060 if (a & ((du_int)1 << FLT_MANT_DIG))
61 {
62 a >>= 1;
63 ++e;
64 }
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000065 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000066 }
67 else
68 {
69 a <<= (FLT_MANT_DIG - sd);
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000070 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000071 }
72 float_bits fb;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000073 fb.u = ((su_int)s & 0x80000000) | /* sign */
74 ((e + 127) << 23) | /* exponent */
75 ((su_int)a & 0x007FFFFF); /* mantissa */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000076 return fb.f;
77}
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +000078
79#if defined(__ARM_EABI__)
Eli Friedman83774b42017-10-03 21:25:07 +000080#if defined(COMPILER_RT_ARMHF_TARGET)
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +000081AEABI_RTABI float __aeabi_l2f(di_int a) {
82 return __floatdisf(a);
83}
Eli Friedman83774b42017-10-03 21:25:07 +000084#else
85AEABI_RTABI float __aeabi_l2f(di_int a) COMPILER_RT_ALIAS(__floatdisf);
Saleem Abdulrasool99e2e662017-05-16 16:41:37 +000086#endif
Eli Friedman83774b42017-10-03 21:25:07 +000087#endif