ona/source/coral/math.zig

74 lines
1.9 KiB
Zig
Raw Normal View History

2023-05-23 02:08:34 +02:00
const std = @import("std");
2023-07-22 14:57:39 +02:00
pub fn Int(comptime int: std.builtin.Type.Int) type {
return @Type(.{.Int = int});
}
pub fn clamp(value: anytype, lower: anytype, upper: anytype) @TypeOf(value, lower, upper) {
return @max(lower, @min(upper, value));
}
2023-07-29 17:04:27 +02:00
pub fn checked_add(a: anytype, b: anytype) ?@TypeOf(a + b) {
const result = @addWithOverflow(a, b);
return if (result.@"1" == 0) result.@"0" else null;
}
2023-11-20 22:39:42 +01:00
pub fn checked_cast(comptime dest_int: std.builtin.Type.Int, value: anytype) ?@Type(.{.Int = dest_int}) {
const dest_min = min_int(dest_int);
const dest_max = max_int(dest_int);
if (value < dest_min or value > dest_max) {
return null;
}
return @intCast(value);
}
2023-07-29 17:04:27 +02:00
pub fn checked_mul(a: anytype, b: anytype) ?@TypeOf(a * b) {
const result = @mulWithOverflow(a, b);
return if (result.@"1" == 0) result.@"0" else null;
}
pub fn checked_sub(a: anytype, b: anytype) ?@TypeOf(a - b) {
const result = @subWithOverflow(a, b);
return if (result.@"1" == 0) result.@"0" else null;
}
2023-07-22 14:57:39 +02:00
pub fn clamped_cast(comptime dest_int: std.builtin.Type.Int, value: anytype) Int(dest_int) {
const Value = @TypeOf(value);
return switch (@typeInfo(Value)) {
.Int => |int| switch (int.signedness) {
.signed => @intCast(clamp(value, min_int(dest_int), max_int(dest_int))),
.unsigned => @intCast(@min(value, max_int(dest_int))),
},
2023-07-29 17:04:27 +02:00
.Float => @intFromFloat(clamp(value, min_int(dest_int), max_int(dest_int))),
2023-07-22 14:57:39 +02:00
else => @compileError("`" ++ @typeName(Value) ++ "` cannot be cast to an int"),
};
2023-05-23 02:08:34 +02:00
}
pub fn max_int(comptime int: std.builtin.Type.Int) comptime_int {
const bit_count = int.bits;
2023-04-19 01:25:35 +02:00
if (bit_count == 0) return 0;
2023-07-10 02:10:56 +02:00
return (1 << (bit_count - @intFromBool(int.signedness == .signed))) - 1;
2023-04-19 01:25:35 +02:00
}
2023-05-23 02:08:34 +02:00
pub fn min_int(comptime int: std.builtin.Type.Int) comptime_int {
2023-07-22 15:44:33 +02:00
if (int.signedness == .unsigned) {
return 0;
}
2023-05-23 02:08:34 +02:00
const bit_count = int.bits;
if (bit_count == 0) return 0;
2023-07-22 15:44:33 +02:00
return -(1 << (bit_count - 1));
2023-04-19 01:25:35 +02:00
}