aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-03-11 18:02:13 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-03-11 18:02:13 -0400
commitde0e565d00e92715d415a9ab09d8f7651bfabc36 (patch)
tree5523d775ff95b9c88ae8d599e82276e4f19d81db
parent1f6e586b2a3fe7f8ca32ce95659032bafef0ad24 (diff)
Fixes for integer truncation
-rw-r--r--examples/game/box.tm2
-rw-r--r--examples/game/player.tm2
-rw-r--r--examples/game/raylib.tm1
-rw-r--r--stdlib/integers.h78
4 files changed, 45 insertions, 38 deletions
diff --git a/examples/game/box.tm b/examples/game/box.tm
index 03294a0a..b95fd895 100644
--- a/examples/game/box.tm
+++ b/examples/game/box.tm
@@ -4,4 +4,4 @@ use ./raylib.tm
struct Box(pos:Vector2, size=Vector2(50, 50), color=Color(0x80,0x80,0x80), blocking=yes):
func draw(b:Box):
- DrawRectangleRec(Rectangle(b.pos.x, b.pos.y, b.size.x, b.size.y), b.color)
+ DrawRectangleV(b.pos, b.size, b.color)
diff --git a/examples/game/player.tm b/examples/game/player.tm
index dac2508a..f73dcf6a 100644
--- a/examples/game/player.tm
+++ b/examples/game/player.tm
@@ -25,4 +25,4 @@ struct Player(pos,prev_pos:Vector2):
p.prev_pos, p.pos = p.pos, p.pos + World.DT*vel
func draw(p:Player):
- DrawRectangleRec(Rectangle(p.pos.x, p.pos.y, Player.SIZE.x, Player.SIZE.y), Player.COLOR)
+ DrawRectangleV(p.pos, Player.SIZE, Player.COLOR)
diff --git a/examples/game/raylib.tm b/examples/game/raylib.tm
index 2721d0e1..faa6f227 100644
--- a/examples/game/raylib.tm
+++ b/examples/game/raylib.tm
@@ -57,6 +57,7 @@ extern CloseWindow:func()
extern ClearBackground:func(color:Color)
extern DrawRectangle:func(x,y,width,height:Int32, color:Color)
extern DrawRectangleRec:func(rec:Rectangle, color:Color)
+extern DrawRectangleV:func(pos:Vector2, size:Vector2, color:Color)
extern DrawText:func(text:CString, x,y:Int32, text_height:Int32, color:Color)
extern GetScreenWidth:func(->Int32)
extern GetScreenHeight:func(->Int32)
diff --git a/stdlib/integers.h b/stdlib/integers.h
index c0601424..3bc3abeb 100644
--- a/stdlib/integers.h
+++ b/stdlib/integers.h
@@ -278,7 +278,7 @@ MACROLIKE PUREFUNC bool Int$is_negative(Int_t x) {
MACROLIKE PUREFUNC Int_t Int$from_num(double n, bool truncate) {
mpz_t result;
mpz_init_set_d(result, n);
- if unlikely (!truncate && mpz_get_d(result) != n)
+ if (!truncate && unlikely(mpz_get_d(result) != n))
fail("Could not convert to an integer without truncation: %g", n);
return Int$from_mpz(result);
}
@@ -299,13 +299,13 @@ 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)
+ if (!truncate && unlikely((Num_t)i64 != n))
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)
+ if (!truncate && unlikely((Num32_t)i64 != n))
fail("Could not convert Num32 to Int64 without truncation: %g\n", (double)n);
return i64;
}
@@ -323,27 +323,28 @@ 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)
+ if (!truncate && unlikely((Num_t)i32 != n))
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)
+ if (!truncate && unlikely((Num32_t)i32 != n))
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;
- if (!truncate && unlikely(i64 != i32))
+ if (!truncate && unlikely((int64_t)i32 != i64))
fail("Integer is too big to fit in a 32-bit integer: %k", (Text_t[1]){Int$value_as_text(i)});
return i32;
}
-MACROLIKE PUREFUNC Int32_t Int32$from_int64(Int64_t i, bool truncate) {
- if (!truncate && unlikely(i != (Int64_t)(Int32_t)i))
- fail("Integer is too big to fit in a 32-bit integer: %ld", i);
- return (Int32_t)i;
+MACROLIKE PUREFUNC Int32_t Int32$from_int64(Int64_t i64, bool truncate) {
+ int32_t i32 = (int32_t)i64;
+ if (!truncate && unlikely((int64_t)i32 != i64))
+ fail("Integer is too big to fit in a 32-bit integer: %ld", i64);
+ return i32;
}
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; }
@@ -351,69 +352,74 @@ 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)
+ if (!truncate && unlikely((Num_t)i16 != n))
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)
+ if (!truncate && unlikely((Num32_t)i16 != n))
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;
- if (!truncate && unlikely(i64 != i16))
+ if (!truncate && unlikely((int64_t)i16 != i64))
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);
- return (Int16_t)i;
+MACROLIKE PUREFUNC Int16_t Int16$from_int64(Int64_t i64, bool truncate) {
+ int16_t i16 = (int16_t)i64;
+ if (!truncate && unlikely((int64_t)i16 != i64))
+ fail("Integer is too big to fit in a 16-bit integer: %ld", i64);
+ return i16;
}
-MACROLIKE PUREFUNC Int16_t Int16$from_int32(Int32_t i, bool truncate) {
- if (!truncate && unlikely(i != (Int32_t)(Int16_t)i))
- fail("Integer is too big to fit in a 16-bit integer: %ld", i);
- return (Int16_t)i;
+MACROLIKE PUREFUNC Int16_t Int16$from_int32(Int32_t i32, bool truncate) {
+ int16_t i16 = (int16_t)i32;
+ if (!truncate && unlikely((int32_t)i16 != i32))
+ fail("Integer is too big to fit in a 16-bit integer: %ld", i32);
+ return i16;
}
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)
+ if (!truncate && unlikely((Num_t)i8 != n))
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)
+ if (!truncate && unlikely((Num32_t)i8 != n))
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;
- if (!truncate && unlikely(i64 != i8))
+ if (!truncate && unlikely((int64_t)i8 != i64))
fail("Integer is too big to fit in an 8-bit integer!");
return i8;
}
-MACROLIKE PUREFUNC Int8_t Int8$from_int64(Int64_t i, bool truncate) {
- if (!truncate && unlikely(i != (Int64_t)(Int8_t)i))
- fail("Integer is too big to fit in a 8-bit integer: %ld", i);
- return (Int8_t)i;
+MACROLIKE PUREFUNC Int8_t Int8$from_int64(Int64_t i64, bool truncate) {
+ int8_t i8 = (int8_t)i64;
+ if (!truncate && unlikely((int64_t)i8 != i64))
+ fail("Integer is too big to fit in a 8-bit integer: %ld", i64);
+ return i8;
}
-MACROLIKE PUREFUNC Int8_t Int8$from_int32(Int32_t i, bool truncate) {
- if (!truncate && unlikely(i != (Int32_t)(Int8_t)i))
- fail("Integer is too big to fit in a 8-bit integer: %ld", i);
- return (Int8_t)i;
+MACROLIKE PUREFUNC Int8_t Int8$from_int32(Int32_t i32, bool truncate) {
+ int8_t i8 = (int8_t)i32;
+ if (!truncate && unlikely((int32_t)i8 != i32))
+ fail("Integer is too big to fit in a 8-bit integer: %ld", i32);
+ return i8;
}
-MACROLIKE PUREFUNC Int8_t Int8$from_int16(Int16_t i, bool truncate) {
- if (!truncate && unlikely(i != (Int16_t)(Int8_t)i))
- fail("Integer is too big to fit in a 8-bit integer: %ld", i);
- return (Int8_t)i;
+MACROLIKE PUREFUNC Int8_t Int8$from_int16(Int16_t i16, bool truncate) {
+ int8_t i8 = (int8_t)i16;
+ if (!truncate && unlikely((int16_t)i8 != i16))
+ fail("Integer is too big to fit in a 8-bit integer: %ld", i16);
+ return i8;
}
#pragma GCC diagnostic pop