aboutsummaryrefslogtreecommitdiff
path: root/src/exact_value.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2018-02-25 15:09:16 +0000
committergingerBill <bill@gingerbill.org>2018-02-25 15:09:16 +0000
commitd247ba4751d8189082849e114e3d4a6106b0d053 (patch)
tree56e023cb3006392918e2074b1d1489b06f59f4dd /src/exact_value.cpp
parent27b7dc336ab7c108d711d6ce00686467f1f0319c (diff)
Hexadecimal floats for "perfect values" 0h42f60000 == 123; use `bit_cast` in compiler
Diffstat (limited to 'src/exact_value.cpp')
-rw-r--r--src/exact_value.cpp57
1 files changed, 23 insertions, 34 deletions
diff --git a/src/exact_value.cpp b/src/exact_value.cpp
index c9ae402e0..2c6527f3f 100644
--- a/src/exact_value.cpp
+++ b/src/exact_value.cpp
@@ -156,39 +156,6 @@ f64 float_from_string(String string) {
i++;
}
-#if 0
- if (len-i > 2 &&
- str[i] == '0' &&
- str[i+1] == 'h') {
- i += 2;
- u8 *text = string.text;
- isize len = string.len;
- if (has_prefix) {
- text += 2;
- len -= 2;
- }
-
- u64 base = 16;
-
- u64 result = {0};
- for (isize i = 0; i < len; i++) {
- Rune r = cast(Rune)text[i];
- if (r == '_') {
- continue;
- }
- u64 v = bit128__digit_value(r);
- if (v >= base) {
- break;
- }
- result *= base;
- result += v;
- }
-
-
- return *cast(f64 *)&result;
- }
-#endif
-
f64 value = 0.0;
for (; i < len; i++) {
Rune r = cast(Rune)str[i];
@@ -255,7 +222,29 @@ f64 float_from_string(String string) {
}
ExactValue exact_value_float_from_string(String string) {
- return exact_value_float(float_from_string(string));
+ if (string.len > 2 && string[0] == '0' && string[1] == 'h') {
+
+ isize digit_count = 0;
+ for (isize i = 2; i < string.len; i++) {
+ if (string[i] != '_') {
+ digit_count += 1;
+ }
+ }
+ u64 u = u64_from_string(string);
+ if (digit_count == 8) {
+ u32 x = cast(u32)u;
+ f32 f = bit_cast<f32>(x);
+ return exact_value_float(cast(f64)f);
+ } else if (digit_count == 16) {
+ f64 f = bit_cast<f64>(u);
+ return exact_value_float(f);
+ } else {
+ GB_PANIC("Invalid hexadecimal float, expected 8 or 16 digits, got %td", digit_count);
+ }
+ }
+
+ f64 f = float_from_string(string);
+ return exact_value_float(f);
}