brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · fd29e15 Raw
79 lines · c
1//===-- funder.c - Handle single-precision floating-point underflow -------===//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 underflowed output values, if they were11// computed in the form of a normalized mantissa and an out-of-range exponent.12//13// On input: x should be a complete IEEE 754 floating-point value representing14// the desired output scaled up by 2^192 (the same value that would have been15// passed to an underflow trap handler in IEEE 754:1985).16//17// This isn't enough information to re-round to the correct output denormal18// without also knowing whether x itself has already been rounded, and which19// way. 'errsign' gives this information, by indicating the sign of the value20// (true result - x). That is, if errsign > 0 it means the true value was21// larger (x was rounded down); if errsign < 0 then x was rounded up; if22// errsign == 0 then x represents the _exact_ desired output value.23//24//===----------------------------------------------------------------------===//25 26#include <stdint.h>27 28#define SIGNBIT 0x8000000029#define MANTSIZE 2330#define BIAS 0xc031 32uint32_t __compiler_rt_funder(uint32_t x, uint32_t errsign) {33  uint32_t sign = x & SIGNBIT;34  uint32_t exponent = (x << 1) >> 24;35 36  // Rule out exponents so small (or large!) that no denormalisation37  // is needed.38  if (exponent > BIAS) {39    // Exponent 0xc1 or above means a normalised number got here by40    // mistake, so we just remove the 0xc0 exponent bias and go41    // straight home.42    return x - (BIAS << MANTSIZE);43  }44  uint32_t bits_lost = BIAS + 1 - exponent;45  if (bits_lost > MANTSIZE + 1) {46    // The implicit leading 1 of the intermediate value's mantissa is47    // below the lowest mantissa bit of a denormal by at least 2 bits.48    // Round down to 0 unconditionally.49    return sign;50  }51 52  // Make the full mantissa (with leading bit) at the top of the word.53  uint32_t mantissa = 0x80000000 | (x << 8);54  // Adjust by 1 depending on the sign of the error.55  mantissa -= errsign >> 31;56  mantissa += (-errsign) >> 31;57 58  // Shift down to the output position, keeping the bits shifted off.59  uint32_t outmant, shifted_off;60  if (bits_lost == MANTSIZE + 1) {61    // Special case for the exponent where we have to shift the whole62    // of 'mantissa' off the bottom of the word.63    outmant = 0;64    shifted_off = mantissa;65  } else {66    outmant = mantissa >> (8 + bits_lost);67    shifted_off = mantissa << (32 - (8 + bits_lost));68  }69 70  // Re-round.71  if (shifted_off >> 31) {72    outmant++;73    if (!(shifted_off << 1))74      outmant &= ~1; // halfway case: round to even75  }76 77  return sign | outmant;78}79