mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-28 06:32:09 +00:00
f0d0a1681a
When truncating to a format with fewer mantissa bits, APFloat::convert will perform a right shift of the mantissa by the difference of the precision of the two formats. Usually, this will result in just the mantissa bits needed for the target format. One special situation is if the input number is denormal. In this case, the right shift may discard significant bits. This is usually not a problem, since truncating a denormal usually results in zero (underflow) after normalization anyway, since the result format's exponent range is usually smaller than the target format's. However, there is one case where the latter property does not hold: when truncating from ppc_fp128 to double. In particular, truncating a ppc_fp128 whose first double of the pair is denormal should result in just that first double, not zero. The current code however performs an excessive right shift, resulting in lost result bits. This is then caught in the APFloat::normalize call performed by APFloat::convert and causes an assertion failure. This patch checks for the scenario of truncating a denormal, and attempts to (possibly partially) replace the initial mantissa right shift by decrementing the exponent, if doing so will still result in a valid *target format* exponent. Index: test/CodeGen/PowerPC/pr16573.ll =================================================================== --- test/CodeGen/PowerPC/pr16573.ll (revision 0) +++ test/CodeGen/PowerPC/pr16573.ll (revision 0) @@ -0,0 +1,11 @@ +; RUN: llc < %s | FileCheck %s + +target triple = "powerpc64-unknown-linux-gnu" + +define double @test() { + %1 = fptrunc ppc_fp128 0xM818F2887B9295809800000000032D000 to double + ret double %1 +} + +; CHECK: .quad -9111018957755033591 + Index: lib/Support/APFloat.cpp =================================================================== --- lib/Support/APFloat.cpp (revision 185817) +++ lib/Support/APFloat.cpp (working copy) @@ -1956,6 +1956,23 @@ X86SpecialNan = true; } + // If this is a truncation of a denormal number, and the target semantics + // has larger exponent range than the source semantics (this can happen + // when truncating from PowerPC double-double to double format), the + // right shift could lose result mantissa bits. Adjust exponent instead + // of performing excessive shift. + if (shift < 0 && isFiniteNonZero()) { + int exponentChange = significandMSB() + 1 - fromSemantics.precision; + if (exponent + exponentChange < toSemantics.minExponent) + exponentChange = toSemantics.minExponent - exponent; + if (exponentChange < shift) + exponentChange = shift; + if (exponentChange < 0) { + shift -= exponentChange; + exponent += exponentChange; + } + } + // If this is a truncation, perform the shift before we narrow the storage. if (shift < 0 && (isFiniteNonZero() || category==fcNaN)) lostFraction = shiftRight(significandParts(), oldPartCount, -shift); git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186409 91177308-0d34-0410-b5e6-96231b3b80d8
Design Of lib/System ==================== The software in this directory is designed to completely shield LLVM from any and all operating system specific functionality. It is not intended to be a complete operating system wrapper (such as ACE), but only to provide the functionality necessary to support LLVM. The software located here, of necessity, has very specific and stringent design rules. Violation of these rules means that cracks in the shield could form and the primary goal of the library is defeated. By consistently using this library, LLVM becomes more easily ported to new platforms since the only thing requiring porting is this library. Complete documentation for the library can be found in the file: llvm/docs/SystemLibrary.html or at this URL: http://llvm.org/docs/SystemLibrary.html While we recommend that you read the more detailed documentation, for the impatient, here's a high level summary of the library's requirements. 1. No system header files are to be exposed through the interface. 2. Std C++ and Std C header files are okay to be exposed through the interface. 3. No exposed system-specific functions. 4. No exposed system-specific data. 5. Data in lib/System classes must use only simple C++ intrinsic types. 6. Errors are handled by returning "true" and setting an optional std::string 7. Library must not throw any exceptions, period. 8. Interface functions must not have throw() specifications. 9. No duplicate function impementations are permitted within an operating system class. To accomplish these requirements, the library has numerous design criteria that must be satisfied. Here's a high level summary of the library's design criteria: 1. No unused functionality (only what LLVM needs) 2. High-Level Interfaces 3. Use Opaque Classes 4. Common Implementations 5. Multiple Implementations 6. Minimize Memory Allocation 7. No Virtual Methods