Commit b73d4f74c2

Andrew Kelley <superjoe30@gmail.com>
2017-08-18 19:13:03
depend on libquadmath
it seems to be shipped with gcc and clang
1 parent e1c2256
Changed files (2)
src/bigfloat.cpp
@@ -5,16 +5,12 @@
  * See http://opensource.org/licenses/MIT
  */
 
-// TODO in this file we cast between __float128 and double
-// and lose precision. for now this is going to be a bug in
-// the compiler because I don't want to add a dependency on libquadmath.
-// when we self host we can use zig's f128 and the problem is fixed.
-
 #include "bigfloat.hpp"
 #include "bigint.hpp"
 #include "buffer.hpp"
 #include <math.h>
 #include <errno.h>
+#include <quadmath.h>
 
 void bigfloat_init_float(BigFloat *dest, __float128 x) {
     dest->value = x;
@@ -51,7 +47,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_
     char *str_begin = (char *)buf_ptr;
     char *str_end;
     errno = 0;
-    dest->value = (__float128)strtod(str_begin, &str_end);
+    dest->value = strtoflt128(str_begin, &str_end);
     if (errno) {
         return ErrorOverflow;
     }
@@ -82,26 +78,29 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
 void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
     dest->value = op1->value / op2->value;
     if (dest->value >= 0.0) {
-        dest->value = (__float128)floor((double)dest->value);
+        dest->value = floorq(dest->value);
     } else {
-        dest->value = (__float128)ceil((double)dest->value);
+        dest->value = ceilq(dest->value);
     }
 }
 
 void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
-    dest->value = (__float128)floor((double)(op1->value / op2->value));
+    dest->value = floorq(op1->value / op2->value);
 }
 
 void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
-    dest->value = (__float128)fmod((double)op1->value, (double)op2->value);
+    dest->value = fmodq(op1->value, op2->value);
 }
 
 void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
-    dest->value = (__float128)fmod(fmod((double)op1->value, (double)op2->value) + (double)op2->value, (double)op2->value);
+    dest->value = fmodq(fmodq(op1->value, op2->value) + op2->value, op2->value);
 }
 
 void bigfloat_write_buf(Buf *buf, const BigFloat *op) {
-    buf_appendf(buf, "%f", (double)op->value);
+    buf_resize(buf, 256);
+    int len = quadmath_snprintf(buf_ptr(buf), buf_len(buf), "%Qf", op->value);
+    assert(len > 0);
+    buf_resize(buf, len);
 }
 
 Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2) {
@@ -164,5 +163,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {
 }
 
 bool bigfloat_has_fraction(const BigFloat *bigfloat) {
-    return ((__float128)floor((double)bigfloat->value)) != bigfloat->value;
+    return floorq(bigfloat->value) != bigfloat->value;
 }
CMakeLists.txt
@@ -123,6 +123,7 @@ target_link_libraries(zig LINK_PUBLIC
     ${LLD_LIBRARIES}
     ${LLVM_LIBRARIES}
     ${CMAKE_THREAD_LIBS_INIT}
+    quadmath
 )
 if(MINGW)
     target_link_libraries(zig LINK_PUBLIC version)