authorgravatar for jahe788@gmail.comIntegratedQuantum <jahe788@gmail.com> 2022-11-11 15:26:26+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-12 15:41:55+02:00
logfbc4331f186c822a5a8d05152e70667026c97541
tree0abdf1434f1c159c51ccc6deaeb9b41ae56c7912
parent7733246d6e22573fdc9c9cd3a6a1c5c0d228ebfb

Implements std.math.sign for float vectors.


1 files changed, 11 insertions(+), 16 deletions(-)

lib/std/math.zig+11-16
......@@ -1596,9 +1596,8 @@ pub fn break_f80(x: f80) F80 {
15961596}
15971597
15981598/// Returns -1, 0, or 1.
1599/// Supports integer types, vectors of integer types, and float types.
1599/// Supports integer and float types and vectors of integer and float types.
16001600/// Unsigned integer types will always return 0 or 1.
1601/// TODO: support vectors of floats
16021601/// Branchless.
16031602pub inline fn sign(i: anytype) @TypeOf(i) {
16041603 const T = @TypeOf(i);
......@@ -1606,15 +1605,14 @@ pub inline fn sign(i: anytype) @TypeOf(i) {
16061605 .Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @boolToInt(i < 0),
16071606 .Float, .ComptimeFloat => @intToFloat(T, @boolToInt(i > 0)) - @intToFloat(T, @boolToInt(i < 0)),
16081607 .Vector => |vinfo| blk: {
1609 const u1xN = std.meta.Vector(vinfo.len, u1);
1610 break :blk switch (@typeInfo(vinfo.child)) {
1611 .Int => @as(T, @bitCast(u1xN, i > @splat(vinfo.len, @as(vinfo.child, 0)))) -
1612 @as(T, @bitCast(u1xN, i < @splat(vinfo.len, @as(vinfo.child, 0)))),
1613 .Float => @compileError("TODO: add support for vectors of floats once @intToFloat accepts vector types"),
1614 // break :blk @intToFloat(T, @bitCast(u1xN, i > @splat(vinfo.len, @as(vinfo.child, 0)))) -
1615 // @intToFloat(T, @bitCast(u1xN, i < @splat(vinfo.len, @as(vinfo.child, 0)))),
1608 switch (@typeInfo(vinfo.child)) {
1609 .Int, .Float => {
1610 const zero = @splat(vinfo.len, @as(vinfo.child, 0));
1611 const one = @splat(vinfo.len, @as(vinfo.child, 1));
1612 break :blk @select(vinfo.child, i > zero, one, zero) - @select(vinfo.child, i < zero, one, zero);
1613 },
16161614 else => @compileError("Expected vector of ints or floats, found " ++ @typeName(T)),
1617 };
1615 }
16181616 },
16191617 else => @compileError("Expected an int, float or vector of one, found " ++ @typeName(T)),
16201618 };
......@@ -1669,24 +1667,21 @@ fn testSign() !void {
16691667 try std.testing.expectEqual(@as(T, 1), sign(@as(T, 2)));
16701668 try std.testing.expectEqual(@as(T, -1), sign(@as(T, -2)));
16711669 try std.testing.expectEqual(@as(T, 0), sign(@as(T, 0)));
1672 // TODO - uncomment once @intToFloat supports vectors
1673 // try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
1670 try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
16741671 }
16751672 {
16761673 const T = f32;
16771674 try std.testing.expectEqual(@as(T, 1), sign(@as(T, 2)));
16781675 try std.testing.expectEqual(@as(T, -1), sign(@as(T, -2)));
16791676 try std.testing.expectEqual(@as(T, 0), sign(@as(T, 0)));
1680 // TODO - uncomment once @intToFloat supports vectors
1681 // try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
1677 try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
16821678 }
16831679 {
16841680 const T = f64;
16851681 try std.testing.expectEqual(@as(T, 1), sign(@as(T, 2)));
16861682 try std.testing.expectEqual(@as(T, -1), sign(@as(T, -2)));
16871683 try std.testing.expectEqual(@as(T, 0), sign(@as(T, 0)));
1688 // TODO - uncomment once @intToFloat supports vectors
1689 // try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
1684 try std.testing.expectEqual(@Vector(3, T){ 1, -1, 0 }, sign(@Vector(3, T){ 2, -2, 0 }));
16901685 }
16911686
16921687 // comptime_int