authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 13:13:03-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-18 13:13:03-04:00
logb73d4f74c2826b8a87215eaaa1f4e4bfa88be81b
tree95235e815dc239a8c61efbde3b83eff7cbef6942
parente1c225694df6b99b7541a99a59def52a5886c665

depend on libquadmath

it seems to be shipped with gcc and clang

2 files changed, 13 insertions(+), 13 deletions(-)

CMakeLists.txt+1
...@@ -123,6 +123,7 @@ target_link_libraries(zig LINK_PUBLIC...@@ -123,6 +123,7 @@ target_link_libraries(zig LINK_PUBLIC
123 ${LLD_LIBRARIES}123 ${LLD_LIBRARIES}
124 ${LLVM_LIBRARIES}124 ${LLVM_LIBRARIES}
125 ${CMAKE_THREAD_LIBS_INIT}125 ${CMAKE_THREAD_LIBS_INIT}
126 quadmath
126)127)
127if(MINGW)128if(MINGW)
128 target_link_libraries(zig LINK_PUBLIC version)129 target_link_libraries(zig LINK_PUBLIC version)
src/bigfloat.cpp+12-13
...@@ -5,16 +5,12 @@...@@ -5,16 +5,12 @@
5 * See http://opensource.org/licenses/MIT5 * See http://opensource.org/licenses/MIT
6 */6 */
77
8// TODO in this file we cast between __float128 and double
9// and lose precision. for now this is going to be a bug in
10// the compiler because I don't want to add a dependency on libquadmath.
11// when we self host we can use zig's f128 and the problem is fixed.
12
13#include "bigfloat.hpp"8#include "bigfloat.hpp"
14#include "bigint.hpp"9#include "bigint.hpp"
15#include "buffer.hpp"10#include "buffer.hpp"
16#include <math.h>11#include <math.h>
17#include <errno.h>12#include <errno.h>
13#include <quadmath.h>
1814
19void bigfloat_init_float(BigFloat *dest, __float128 x) {15void bigfloat_init_float(BigFloat *dest, __float128 x) {
20 dest->value = x;16 dest->value = x;
...@@ -51,7 +47,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_...@@ -51,7 +47,7 @@ int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_
51 char *str_begin = (char *)buf_ptr;47 char *str_begin = (char *)buf_ptr;
52 char *str_end;48 char *str_end;
53 errno = 0;49 errno = 0;
54 dest->value = (__float128)strtod(str_begin, &str_end);50 dest->value = strtoflt128(str_begin, &str_end);
55 if (errno) {51 if (errno) {
56 return ErrorOverflow;52 return ErrorOverflow;
57 }53 }
...@@ -82,26 +78,29 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {...@@ -82,26 +78,29 @@ void bigfloat_div(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
82void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {78void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
83 dest->value = op1->value / op2->value;79 dest->value = op1->value / op2->value;
84 if (dest->value >= 0.0) {80 if (dest->value >= 0.0) {
85 dest->value = (__float128)floor((double)dest->value);81 dest->value = floorq(dest->value);
86 } else {82 } else {
87 dest->value = (__float128)ceil((double)dest->value);83 dest->value = ceilq(dest->value);
88 }84 }
89}85}
9086
91void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {87void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
92 dest->value = (__float128)floor((double)(op1->value / op2->value));88 dest->value = floorq(op1->value / op2->value);
93}89}
9490
95void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {91void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
96 dest->value = (__float128)fmod((double)op1->value, (double)op2->value);92 dest->value = fmodq(op1->value, op2->value);
97}93}
9894
99void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {95void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {
100 dest->value = (__float128)fmod(fmod((double)op1->value, (double)op2->value) + (double)op2->value, (double)op2->value);96 dest->value = fmodq(fmodq(op1->value, op2->value) + op2->value, op2->value);
101}97}
10298
103void bigfloat_write_buf(Buf *buf, const BigFloat *op) {99void bigfloat_write_buf(Buf *buf, const BigFloat *op) {
104 buf_appendf(buf, "%f", (double)op->value);100 buf_resize(buf, 256);
101 int len = quadmath_snprintf(buf_ptr(buf), buf_len(buf), "%Qf", op->value);
102 assert(len > 0);
103 buf_resize(buf, len);
105}104}
106105
107Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2) {106Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2) {
...@@ -164,5 +163,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {...@@ -164,5 +163,5 @@ Cmp bigfloat_cmp_zero(const BigFloat *bigfloat) {
164}163}
165164
166bool bigfloat_has_fraction(const BigFloat *bigfloat) {165bool bigfloat_has_fraction(const BigFloat *bigfloat) {
167 return ((__float128)floor((double)bigfloat->value)) != bigfloat->value;166 return floorq(bigfloat->value) != bigfloat->value;
168}167}