71 lines · c
1//===-- floatuntixf.c - Implement __floatuntixf ---------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements __floatuntixf for the compiler_rt library.10//11//===----------------------------------------------------------------------===//12 13#include "int_lib.h"14 15#ifdef CRT_HAS_128BIT16 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 12820// bits tu_int is a 128 bit integral type21 22// gggg gggg gggg gggg gggg gggg gggg gggg | gggg gggg gggg gggg seee eeee eeee23// eeee | 1mmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm24// mmmm mmmm mmmm25 26COMPILER_RT_ABI xf_float __floatuntixf(tu_int a) {27 if (a == 0)28 return 0.0;29 const unsigned N = sizeof(tu_int) * CHAR_BIT;30 int sd = N - __clzti2(a); // number of significant digits31 int e = sd - 1; // exponent32 if (sd > LDBL_MANT_DIG) {33 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx34 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR35 // 1234567890123456789012345636 // 1 = msb 1 bit37 // P = bit LDBL_MANT_DIG-1 bits to the right of 138 // Q = bit LDBL_MANT_DIG bits to the right of 139 // R = "or" of all bits to the right of Q40 switch (sd) {41 case LDBL_MANT_DIG + 1:42 a <<= 1;43 break;44 case LDBL_MANT_DIG + 2:45 break;46 default:47 a = (a >> (sd - (LDBL_MANT_DIG + 2))) |48 ((a & ((tu_int)(-1) >> ((N + LDBL_MANT_DIG + 2) - sd))) != 0);49 };50 // finish:51 a |= (a & 4) != 0; // Or P into R52 ++a; // round - this step may add a significant bit53 a >>= 2; // dump Q and R54 // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits55 if (a & ((tu_int)1 << LDBL_MANT_DIG)) {56 a >>= 1;57 ++e;58 }59 // a is now rounded to LDBL_MANT_DIG bits60 } else {61 a <<= (LDBL_MANT_DIG - sd);62 // a is now rounded to LDBL_MANT_DIG bits63 }64 xf_bits fb;65 fb.u.high.s.low = (e + 16383); // exponent66 fb.u.low.all = (du_int)a; // mantissa67 return fb.f;68}69 70#endif71