10b57cec5SDimitry Andric //===-- floatundisf.c - Implement __floatundisf ---------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements __floatundisf for the compiler_rt library. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric // Returns: convert a to a float, rounding toward even. 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric // Assumption: float is a IEEE 32 bit floating point type 160b57cec5SDimitry Andric // du_int is a 64 bit integral type 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric // seee eeee emmm mmmm mmmm mmmm mmmm mmmm 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #include "int_lib.h" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric COMPILER_RT_ABI float __floatundisf(du_int a) { 230b57cec5SDimitry Andric if (a == 0) 240b57cec5SDimitry Andric return 0.0F; 250b57cec5SDimitry Andric const unsigned N = sizeof(du_int) * CHAR_BIT; 260b57cec5SDimitry Andric int sd = N - __builtin_clzll(a); // number of significant digits 27*5ffd83dbSDimitry Andric si_int e = sd - 1; // 8 exponent 280b57cec5SDimitry Andric if (sd > FLT_MANT_DIG) { 290b57cec5SDimitry Andric // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx 300b57cec5SDimitry Andric // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR 310b57cec5SDimitry Andric // 12345678901234567890123456 320b57cec5SDimitry Andric // 1 = msb 1 bit 330b57cec5SDimitry Andric // P = bit FLT_MANT_DIG-1 bits to the right of 1 340b57cec5SDimitry Andric // Q = bit FLT_MANT_DIG bits to the right of 1 350b57cec5SDimitry Andric // R = "or" of all bits to the right of Q 360b57cec5SDimitry Andric switch (sd) { 370b57cec5SDimitry Andric case FLT_MANT_DIG + 1: 380b57cec5SDimitry Andric a <<= 1; 390b57cec5SDimitry Andric break; 400b57cec5SDimitry Andric case FLT_MANT_DIG + 2: 410b57cec5SDimitry Andric break; 420b57cec5SDimitry Andric default: 430b57cec5SDimitry Andric a = (a >> (sd - (FLT_MANT_DIG + 2))) | 440b57cec5SDimitry Andric ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG + 2) - sd))) != 0); 450b57cec5SDimitry Andric }; 460b57cec5SDimitry Andric // finish: 470b57cec5SDimitry Andric a |= (a & 4) != 0; // Or P into R 480b57cec5SDimitry Andric ++a; // round - this step may add a significant bit 490b57cec5SDimitry Andric a >>= 2; // dump Q and R 500b57cec5SDimitry Andric // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits 510b57cec5SDimitry Andric if (a & ((du_int)1 << FLT_MANT_DIG)) { 520b57cec5SDimitry Andric a >>= 1; 530b57cec5SDimitry Andric ++e; 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric // a is now rounded to FLT_MANT_DIG bits 560b57cec5SDimitry Andric } else { 570b57cec5SDimitry Andric a <<= (FLT_MANT_DIG - sd); 580b57cec5SDimitry Andric // a is now rounded to FLT_MANT_DIG bits 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric float_bits fb; 610b57cec5SDimitry Andric fb.u = ((e + 127) << 23) | // exponent 620b57cec5SDimitry Andric ((su_int)a & 0x007FFFFF); // mantissa 630b57cec5SDimitry Andric return fb.f; 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric #if defined(__ARM_EABI__) 670b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET) 680b57cec5SDimitry Andric AEABI_RTABI float __aeabi_ul2f(du_int a) { return __floatundisf(a); } 690b57cec5SDimitry Andric #else 700b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatundisf, __aeabi_ul2f) 710b57cec5SDimitry Andric #endif 720b57cec5SDimitry Andric #endif 73