[compiler-rt] r240450 - Fix incorrect truncation at the overflow boundary

Pirama Arumuga Nainar pirama at google.com
Tue Jun 23 13:24:54 PDT 2015


Author: pirama
Date: Tue Jun 23 15:24:53 2015
New Revision: 240450

URL: http://llvm.org/viewvc/llvm-project?rev=240450&view=rev
Log:
Fix incorrect truncation at the overflow boundary

Summary:
This patch fixes incorrect truncation when the input wider value is
exactly 2^dstBits.  For that value, the overflow to infinity is not
correctly handled.  The fix is to replace a strict '>' with '>='.

Currently,
__truncdfsf2(340282366900000000000000000000000000000.0) returns infinity
__truncdfsf2(340282366920938463463374607431768211456.0) returns 0
__truncdfsf2(400000000000000000000000000000000000000.0) returns infinity

Likewise, __truncdfhf2 and __truncsfhf2 (and consequently gnu_f2h_ieee)
are discontinuous at 65536.0.

This patch adds tests for all three cases, along with adding a missing
header include to fp_test.h.

Reviewers: joerg, ab, srhines

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D10594

Added:
    compiler-rt/trunk/test/builtins/Unit/truncdfsf2_test.c
Modified:
    compiler-rt/trunk/lib/builtins/fp_trunc_impl.inc
    compiler-rt/trunk/test/builtins/Unit/fp_test.h
    compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c
    compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c

Modified: compiler-rt/trunk/lib/builtins/fp_trunc_impl.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/builtins/fp_trunc_impl.inc?rev=240450&r1=240449&r2=240450&view=diff
==============================================================================
--- compiler-rt/trunk/lib/builtins/fp_trunc_impl.inc (original)
+++ compiler-rt/trunk/lib/builtins/fp_trunc_impl.inc Tue Jun 23 15:24:53 2015
@@ -99,7 +99,7 @@ static inline dst_t __truncXfYf2__(src_t
         absResult |= dstQNaN;
         absResult |= ((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode;
     }
-    else if (aAbs > overflow) {
+    else if (aAbs >= overflow) {
         // a overflows to infinity.
         absResult = (dst_rep_t)dstInfExp << dstSigBits;
     }

Modified: compiler-rt/trunk/test/builtins/Unit/fp_test.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/builtins/Unit/fp_test.h?rev=240450&r1=240449&r2=240450&view=diff
==============================================================================
--- compiler-rt/trunk/test/builtins/Unit/fp_test.h (original)
+++ compiler-rt/trunk/test/builtins/Unit/fp_test.h Tue Jun 23 15:24:53 2015
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <string.h>
+#include <stdint.h>
 
 enum EXPECTED_RESULT {
     LESS_0, LESS_EQUAL_0, EQUAL_0, GREATER_0, GREATER_EQUAL_0, NEQUAL_0

Modified: compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c?rev=240450&r1=240449&r2=240450&view=diff
==============================================================================
--- compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c (original)
+++ compiler-rt/trunk/test/builtins/Unit/truncdfhf2_test.c Tue Jun 23 15:24:53 2015
@@ -107,5 +107,8 @@ int main()
     if (test__truncdfhf2(-65520.0,
                          UINT16_C(0xfc00)))
         return 1;
+    if (test__truncdfhf2(65536.0,
+                         UINT16_C(0x7c00)))
+        return 1;
     return 0;
 }

Added: compiler-rt/trunk/test/builtins/Unit/truncdfsf2_test.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/builtins/Unit/truncdfsf2_test.c?rev=240450&view=auto
==============================================================================
--- compiler-rt/trunk/test/builtins/Unit/truncdfsf2_test.c (added)
+++ compiler-rt/trunk/test/builtins/Unit/truncdfsf2_test.c Tue Jun 23 15:24:53 2015
@@ -0,0 +1,38 @@
+//===--------------- truncdfsf2_test.c - Test __truncdfsf2 ----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file tests __truncdfsf2 for the compiler_rt library.
+//
+//===----------------------------------------------------------------------===//
+
+#include <stdio.h>
+
+#include "fp_test.h"
+
+float __truncdfsf2(double a);
+
+int test__truncdfsf2(double a)
+{
+    float actual = __truncdfsf2(a);
+    float expected = a;
+
+    if (actual != expected) {
+        printf("error in test__truncdfsf2(%lf) = %f, "
+               "expected %f\n", a, actual, expected);
+        return 1;
+    }
+    return 0;
+}
+
+int main()
+{
+    if (test__truncdfsf2(340282366920938463463374607431768211456.0))
+        return 1;
+    return 0;
+}

Modified: compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c?rev=240450&r1=240449&r2=240450&view=diff
==============================================================================
--- compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c (original)
+++ compiler-rt/trunk/test/builtins/Unit/truncsfhf2_test.c Tue Jun 23 15:24:53 2015
@@ -104,6 +104,9 @@ int main()
     if (test__truncsfhf2(65520.0f,
                          UINT16_C(0x7c00)))
         return 1;
+    if (test__truncsfhf2(65536.0f,
+                         UINT16_C(0x7c00)))
+        return 1;
     if (test__truncsfhf2(-65520.0f,
                          UINT16_C(0xfc00)))
         return 1;





More information about the llvm-commits mailing list