63 lines · c
1//===-- fnorm2.c - Handle single-precision denormal inputs to binary op ---===//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 helper function is available for use by single-precision float10// arithmetic implementations, to handle denormal inputs on entry by11// renormalizing the mantissa and modifying the exponent to match.12//13//===----------------------------------------------------------------------===//14 15#include <stdint.h>16 17// Structure containing the function's inputs and outputs.18//19// On entry: a, b are two input floating-point numbers, still in IEEE 75420// encoding. expa and expb are the 8-bit exponents of those numbers, extracted21// and shifted down to the low 8 bits of the word, with no other change.22// Neither value should be zero, or have the maximum exponent (indicating an23// infinity or NaN).24//25// On exit: each of a and b contains the mantissa of the input value, with the26// leading 1 bit made explicit, and shifted up to the top of the word. If expa27// was zero (indicating that a was denormal) then it is now represented as a28// normalized number with an out-of-range exponent (zero or negative). The same29// applies to expb and b.30struct fnorm2 {31 uint32_t a, b, expa, expb;32};33 34void __compiler_rt_fnorm2(struct fnorm2 *values) {35 // Shift the mantissas of a and b to the right place to follow a leading 1 in36 // the top bit, if there is one.37 values->a <<= 8;38 values->b <<= 8;39 40 // Test if a is denormal.41 if (values->expa == 0) {42 // If so, decide how much further up to shift its mantissa, and adjust its43 // exponent to match. This brings the leading 1 of the denormal mantissa to44 // the top of values->a.45 uint32_t shift = __builtin_clz(values->a);46 values->a <<= shift;47 values->expa = 1 - shift;48 } else {49 // Otherwise, leave the mantissa of a in its current position, and OR in50 // the explicit leading 1.51 values->a |= 0x80000000;52 }53 54 // Do the same operation on b.55 if (values->expb == 0) {56 uint32_t shift = __builtin_clz(values->b);57 values->b <<= shift;58 values->expb = 1 - shift;59 } else {60 values->b |= 0x80000000;61 }62}63