llvm-project
66 строк · 1.9 Кб
1//===-- lib/floatsisf.c - integer -> single-precision conversion --*- C -*-===//
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 integer to single-precision conversion for the
10// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
11// mode.
12//
13//===----------------------------------------------------------------------===//
14
15#define SINGLE_PRECISION
16#include "fp_lib.h"
17
18#include "int_lib.h"
19
20COMPILER_RT_ABI fp_t __floatsisf(si_int a) {
21
22const int aWidth = sizeof a * CHAR_BIT;
23
24// Handle zero as a special case to protect clz
25if (a == 0)
26return fromRep(0);
27
28// All other cases begin by extracting the sign and absolute value of a
29rep_t sign = 0;
30su_int aAbs = (su_int)a;
31if (a < 0) {
32sign = signBit;
33aAbs = -aAbs;
34}
35
36// Exponent of (fp_t)a is the width of abs(a).
37const int exponent = (aWidth - 1) - clzsi(aAbs);
38rep_t result;
39
40// Shift a into the significand field, rounding if it is a right-shift
41if (exponent <= significandBits) {
42const int shift = significandBits - exponent;
43result = (rep_t)aAbs << shift ^ implicitBit;
44} else {
45const int shift = exponent - significandBits;
46result = (rep_t)aAbs >> shift ^ implicitBit;
47rep_t round = (rep_t)aAbs << (typeWidth - shift);
48if (round > signBit)
49result++;
50if (round == signBit)
51result += result & 1;
52}
53
54// Insert the exponent
55result += (rep_t)(exponent + exponentBias) << significandBits;
56// Insert the sign bit and return
57return fromRep(result | sign);
58}
59
60#if defined(__ARM_EABI__)
61#if defined(COMPILER_RT_ARMHF_TARGET)
62AEABI_RTABI fp_t __aeabi_i2f(int a) { return __floatsisf(a); }
63#else
64COMPILER_RT_ALIAS(__floatsisf, __aeabi_i2f)
65#endif
66#endif
67