From 8b57eeb9ea69fdd581f34f77e0a1c1bec425d6d3 Mon Sep 17 00:00:00 2001
From: Leo Tenenbaum
Date: Tue, 7 Jan 2020 22:08:02 -0500
Subject: finished read_f32
---
README.html | 4 ++--
README.md | 2 +-
binfile.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++------------
tests.c | 1 +
4 files changed, 51 insertions(+), 15 deletions(-)
diff --git a/README.html b/README.html
index 0d9d5d9..3b4bc57 100644
--- a/README.html
+++ b/README.html
@@ -44,9 +44,9 @@ it is nearly as fast in theory.
-toc
Source Code
+toc
Compiler Source Code
-The source code to toc
is licensed under the GNU General Public License, version 3. See LICENSE
for more information.
+Most of the source code for the toc
compiler is licensed under the GNU General Public License, version 3. See LICENSE
for more information.
toc
is written in C, for speed and portability. It has no dependencies, other than the C runtime library.
diff --git a/README.md b/README.md
index 64d0569..9366559 100644
--- a/README.md
+++ b/README.md
@@ -43,7 +43,7 @@ On other systems, you can just compile main.c with a C compiler. `toc` uses seve
### `toc` Compiler Source Code
-The source code for the `toc` compiler is licensed under the GNU General Public License, version 3. See `LICENSE` for more information.
+Most of the source code for the `toc` compiler is licensed under the GNU General Public License, version 3. See `LICENSE` for more information.
`toc` is written in C, for speed and portability. It has no dependencies, other than the C runtime library.
diff --git a/binfile.c b/binfile.c
index b05878a..6aaf007 100644
--- a/binfile.c
+++ b/binfile.c
@@ -2,20 +2,23 @@
#ifdef TOC_DEBUG
#define BINFILE_PRINT
+static bool binfile_printing_enabled = true;
#endif
static inline void write_u8(FILE *fp, U8 u8) {
putc(u8, fp);
#ifdef BINFILE_PRINT
- static int col = 0;
- printf("%02x ", u8);
- ++col;
- if (col == 8) printf(" ");
- if (col == 16) {
- col = 0;
- printf("\n");
+ if (binfile_printing_enabled) {
+ static int col = 0;
+ printf("%02x ", u8);
+ ++col;
+ if (col == 8) printf(" ");
+ if (col == 16) {
+ col = 0;
+ printf("\n");
+ }
+ fflush(stdout);
}
- fflush(stdout);
#endif
}
@@ -171,10 +174,20 @@ static F32 read_f32(FILE *fp) {
#ifdef TOC_PORTABLE
/* TODO: infinity, NaN */
U32 u32 = read_u32(fp);
- U32 sign = (u32 & 0x8000000);
- U32 exponent = (u32 & 0x7f80000) >> 23;
- U32 fraction = (u32 & 0x007ffff);
- /* TODO: finish me */
+ U32 sign = (u32 & 0x80000000);
+ U32 exponent = (u32 & 0x7f800000) >> 23;
+ U32 fraction = (u32 & 0x007fffff);
+ F32 flt = (float)fraction;
+ I32 signed_exponent = (I32)exponent - 127;
+ while (signed_exponent < 0) {
+ ++signed_exponent;
+ flt /= (F32)2;
+ }
+ while (signed_exponent > 0) {
+ --signed_exponent;
+ flt *= (F32)2;
+ }
+ if (sign) flt = -flt;
#else
F32 f32;
fread(&f32, sizeof f32, 1, fp);
@@ -231,3 +244,25 @@ static void write_vlq(FILE *fp, U64 x) {
}
write_u8(fp, (U8)x);
}
+
+#ifdef TOC_DEBUG
+static void binfile_test(void) {
+ binfile_printing_enabled = false;
+ FILE *fp = tmpfile();
+ /* U64 a = 12387217312; */
+ /* write_vlq(fp, a); */
+ I64 b = -123981232131;
+ write_i64(fp, b);
+ U8 c = 12;
+ write_u8(fp, c);
+ float d = -2.323198123f;
+ write_f32(fp, d);
+ fseek(fp, 0L, SEEK_SET);
+ /* assert(read_vlq(fp) == a); */
+ assert(read_i64(fp) == b);
+ assert(read_u8(fp) == c);
+ assert(read_f32(fp) == d);
+ fclose(fp);
+ binfile_printing_enabled = true;
+}
+#endif
diff --git a/tests.c b/tests.c
index 97e021b..9d82897 100644
--- a/tests.c
+++ b/tests.c
@@ -32,4 +32,5 @@ static void test_all(void) {
arr_test();
block_arr_test();
idents_test();
+ binfile_test();
}
--
cgit v1.2.3