llvm-project
73 строки · 2.5 Кб
1//===-- floattixf.c - Implement __floattixf -------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements __floattixf for the compiler_rt library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "int_lib.h"
14
15#ifdef CRT_HAS_128BIT
16
17// Returns: convert a to a long double, rounding toward even.
18
19// Assumption: long double is a IEEE 80 bit floating point type padded to 128
20// bits ti_int is a 128 bit integral type
21
22// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee
23// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
24// mmmm mmmm mmmm
25
26COMPILER_RT_ABI xf_float __floattixf(ti_int a) {
27if (a == 0)
28return 0.0;
29const unsigned N = sizeof(ti_int) * CHAR_BIT;
30const ti_int s = a >> (N - 1);
31a = (a ^ s) - s;
32int sd = N - __clzti2(a); // number of significant digits
33int e = sd - 1; // exponent
34if (sd > LDBL_MANT_DIG) {
35// start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
36// finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
37// 12345678901234567890123456
38// 1 = msb 1 bit
39// P = bit LDBL_MANT_DIG-1 bits to the right of 1
40// Q = bit LDBL_MANT_DIG bits to the right of 1
41// R = "or" of all bits to the right of Q
42switch (sd) {
43case LDBL_MANT_DIG + 1:
44a <<= 1;
45break;
46case LDBL_MANT_DIG + 2:
47break;
48default:
49a = ((tu_int)a >> (sd - (LDBL_MANT_DIG + 2))) |
50((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG + 2) - sd))) != 0);
51};
52// finish:
53a |= (a & 4) != 0; // Or P into R
54++a; // round - this step may add a significant bit
55a >>= 2; // dump Q and R
56// a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
57if (a & ((tu_int)1 << LDBL_MANT_DIG)) {
58a >>= 1;
59++e;
60}
61// a is now rounded to LDBL_MANT_DIG bits
62} else {
63a <<= (LDBL_MANT_DIG - sd);
64// a is now rounded to LDBL_MANT_DIG bits
65}
66xf_bits fb;
67fb.u.high.s.low = ((su_int)s & 0x8000) | // sign
68(e + 16383); // exponent
69fb.u.low.all = (du_int)a; // mantissa
70return fb.f;
71}
72
73#endif // CRT_HAS_128BIT
74