diff options
| author | koenditor <koend@tuta.io> | 2024-06-01 08:21:48 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2024-06-01 13:28:16 +0300 |
| commit | 64ef45eb059328ac8579c22506fa7574e8cf45f2 (patch) | |
| tree | d7b554a90fc86a0344b714a91e720528025b1788 /lib/std/math.zig | |
| parent | e09963d8544c19812db20e520f09f6e5d9a57d64 (diff) | |
| download | zig-64ef45eb059328ac8579c22506fa7574e8cf45f2.tar.gz zig-64ef45eb059328ac8579c22506fa7574e8cf45f2.zip | |
Support Vectors in std.math.clamp
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index 9668d30698..76786030de 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -518,7 +518,15 @@ test wrap { /// ``` /// Limit val to the inclusive range [lower, upper]. pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, upper) { - assert(lower <= upper); + const T = @TypeOf(val, lower, upper); + switch (@typeInfo(T)) { + .Int, .Float, .ComptimeInt, .ComptimeFloat => assert(lower <= upper), + .Vector => |vinfo| switch (@typeInfo(vinfo.child)) { + .Int, .Float => assert(@reduce(.And, lower <= upper)), + else => @compileError("Expected vector of ints or floats, found " ++ @typeName(T)), + }, + else => @compileError("Expected an int, float or vector of one, found " ++ @typeName(T)), + } return @max(lower, @min(val, upper)); } test clamp { @@ -533,6 +541,9 @@ test clamp { try testing.expect(std.math.clamp(@as(f32, 1.1), @as(f32, 0.0), @as(f32, 1.0)) == 1.0); try testing.expect(std.math.clamp(@as(f32, -127.5), @as(f32, -200), @as(f32, -100)) == -127.5); + // Vector + try testing.expect(@reduce(.And, std.math.clamp(@as(@Vector(3, f32), .{ 1.4, 15.23, 28.3 }), @as(@Vector(3, f32), .{ 9.8, 13.2, 15.6 }), @as(@Vector(3, f32), .{ 15.2, 22.8, 26.3 })) == @as(@Vector(3, f32), .{ 9.8, 15.23, 26.3 }))); + // Mix of comptime and non-comptime var i: i32 = 1; _ = &i; |
