Skip to content

Commit 4615ed5

Browse files
committed
float literals now parse using musl's 128 bit float code
fixes float literals not having 128 bit precision
1 parent 127bb12 commit 4615ed5

File tree

9 files changed

+1090
-25
lines changed

9 files changed

+1090
-25
lines changed

CMakeLists.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ set(EMBEDDED_SOFTFLOAT_SOURCES
313313
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f32_to_f128M.c"
314314
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f128M.c"
315315
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/f64_to_f16.c"
316+
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/i32_to_f128M.c"
316317
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_add256M.c"
317318
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addCarryM.c"
318319
"${CMAKE_SOURCE_DIR}/deps/SoftFloat-3e/source/s_addComplCarryM.c"
@@ -427,11 +428,12 @@ set(ZIG_SOURCES
427428
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
428429
"${CMAKE_SOURCE_DIR}/src/target.cpp"
429430
"${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
430-
"${CMAKE_SOURCE_DIR}/src/util.cpp"
431431
"${CMAKE_SOURCE_DIR}/src/translate_c.cpp"
432+
"${CMAKE_SOURCE_DIR}/src/util.cpp"
432433
)
433-
set(BLAKE_SOURCES
434+
set(OPTIMIZED_C_SOURCES
434435
"${CMAKE_SOURCE_DIR}/src/blake2b.c"
436+
"${CMAKE_SOURCE_DIR}/src/parse_f128.c"
435437
)
436438
set(ZIG_CPP_SOURCES
437439
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
@@ -6600,7 +6602,7 @@ else()
66006602
endif()
66016603
endif()
66026604

6603-
set(BLAKE_CFLAGS "-std=c99")
6605+
set(OPTIMIZED_C_FLAGS "-std=c99 -O3")
66046606

66056607
set(EXE_LDFLAGS " ")
66066608
if(MINGW)
@@ -6626,9 +6628,9 @@ set_target_properties(zig_cpp PROPERTIES
66266628
COMPILE_FLAGS ${EXE_CFLAGS}
66276629
)
66286630

6629-
add_library(embedded_blake STATIC ${BLAKE_SOURCES})
6630-
set_target_properties(embedded_blake PROPERTIES
6631-
COMPILE_FLAGS "${BLAKE_CFLAGS} -O3"
6631+
add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})
6632+
set_target_properties(opt_c_util PROPERTIES
6633+
COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"
66326634
)
66336635

66346636
add_executable(zig ${ZIG_SOURCES})
@@ -6639,7 +6641,7 @@ set_target_properties(zig PROPERTIES
66396641

66406642
target_link_libraries(zig LINK_PUBLIC
66416643
zig_cpp
6642-
embedded_blake
6644+
opt_c_util
66436645
${SOFTFLOAT_LIBRARIES}
66446646
${CLANG_LIBRARIES}
66456647
${LLD_LIBRARIES}

src/bigfloat.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "bigint.hpp"
1010
#include "buffer.hpp"
1111
#include "softfloat.hpp"
12+
#include "parse_f128.h"
1213
#include <stdio.h>
1314
#include <math.h>
1415
#include <errno.h>
@@ -65,22 +66,18 @@ void bigfloat_init_bigint(BigFloat *dest, const BigInt *op) {
6566
}
6667
}
6768

68-
int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len) {
69+
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len) {
6970
char *str_begin = (char *)buf_ptr;
7071
char *str_end;
7172

7273
errno = 0;
73-
double value = strtod(str_begin, &str_end); // TODO actual f128 parsing
74+
dest->value = parse_f128(str_begin, &str_end);
7475
if (errno) {
7576
return ErrorOverflow;
7677
}
7778

78-
float64_t value_f64;
79-
memcpy(&value_f64, &value, sizeof(double));
80-
f64_to_f128M(value_f64, &dest->value);
81-
8279
assert(str_end <= ((char*)buf_ptr) + buf_len);
83-
return 0;
80+
return ErrorNone;
8481
}
8582

8683
void bigfloat_add(BigFloat *dest, const BigFloat *op1, const BigFloat *op2) {

src/bigfloat.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void bigfloat_init_64(BigFloat *dest, double x);
2828
void bigfloat_init_128(BigFloat *dest, float128_t x);
2929
void bigfloat_init_bigfloat(BigFloat *dest, const BigFloat *x);
3030
void bigfloat_init_bigint(BigFloat *dest, const BigInt *op);
31-
int bigfloat_init_buf_base10(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len);
31+
Error bigfloat_init_buf(BigFloat *dest, const uint8_t *buf_ptr, size_t buf_len);
3232

3333
float16_t bigfloat_to_f16(const BigFloat *bigfloat);
3434
float bigfloat_to_f32(const BigFloat *bigfloat);

0 commit comments

Comments
 (0)