# [PATCH] [COMPILER-RT] Implement __addtf3 and __subtf3

Dmitri Gribenko gribozavr at gmail.com
Tue Mar 4 02:44:24 PST 2014

```
================
Comment at: test/builtins/Unit/subtf3_test.c:21-32
@@ +20,14 @@
+
+static inline long double fromRep(uint64_t hi, uint64_t lo)
+{
+    __uint128_t x = ((__uint128_t)hi << 64) + lo;
+    const union {long double f; __uint128_t i; } rep = {.i = x};
+    return rep.f;
+}
+
+static inline __uint128_t toRep(long double x)
+{
+    const union {long double f; __uint128_t i;} rep = {.f = x};
+    return rep.i;
+}
+
----------------
Even though there is no intention of breaking this union trick in Clang, it is undefined behavior.  The correct way to do this is to use memcpy() to copy from long double* to __uint128_t*.

================
Comment at: test/builtins/Unit/addtf3_test.c:21-57
@@ +20,39 @@
+
+static inline long double fromRep(uint64_t hi, uint64_t lo)
+{
+    __uint128_t x = ((__uint128_t)hi << 64) + lo;
+    const union {long double f; __uint128_t i; } rep = {.i = x};
+    return rep.f;
+}
+
+static inline __uint128_t toRep(long double x)
+{
+    const union {long double f; __uint128_t i;} rep = {.f = x};
+    return rep.i;
+}
+
+// return 0 if equal
+// use two 64-bit integers intead of one 128-bit integer
+// because 128-bit integer constant can't be assigned directly
+static inline int compareResult(long double result,
+                                uint64_t expectedHi,
+                                uint64_t expectedLo)
+{
+    __uint128_t rep = toRep(result);
+    uint64_t hi = rep >> 64;
+    uint64_t lo = rep;
+
+    if (hi == expectedHi && lo == expectedLo){
+        return 0;
+    }
+    // test other posible NaN representation(signal NaN)
+    else if (expectedHi == 0x7fff800000000000UL && expectedLo == 0x0UL){
+        if ((hi & 0x7fff000000000000UL) == 0x7fff000000000000UL &&
+            ((hi & 0xffffffffffffUL) > 0 || lo > 0)){
+            return 0;
+        }
+    }
+    return 1;
+}
+
+// Returns: a + b
----------------
Please don't duplicate these helpers, factor them out to a shared header.

================
Comment at: test/builtins/Unit/addtf3_test.c:83
@@ +82,3 @@
+    // qNaN + any = qNaN
+    if (test__addtf3(fromRep(0x7fff800000000000UL, 0x0),
+                     0x1.23456789abcdefp+5L,
----------------
It would be easier to read these tests if in addition to fromRep() there were makePlusInf(), makeQNaN() etc.

http://llvm-reviews.chandlerc.com/D2798

```

More information about the llvm-commits mailing list