diff options
-rw-r--r-- | binfile.c | 38 | ||||
-rw-r--r-- | misc.c | 54 | ||||
-rw-r--r-- | point.toc | 5 | ||||
-rw-r--r-- | rand.c | 9 | ||||
-rw-r--r-- | str.c | 28 | ||||
-rw-r--r-- | toc.c | 3 |
6 files changed, 72 insertions, 65 deletions
@@ -139,8 +139,8 @@ static void write_f32(FILE *fp, F32 f32) { /* TODO: infinity, NaN */ U32 fraction = 0; U32 fraction_bit = ((U32)1) << 22; - unsigned exponent = 127; - unsigned sign = f32 < 0; + U32 exponent = 127; + U32 sign = f32 < 0; if (sign) f32 = -f32; while (f32 < (F32)1) { f32 *= (F32)2; @@ -161,34 +161,33 @@ static void write_f32(FILE *fp, F32 f32) { f32 *= (F32)2; fraction_bit >>= 1; } - write_u8(fp, fraction & 0xFF); - write_u8(fp, (fraction & 0xFF00) >> 8); - unsigned byte3 = (fraction & 0x7F0000) >> 16; - byte3 |= (exponent & 1) << 7; - write_u8(fp, (U8)byte3); - unsigned byte4 = exponent >> 1; - byte4 |= (sign << 7); - write_u8(fp, (U8)byte4); + write_u32(fp, fraction | (exponent << 23) | (sign << 31)); #else fwrite(&f32, sizeof f32, 1, fp); #endif } 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 */ +#else + F32 f32; + fread(&f32, sizeof f32, 1, fp); + return f32; +#endif } static void write_f64(FILE *fp, F64 f64) { #if BINFILE_PORTABLE U64 fraction = 0; U64 fraction_bit = ((U64)1) << 51; - unsigned exponent = 1023; - unsigned sign = f64 < 0; + U64 exponent = 1023; + U64 sign = f64 < 0; if (sign) f64 = -f64; while (f64 < (F64)1) { f64 *= (F64)2; @@ -209,18 +208,7 @@ static void write_f64(FILE *fp, F64 f64) { f64 *= (F64)2; fraction_bit >>= 1; } - write_u8(fp, fraction & 0xFF); - write_u8(fp, (fraction & 0xFF00) >> 8); - write_u8(fp, (fraction & 0xFF0000) >> 16); - write_u8(fp, (fraction & 0xFF000000) >> 24); - write_u8(fp, (fraction & 0xFF00000000) >> 32); - write_u8(fp, (fraction & 0xFF0000000000) >> 40); - unsigned byte7 = (fraction & 0xF000000000000) >> 48; - byte7 |= (exponent & 0xF) << 4; - write_u8(fp, (U8)byte7); - unsigned byte8 = (exponent & 0x7F0) >> 4; - byte8 |= (sign << 7); - write_u8(fp, (U8)byte8); + write_u64(fp, fraction | (exponent << 52) | (sign << 63)); #else fwrite(&f64, sizeof f64, 1, fp); #endif @@ -0,0 +1,54 @@ +/* +Miscellaneous C functions which toc uses. + +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +For more information, please refer to <http://unlicense.org/> +*/ + + +/* +A better alternative to strncpy. dest is guaranteed to be a null-terminated string +after this function is run. +Returns the number of characters copied to dest, not including the null character. +destsz must be greater than 0. +*/ +size_t str_copy(char *dest, size_t destsz, const char *src) { + assert(destsz); + if (!*src) { + *dest = 0; + return 0; + } + for (size_t i = 0; i < destsz-1; ++i) { + *dest = *src; + if (!*src) { + *dest = 0; + return i; + } + ++src; ++dest; + } + dest[destsz-1] = 0; + return destsz-1; +} + +static U32 rand_u32(U32 seed) { + U64 seed64 = (U64)seed; + return (U32)((seed64 * 0x832f0fda4e1a8642 + 0x41d49cd5459a2ab4) >> 32); +} @@ -1,9 +1,11 @@ pkg "point"; #export f ::= 3.123343234; -#export g ::= -3.14938244 as f64; +#export g ::= -3.14938244 as f64; +/* + #export Point ::= struct { x, y: int; }; @@ -16,3 +18,4 @@ pkg "point"; mk_point2 ::= fn(x, y:int) p: Point { p = mk_point(x*x, y*y); }; +*/
\ No newline at end of file @@ -1,9 +0,0 @@ -/* - Copyright (C) 2019, 2020 Leo Tenenbaum. - This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. - You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>. -*/ -static U32 rand_u32(U32 seed) { - U64 seed64 = (U64)seed; - return (U32)((seed64 * 0x832f0fda4e1a8642 + 0x41d49cd5459a2ab4) >> 32); -} @@ -1,28 +0,0 @@ -/* - Copyright (C) 2019, 2020 Leo Tenenbaum. - This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever. - You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>. -*/ -/* -A better alternative to strncpy. dest is guaranteed to be a null-terminated string -after this function is run. -Returns the number of characters copied to dest, not including the null character. -destsz must be greater than 0. -*/ -size_t str_copy(char *dest, size_t destsz, const char *src) { - assert(destsz); - if (!*src) { - *dest = 0; - return 0; - } - for (size_t i = 0; i < destsz-1; ++i) { - *dest = *src; - if (!*src) { - *dest = 0; - return i; - } - ++src; ++dest; - } - dest[destsz-1] = 0; - return destsz-1; -} @@ -50,9 +50,8 @@ static void fprint_char_literal(FILE *f, char c) { #include "arr.c" #include "location.c" #include "err.c" -#include "rand.c" +#include "misc.c" #include "blockarr.c" -#include "str.c" #include "instance_table.c" #include "copy.c" #include "binfile.c" |