diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2025-02-10 20:20:26 +0100 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2025-02-15 18:38:28 +0100 |
| commit | dd3d8f9d768479df36e51d402adf55afad1aff07 (patch) | |
| tree | 21b11a361ca080a2d130f33fe435b4ac284731be /src | |
| parent | 831dc694c404826e9a1bf07788e10b9ac3d9cb2d (diff) | |
update
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.zig | 73 | ||||
| -rw-r--r-- | src/root.zig | 191 | ||||
| -rw-r--r-- | src/test.c | 119 |
3 files changed, 383 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..d40f09a --- /dev/null +++ b/src/main.zig @@ -0,0 +1,73 @@ +//! By convention, main.zig is where your main function lives in the case that +//! you are building an executable. If you are making a library, the convention +//! is to delete this file and start with root.zig instead. + +const std = @import("std"); + +/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details. +const zisp = @import("zisp_lib"); + +pub fn main() !void { + const T = extern union { + bits: u64, + double: f64, + }; + var f1: *volatile f64 = undefined; + var f2: *volatile f64 = undefined; + var x: *volatile T = undefined; + + var _gpa: std.heap.GeneralPurposeAllocator(.{}) = .init; + const gpa = _gpa.allocator(); + + f1 = try gpa.create(f64); + f2 = try gpa.create(f64); + x = try gpa.create(T); + + f1.* = 0.0; + f2.* = 0.0; + x.*.double = f1.* / f2.*; + std.debug.print(" 0/0: {x}\n", .{x.*.bits}); + + f1.* = -0.0; + f2.* = 0.0; + x.*.double = f1.* / f2.*; + std.debug.print("-0/0: {x}\n", .{x.*.bits}); + + // // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) + // std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + + // // stdout is for the actual output of your application, for example if you + // // are implementing gzip, then only the compressed bytes should be sent to + // // stdout, not any debugging messages. + // const stdout_file = std.io.getStdOut().writer(); + // var bw = std.io.bufferedWriter(stdout_file); + // const stdout = bw.writer(); + + // try stdout.print("Run `zig build test` to run the tests.\n", .{}); + + // try bw.flush(); // Don't forget to flush! +} + +test "simple test" { + var list = std.ArrayList(i32).init(std.testing.allocator); + defer list.deinit(); // Try commenting this out and see if zig detects the memory leak! + try list.append(42); + try std.testing.expectEqual(@as(i32, 42), list.pop()); +} + +test "use other module" { + //try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50)); +} + +test "fuzz example" { + const Context = struct { + fn testOne(context: @This(), input: []const u8) anyerror!void { + _ = context; + // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! + try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); + } + }; + try std.testing.fuzz(Context{}, Context.testOne, .{}); +} + +test "nan" {} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..05ad381 --- /dev/null +++ b/src/root.zig @@ -0,0 +1,191 @@ +//! By convention, root.zig is the root source file when making a library. If +//! you are making an executable, the convention is to delete this file and +//! start with main.zig instead. +const std = @import("std"); +const builtin = @import("builtin"); +const testing = std.testing; + +// Read the following article to understand the NaN-packing strategy: +// +// https://tkammer.de/zisp/notes/nan.html +// +// Note: Packed structs are least-to-most significant, so the order of fields +// must be reversed relative to a typical big-endian illustration of the bit +// patterns of IEEE 754 double-precision floating point numbers. + +const Value = packed union { + double: f64, + nan: packed struct { + rest: u51, + quiet: u1, + exp: u11, + sign: u1, + }, + int: packed struct { + code: u51, + neg: bool, + exp: u11, + is_int: bool, + }, + pointer: packed struct { + value: u48, + type: u3, + _zo: u1, + _qnan: u12, + }, +}; + +// Helpers + +inline fn zisp_dump(v: Value) void { + std.debug.dumpHex(std.mem.asBytes(&v)); +} + +///! Checks for any IEEE 754 NaN. +inline fn zisp_is_nan(v: Value) bool { + return v.nan.exp == std.math.maxInt(u11); +} + +///! Checks for a Zisp value packed into a NaN. +inline fn zisp_is_packed(v: Value) bool { + return zisp_is_nan(v) and v.nan.rest != 0; +} + +///! Checks for a regular double including infinity or canonical NaN +inline fn zisp_is_double(v: Value) bool { + return !zisp_is_packed(v); +} + +inline fn zisp_assert_double(v: Value) void { + if (!zisp_is_double(v)) { + zisp_dump(v); + @panic("not double"); + } +} + +inline fn zisp_is_int(v: Value) bool { + return zisp_is_packed(v) and v.int.is_int; +} + +inline fn zisp_assert_int(v: Value) void { + if (!zisp_is_int(v)) { + zisp_dump(v); + @panic("not int"); + } +} + +// See detailed NaN packing docs for why the +/- 1. +const zisp_int_min = std.math.minInt(i52) + 1; +const zisp_int_max = std.math.maxInt(i52) - 1; + +inline fn zisp_assert_int_range(int: i64) void { + if (int < zisp_int_min) { + std.debug.print("int to pack is too small: {}", .{int}); + @panic("int to pack is too small"); + } + if (int > zisp_int_max) { + std.debug.print("int to pack is too large: {}", .{int}); + @panic("int to pack is too large"); + } +} + +inline fn zisp_int_pack_neg(int: i64) Value { + return @bitCast(int); +} + +inline fn zisp_int_unpack_neg(v: Value) i64 { + return @bitCast(v); +} + +const zisp_int_pos_mask: u64 = 0xfff7ffffffffffff; + +inline fn zisp_int_pack_pos(int: i64) Value { + const uint: u64 = @bitCast(int); + return @bitCast(uint ^ zisp_int_pos_mask); +} + +inline fn zisp_int_unpack_pos(v: Value) i64 { + const uint: u64 = @bitCast(v); + return @bitCast(uint ^ zisp_int_pos_mask); +} + +inline fn zisp_int_pack(int: i64) Value { + zisp_assert_int_range(int); + if (int < 0) { + return zisp_int_pack_neg(int); + } else { + return zisp_int_pack_pos(int); + } +} + +inline fn zisp_int_unpack(v: Value) i64 { + zisp_assert_int(v); + if (v.int.neg) { + return zisp_int_unpack_neg(v); + } else { + return zisp_int_unpack_pos(v); + } +} + +// Doubles + +pub fn zisp_double(d: f64) Value { + return @bitCast(d); +} + +// pub fn zisp_double_p(v: Value) Value { +// return zisp_bool(zisp_is_double(v)); +// } + +pub fn zisp_double_get(v: Value) f64 { + zisp_assert_double(v); + return v.double; +} + +pub fn zisp_double_add(v1: Value, v2: Value) Value { + const d1 = zisp_double_get(v1); + const d2 = zisp_double_get(v2); + return zisp_double(d1 + d2); +} + +// Ints + +pub fn zisp_int(int: i64) Value { + return zisp_int_pack(int); +} + +// pub fn zisp_int_p(v: Value) Value { +// return zisp_bool(zisp_is_int(v)); +// } + +pub fn zisp_int_get(v: Value) i64 { + return zisp_int_unpack(v); +} + +pub fn zisp_int_add(v1: Value, v2: Value) Value { + const int1 = zisp_int_get(v1); + const int2 = zisp_int_get(v2); + return zisp_int(int1 + int2); +} + +// Tests + +test "double add functionality" { + const d1: f64 = 0.123456789; + const d2: f64 = -0.987654321; + const v1 = zisp_double(d1); + const v2 = zisp_double(d2); + const v3 = zisp_double_add(v1, v2); + const result = zisp_double_get(v3); + try std.testing.expect(result == d1 + d2); +} + +test "int add functionality" { + const int1: i64 = 123456789; + const int2: i64 = -987654321; + const v1 = zisp_int(int1); + const v2 = zisp_int(int2); + const v3 = zisp_int_add(v1, v2); + const result = zisp_int_get(v3); + try std.testing.expect(result == int1 + int2); +} diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..0b0917e --- /dev/null +++ b/src/test.c @@ -0,0 +1,119 @@ +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <math.h> + +union test { + double d; + uint64_t u; +}; + +int main(int argc, char** argv) { + + volatile uint64_t mask; + volatile uint64_t min; + volatile uint64_t max; + + volatile double d1; + volatile double d2; + + volatile uint64_t pd1; + volatile uint64_t pd2; + + + // 0 .. 2^51 + + // SE__________QP__ + mask = 0b1111111111110111111111111111111111111111111111111111111111111111; + min = 0b1111111111110111111111111111111111111111111111111111111111111111; + max = 0b1111111111110000000000000000000000000000000000000000000000000001; + + memcpy(&d1, &min, 8); + memcpy(&d2, &max, 8); + + printf("%lf\n", d1); + printf("%lf\n", d2); + + memcpy(&pd1, &d1, 8); + memcpy(&pd2, &d2, 8); + pd1 ^= mask; + pd2 ^= mask; + + printf("%ld\n", pd1); + printf("%ld\n", pd2); + + printf("\n"); + + // -2^51 + 1 .. -1 + + // SE__________QP__ + min = 0b1111111111111000000000000000000000000000000000000000000000000001; + max = 0b1111111111111111111111111111111111111111111111111111111111111111; + + memcpy(&d1, &min, 8); + memcpy(&d2, &max, 8); + + printf("%lf\n", d1); + printf("%lf\n", d2); + + memcpy(&pd1, &d1, 8); + memcpy(&pd2, &d2, 8); + + printf("%ld\n", pd1); + printf("%ld\n", pd2); + + printf("\n"); + + return 0; + + // -2^50 + 1 .. -1 + + // SE__________QFP__ + mask = 0b1111111111111100000000000000000000000000000000000000000000000000; + min = 0b0000000000000000000000000000000000000000000000000000000000000001; + max = 0b0000000000000011111111111111111111111111111111111111111111111111; + + printf("%ld\n", (int64_t) (min | mask)); + printf("%ld\n", (int64_t) (max | mask)); + + + // 0 .. 2^50 + + // SE__________QFP__ + mask = 0b0000000000000011111111111111111111111111111111111111111111111111; + min = 0b0000000000000100000000000000000000000000000000000000000000000000; + max = 0b0000000000000111111111111111111111111111111111111111111111111111; + + printf("%ld\n", (int64_t) (min & mask)); + printf("%ld\n", (int64_t) (max & mask)); + + + + + /* volatile union test x; */ + /* volatile double f1; */ + /* volatile double f2; */ + + /* f1 = 0.0; */ + /* f2 = 0.0; */ + /* x.d = f1 / f2; */ + /* printf(" 0/0: %lx\n", x.u); */ + + /* f1 = -0.0; */ + /* f2 = +0.0; */ + /* x.d = f1 / f2; */ + /* printf("-0/0: %lx\n", x.u); */ + + /* x.d = sqrt(-1); */ + /* printf("sqrt(-1): %lx\n", x.u); */ + + + /* double nan_value = fabs(sqrt(-1.0)); // Standard way to generate NaN */ + + /* uint64_t nan_bits; */ + /* memcpy(&nan_bits, &nan_value, sizeof(nan_bits)); */ + + /* printf("NaN in hex: 0x%016lx\n", nan_bits); */ + + /* return 0; */ +} |
