blob: e4bcb5ff00920b7114de3dd31634b802dbe59dab [file] [log] [blame]
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00001/* ===-- floattixf.c - Implement __floattixf -------------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan2bf62722009-08-05 04:02:56 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floattixf for the compiler_rt library.
11 *
12 * ===----------------------------------------------------------------------===
13 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000014
15#if __x86_64
16
17#include "int_lib.h"
18#include <float.h>
19
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000020/* Returns: convert a to a long double, rounding toward even. */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000021
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000022/* Assumption: long double is a IEEE 80 bit floating point type padded to 128 bits
23 * ti_int is a 128 bit integral type
24 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000025
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000026/* gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee eeee |
27 * 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm
28 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000029
30si_int __clzti2(ti_int a);
31
32long double
33__floattixf(ti_int a)
34{
35 if (a == 0)
36 return 0.0;
37 const unsigned N = sizeof(ti_int) * CHAR_BIT;
38 const ti_int s = a >> (N-1);
39 a = (a ^ s) - s;
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000040 int sd = N - __clzti2(a); /* number of significant digits */
41 int e = sd - 1; /* exponent */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000042 if (sd > LDBL_MANT_DIG)
43 {
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000044 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
45 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
46 * 12345678901234567890123456
47 * 1 = msb 1 bit
48 * P = bit LDBL_MANT_DIG-1 bits to the right of 1
49 * Q = bit LDBL_MANT_DIG bits to the right of 1
50 * R = "or" of all bits to the right of Q
Edward O'Callaghan37a6a452009-08-07 20:30:09 +000051 */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000052 switch (sd)
53 {
54 case LDBL_MANT_DIG + 1:
55 a <<= 1;
56 break;
57 case LDBL_MANT_DIG + 2:
58 break;
59 default:
60 a = ((tu_int)a >> (sd - (LDBL_MANT_DIG+2))) |
61 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG+2) - sd))) != 0);
62 };
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000063 /* finish: */
64 a |= (a & 4) != 0; /* Or P into R */
65 ++a; /* round - this step may add a significant bit */
66 a >>= 2; /* dump Q and R */
67 /* a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000068 if (a & ((tu_int)1 << LDBL_MANT_DIG))
69 {
70 a >>= 1;
71 ++e;
72 }
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000073 /* a is now rounded to LDBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000074 }
75 else
76 {
77 a <<= (LDBL_MANT_DIG - sd);
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000078 /* a is now rounded to LDBL_MANT_DIG bits */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000079 }
80 long_double_bits fb;
Edward O'Callaghanaabd9612009-09-03 09:12:20 +000081 fb.u.high.s.low = ((su_int)s & 0x8000) | /* sign */
Edward O'Callaghan2bf62722009-08-05 04:02:56 +000082 (e + 16383); /* exponent */
83 fb.u.low.all = (du_int)a; /* mantissa */
Daniel Dunbarb3a69012009-06-26 16:47:03 +000084 return fb.f;
85}
86
87#endif