From 29358b9cd0eea2ddf05d216d75d01dff5f0ea780 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 9 Mar 2025 15:56:44 -0400 Subject: Add some missing modulus and conversion methods for floats/ints --- stdlib/integers.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'stdlib/integers.h') diff --git a/stdlib/integers.h b/stdlib/integers.h index c78cabf0..f70ed29d 100644 --- a/stdlib/integers.h +++ b/stdlib/integers.h @@ -282,7 +282,6 @@ MACROLIKE PUREFUNC Int_t Int$from_num(double n, bool truncate) { fail("Could not convert to an integer without truncation: %g", n); return Int$from_mpz(result); } -#pragma GCC diagnostic pop MACROLIKE PUREFUNC Int_t Int$from_num32(float n, bool truncate) { return Int$from_num((double)n, truncate); } MACROLIKE Int_t Int$from_int64(int64_t i) { if likely (i >= SMALLEST_SMALL_INT && i <= BIGGEST_SMALL_INT) @@ -298,6 +297,18 @@ MACROLIKE CONSTFUNC Int_t Int$from_byte(Byte_t b) { return I_small(b); } MACROLIKE CONSTFUNC Int_t Int$from_bool(Bool_t b) { return I_small(b); } // Int64 constructors +MACROLIKE PUREFUNC Int64_t Int64$from_num(Num_t n, bool truncate) { + int64_t i64 = (int64_t)n; + if unlikely ((Num_t)n != n && !truncate) + fail("Could not convert Num to Int64 without truncation: %g\n", n); + return i64; +} +MACROLIKE PUREFUNC Int64_t Int64$from_num32(Num32_t n, bool truncate) { + int64_t i64 = (int64_t)n; + if unlikely ((Num32_t)n != n && !truncate) + fail("Could not convert Num32 to Int64 without truncation: %g\n", (double)n); + return i64; +} MACROLIKE PUREFUNC Int64_t Int64$from_int(Int_t i, bool truncate) { if likely (i.small & 1) return (int64_t)(i.small >> 2); @@ -310,6 +321,18 @@ MACROLIKE CONSTFUNC Int64_t Int64$from_int16(Int16_t i) { return (Int64_t)i; } MACROLIKE CONSTFUNC Int64_t Int64$from_int8(Int8_t i) { return (Int64_t)i; } // Int32 constructors +MACROLIKE PUREFUNC Int32_t Int32$from_num(Num_t n, bool truncate) { + int32_t i32 = (int32_t)n; + if unlikely ((Num_t)n != n && !truncate) + fail("Could not convert Num to Int32 without truncation: %g\n", n); + return i32; +} +MACROLIKE PUREFUNC Int32_t Int32$from_num32(Num32_t n, bool truncate) { + int32_t i32 = (int32_t)n; + if unlikely ((Num32_t)n != n && !truncate) + fail("Could not convert Num32 to Int32 without truncation: %g\n", (double)n); + return i32; +} MACROLIKE PUREFUNC Int32_t Int32$from_int(Int_t i, bool truncate) { int64_t i64 = Int64$from_int(i, truncate); int32_t i32 = (int32_t)i64; @@ -326,6 +349,18 @@ MACROLIKE CONSTFUNC Int32_t Int32$from_int16(Int16_t i) { return (Int32_t)i; } MACROLIKE CONSTFUNC Int32_t Int32$from_int8(Int8_t i) { return (Int32_t)i; } // Int16 constructors +MACROLIKE PUREFUNC Int16_t Int16$from_num(Num_t n, bool truncate) { + int16_t i16 = (int16_t)n; + if unlikely ((Num_t)n != n && !truncate) + fail("Could not convert Num to Int16 without truncation: %g\n", n); + return i16; +} +MACROLIKE PUREFUNC Int16_t Int16$from_num32(Num32_t n, bool truncate) { + int16_t i16 = (int16_t)n; + if unlikely ((Num32_t)n != n && !truncate) + fail("Could not convert Num32 to Int16 without truncation: %g\n", (double)n); + return i16; +} MACROLIKE PUREFUNC Int16_t Int16$from_int(Int_t i, bool truncate) { int64_t i64 = Int64$from_int(i, truncate); int16_t i16 = (int16_t)i64; @@ -333,7 +368,6 @@ MACROLIKE PUREFUNC Int16_t Int16$from_int(Int_t i, bool truncate) { fail("Integer is too big to fit in a 16-bit integer!"); return i16; } - MACROLIKE PUREFUNC Int16_t Int16$from_int64(Int64_t i, bool truncate) { if (!truncate && unlikely(i != (Int64_t)(Int16_t)i)) fail("Integer is too big to fit in a 16-bit integer: %ld", i); @@ -347,6 +381,18 @@ MACROLIKE PUREFUNC Int16_t Int16$from_int32(Int32_t i, bool truncate) { MACROLIKE CONSTFUNC Int16_t Int16$from_int8(Int8_t i) { return (Int16_t)i; } // Int8 constructors +MACROLIKE PUREFUNC Int8_t Int8$from_num(Num_t n, bool truncate) { + int8_t i8 = (int8_t)n; + if unlikely ((Num_t)n != n && !truncate) + fail("Could not convert Num to Int8 without truncation: %g\n", n); + return i8; +} +MACROLIKE PUREFUNC Int8_t Int8$from_num32(Num32_t n, bool truncate) { + int8_t i8 = (int8_t)n; + if unlikely ((Num32_t)n != n && !truncate) + fail("Could not convert Num32 to Int8 without truncation: %g\n", (double)n); + return i8; +} MACROLIKE PUREFUNC Int8_t Int8$from_int(Int_t i, bool truncate) { int64_t i64 = Int64$from_int(i, truncate); int8_t i8 = (int8_t)i64; @@ -369,5 +415,6 @@ MACROLIKE PUREFUNC Int8_t Int8$from_int16(Int16_t i, bool truncate) { fail("Integer is too big to fit in a 8-bit integer: %ld", i); return (Int8_t)i; } +#pragma GCC diagnostic pop // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3