zig/test/behavior/x86_64/math.zig
2025-01-16 20:42:08 -05:00

294 lines
13 KiB
Zig

fn testBinary(comptime op: anytype) !void {
const testType = struct {
fn testType(comptime Type: type, comptime imm_lhs: Type, comptime imm_rhs: Type) !void {
const expected = op(Type, imm_lhs, imm_rhs);
try struct {
fn testOne(actual: @TypeOf(expected)) !void {
if (switch (@typeInfo(@TypeOf(expected))) {
else => actual != expected,
.vector => @reduce(.Or, actual != expected),
}) return error.Unexpected;
}
noinline fn testOps(mem_lhs: Type, mem_rhs: Type) !void {
var reg_lhs = mem_lhs;
var reg_rhs = mem_rhs;
_ = .{ &reg_lhs, &reg_rhs };
try testOne(op(Type, reg_lhs, reg_rhs));
try testOne(op(Type, reg_lhs, mem_rhs));
try testOne(op(Type, reg_lhs, imm_rhs));
try testOne(op(Type, mem_lhs, reg_rhs));
try testOne(op(Type, mem_lhs, mem_rhs));
try testOne(op(Type, mem_lhs, imm_rhs));
try testOne(op(Type, imm_lhs, reg_rhs));
try testOne(op(Type, imm_lhs, mem_rhs));
}
}.testOps(imm_lhs, imm_rhs);
}
}.testType;
try testType(u8, 0xbb, 0x43);
try testType(u16, 0xb8bf, 0x626d);
try testType(u32, 0x80d7a2c6, 0xbff6a402);
try testType(u64, 0x71138bc6b4a38898, 0x1bc4043de9438c7b);
try testType(u128, 0xe05fc132ef2cd8affee00a907f0a851f, 0x29f912a72cfc6a7c6973426a9636da9a);
try testType(
u256,
0xb7935f5c2f3b1ae7a422c0a7c446884294b7d5370bada307d2fe5a4c4284a999,
0x310e6e196ba4f143b8d285ca6addf7f3bb3344224aff221b27607a31e148be08,
);
try testType(
u512,
0xe5b1fedca3c77db765e517aabd05ffc524a3a8aff1784bbf67c45b894447ede32b65b9940e78173c591e56e078932d465f235aece7ad47b7f229df7ba8f12295,
0x8b4bb7c2969e3b121cc1082c442f8b4330f0a50058438fed56447175bb10178607ecfe425cb54dacc25ef26810f3e04681de1844f1aa8d029aca75d658634806,
);
try testType(@Vector(1, u8), .{
0x1f,
}, .{
0x06,
});
try testType(@Vector(2, u8), .{
0x80, 0x63,
}, .{
0xe4, 0x28,
});
try testType(@Vector(4, u8), .{
0x83, 0x9e, 0x1e, 0xc1,
}, .{
0xf0, 0x5c, 0x46, 0x85,
});
try testType(@Vector(8, u8), .{
0x1e, 0x4d, 0x9d, 0x2a, 0x4c, 0x74, 0x0a, 0x83,
}, .{
0x28, 0x60, 0xa9, 0xb5, 0xd9, 0xa6, 0xf1, 0xb6,
});
try testType(@Vector(16, u8), .{
0xea, 0x80, 0xbb, 0xe8, 0x74, 0x81, 0xc8, 0x66, 0x7b, 0x41, 0x90, 0xcb, 0x30, 0x70, 0x4b, 0x0f,
}, .{
0x61, 0x26, 0xbe, 0x47, 0x00, 0x9c, 0x55, 0xa5, 0x59, 0xf0, 0xb2, 0x20, 0x30, 0xaf, 0x82, 0x3e,
});
try testType(@Vector(32, u8), .{
0xa1, 0x88, 0xc4, 0xf4, 0x77, 0x0b, 0xf5, 0xbb, 0x09, 0x03, 0xbf, 0xf5, 0xcc, 0x7f, 0x6b, 0x2a,
0x4c, 0x05, 0x37, 0xc9, 0x8a, 0xcb, 0x91, 0x23, 0x09, 0x5f, 0xb8, 0x99, 0x4a, 0x75, 0x26, 0xe4,
}, .{
0xff, 0x0f, 0x99, 0x49, 0xa6, 0x25, 0xa7, 0xd4, 0xc9, 0x2f, 0x97, 0x6a, 0x01, 0xd6, 0x6e, 0x41,
0xa4, 0xb5, 0x3c, 0x03, 0xea, 0x82, 0x9c, 0x5f, 0xac, 0x07, 0x16, 0x15, 0x1c, 0x64, 0x25, 0x2f,
});
try testType(@Vector(64, u8), .{
0xaa, 0x08, 0xeb, 0xb2, 0xd7, 0x89, 0x0f, 0x98, 0xda, 0x9f, 0xa6, 0x4e, 0x3c, 0xce, 0x1b, 0x1b,
0x9e, 0x5f, 0x2b, 0xd6, 0x59, 0x26, 0x47, 0x05, 0x2a, 0xb7, 0xd1, 0x10, 0xde, 0xd9, 0x84, 0x00,
0x07,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Type(@Vector(2, u16), .{
0xa0fa, 0xc365,
}, .{
0xe736, 0xc394,
});
try testType(@Vector(4, u16), .{
0x9608, 0xa558, 0x161b, 0x206f,
}, .{
0x3088, 0xf25c, 0x7837, 0x9b3f,
});
try testType(@Vector(8, u16), .{
0xcf61, 0xb121, 0x3cf1, 0x3e9f, 0x43a7, 0x8d69, 0x96f5, 0xc11e,
}, .{
0xee30, 0x82f0, 0x270b, 0x1498, 0x4c60, 0x6e72, 0x0b64, 0x02d4,
});
try testType(@Vector(16, u16), .{
0x9191, 0xd23e, 0xf844, 0xd84a, 0xe907, 0xf1e8, 0x712d, 0x90af,
0x6541, 0x3fa6, 0x92eb, 0xe35a, 0xc0c9, 0xcb47, 0xb790, 0x4453,
}, .{
0x21c3, 0x4039, 0x9b71, 0x60bd, 0xcd7f, 0x2ec8, 0x50ba, 0xe810,
0xebd4, 0x06e5, 0xed18, 0x2f66, 0x7e31, 0xe282, 0xad63, 0xb25e,
});
try testType(@Vector(32, u16), .{
0x6b6a, 0x30a9, 0xc267, 0x2231, 0xbf4c, 0x00bc, 0x9c2c, 0x2928,
0xecad, 0x82df, 0xcfb0, 0xa4e5, 0x909b, 0x1b05, 0xaf40, 0x1fd9,
0xcec6, 0xd8dc, 0xd4b5, 0x6d59, 0x8e3f, 0x4d8a, 0xb83a, 0x808e,
0x47e2, 0x5782, 0x59bf, 0xcefc, 0x5179, 0x3f48, 0x93dc, 0x66d2,
}, .{
0x1be8, 0xe98c, 0xf9b3, 0xb008, 0x2f8d, 0xf087, 0xc9b9, 0x75aa,
0xbd16, 0x9540, 0xc5bd, 0x2b2c, 0xd43f, 0x9394, 0x3e1d, 0xf695,
0x167d, 0xff7a, 0xf09d, 0xdff8, 0xdfa2, 0xc779, 0x70b7, 0x01bd,
0x46b3, 0x995a, 0xb7bc, 0xa79d, 0x5542, 0x961e, 0x37cd, 0x9c2a,
});
try testType(@Vector(64, u16), .{
0x6b87, 0xfd84, 0x436b, 0xe345, 0xfb82, 0x81fc, 0x0992, 0x45f9,
0x5527, 0x1f6d, 0xda46, 0x6a16, 0xf6e1, 0x8fb7, 0x3619, 0xdfe3,
0x64ce, 0x8ac6, 0x3ae8, 0x30e3, 0xec3b, 0x4ba7, 0x02a4, 0xa694,
0x8e68, 0x8f0c, 0x5e30, 0x0e55, 0x6538, 0x9852, 0xea35, 0x7be2,
0xdabd, 0x57e6, 0x5b38, 0x0fb2, 0x2604, 0x85e7, 0x6595, 0x8de9,
0x49b1, 0xe9a2, 0x3758, 0xa4d9, 0x505b, 0xc9d3, 0xddc5, 0x9a43,
0xfd44, 0x50f5, 0x379e, 0x03b6, 0x6375, 0x692f, 0x5586, 0xc717,
0x94dd, 0xee06, 0xb32d, 0x0bb9, 0x0e35, 0x5f8f, 0x0ba4, 0x19a8,
}, .{
0xbeeb, 0x3e54, 0x6486, 0x5167, 0xe432, 0x57cf, 0x9cac, 0x922e,
0xd2f8, 0x5614, 0x2e7f, 0x19cf, 0x9a07, 0x0524, 0x168f, 0x4464,
0x4def, 0x83ce, 0x97b4, 0xf269, 0xda5f, 0x28c1, 0x9cc3, 0xfa7c,
0x25a0, 0x912d, 0x25b2, 0xd60d, 0xcd82, 0x0e03, 0x40cc, 0xc9dc,
0x18eb, 0xc609, 0xb06d, 0x29e0, 0xf3c7, 0x997b, 0x8ca2, 0xa750,
0xc9bc, 0x8f0e, 0x3916, 0xd905, 0x94f8, 0x397f, 0x98b5, 0xc61d,
0x05db, 0x3e7a, 0xf750, 0xe8de, 0x3225, 0x81d9, 0x612e, 0x0a7e,
0x2c02, 0xff5b, 0x19ca, 0xbbf5, 0x870e, 0xc9ca, 0x47bb, 0xcfcc,
});
try testType(@Vector(1, u32), .{
0x1d0d9cc4,
}, .{
0xce2d0ab6,
});
try testType(@Vector(2, u32), .{
0x5ab78c03, 0xd21bb513,
}, .{
0x8a6664eb, 0x79eac37d,
});
try testType(@Vector(4, u32), .{
0x234d576e, 0x4151cc9c, 0x39f558e4, 0xba935a32,
}, .{
0x398f2a9d, 0x4540f093, 0x9225551c, 0x3bac865b,
});
try testType(@Vector(8, u32), .{
0xb8336635, 0x2fc3182c, 0x27a00123, 0x71587fbe,
0x9cbc65d2, 0x6f4bb0e6, 0x362594ce, 0x9971df38,
}, .{
0x5727e734, 0x972b0313, 0xff25f5dc, 0x924f8e55,
0x04920a61, 0xa1c3b334, 0xf52df4b6, 0x5ef72ecc,
});
try testType(@Vector(16, u32), .{
0xfb566f9e, 0x9ad4691a, 0x5b5f9ec0, 0x5a572d2a,
0x8f2f226b, 0x2dfc7e33, 0x9fb07e32, 0x9d672a2e,
0xbedc3cee, 0x6872428d, 0xbc73a9fd, 0xd4d5f055,
0x69c1e9ee, 0x65038deb, 0x1449061a, 0x48412ec2,
}, .{
0x96cbe946, 0x3f24f60b, 0xaeacdc53, 0x7611a8b4,
0x031a67a8, 0x52a26828, 0x75646f4b, 0xb75902c3,
0x1f881f08, 0x834e02a4, 0x5e5b40eb, 0xc75c264d,
0xa8251e09, 0x28e46bbd, 0x12cb1f31, 0x9a2af615,
});
try testType(@Vector(32, u32), .{
0x131bbb7b, 0xa7311026, 0x9d5e59a0, 0x99b090d6,
0xfe969e2e, 0x04547697, 0x357d3250, 0x43be6d7a,
0x16ecf5c5, 0xf60febcc, 0x1d1e2602, 0x138a96d2,
0x9117ba72, 0x9f185b32, 0xc10e23fd, 0x3e6b7fd8,
0x4dc9be70, 0x2ee30047, 0xaffeab60, 0x7172d362,
0x6154bfcf, 0x5388dc3e, 0xd6e5a76e, 0x8b782f2d,
0xacbef4a2, 0x843aca71, 0x25d8ab5c, 0xe1a63a39,
0xc26212e5, 0x0847b84b, 0xb53541e5, 0x0c8e44db,
}, .{
0x4ad92822, 0x715b623f, 0xa5bed8a7, 0x937447a9,
0x7ecb38eb, 0x0a2f3dfc, 0x96f467a2, 0xec882793,
0x41a8707f, 0xf7310656, 0x76217b80, 0x2058e5fc,
0x26682154, 0x87313e31, 0x4bdc480a, 0x193572ff,
0x60b03c75, 0x0fe45908, 0x56c73703, 0xdb86554c,
0xdda2dd7d, 0x34371b27, 0xe4e6ad50, 0x422d1828,
0x1de3801b, 0xdce268d3, 0x20af9ec8, 0x188a591f,
0xf080e943, 0xc8718d14, 0x3f920382, 0x18d101b5,
});
try testType(@Vector(1, u64), .{
0x333f593bf9d08546,
}, .{
0x6918bd767e730778,
});
try testType(@Vector(2, u64), .{
0x4cd89a317b03d430, 0x28998f61842f63a9,
}, .{
0x6c34db64af0e217e, 0x57aa5d02cd45dceb,
});
try testType(@Vector(4, u64), .{
0x946cf7e7484691c9, 0xf4fc5be2a762fcbf,
0x71cc83bc25abaf14, 0xc69cef44c6f833a1,
}, .{
0x9f90cbd6c3ce1d4e, 0x182f65295dff4e84,
0x4dfe62c59fed0040, 0x18402347c1db1999,
});
try testType(@Vector(8, u64), .{
0x92c6281333943e2c, 0xa97750504668efb5,
0x234be51057c0181f, 0xefbc1f407f3df4fb,
0x8da6cc7c39cebb94, 0xb408f7e56feee497,
0x2363f1f8821592ed, 0x01716e800c0619e1,
}, .{
0xa617426684147e7e, 0x7542da7ebe093a7b,
0x3f21d99ac57606b7, 0x65cd36d697d22de4,
0xed23d6bdf176c844, 0x2d4573f100ff7b58,
0x4968f4d21b49f8ab, 0xf5d9a205d453e933,
});
try testType(@Vector(16, u64), .{
0x2f61a4ee66177b4a, 0xf13b286b279f6a93,
0x36b46beb63665318, 0x74294dbde0da98d2,
0x3aa872ba60b936eb, 0xe8f698b36e62600b,
0x9e8930c21a6a1a76, 0x876998b09b8eb03c,
0xa0244771a2ec0adb, 0xb4c72bff3d3ac1a2,
0xd70677210830eced, 0x6622abc1734dd72d,
0x157e2bb0d57d6596, 0x2aac8192fb7ef973,
0xc4a0ca92f34d7b13, 0x04300f8ad1845246,
}, .{
0xeaf71dcf0eb76f5d, 0x0e84b1b63dc97139,
0x0f64cc38d23c94a1, 0x12775cf0816349b7,
0xfdcf13387ba48d54, 0xf8d3c672cacd8779,
0xe728c1f5eb56ab1e, 0x05931a34877f7a69,
0x1861a763c8dafd1f, 0x4ac97573ecd5739f,
0x3384414c9bf77b8c, 0x32c15bbd04a5ddc4,
0xbfd88aee1d82ed32, 0x20e91c15b701059a,
0xed533d18f8657f3f, 0x1ddd7cd7f6bab957,
});
if (false) try testType(@Vector(1, u128), .{
0x5f11e16b0ca3392f907a857881455d2e,
}, .{
0xf9142d73b408fd6955922f9fc147f7d7,
});
}
inline fn bitAnd(comptime Type: type, lhs: Type, rhs: Type) @TypeOf(lhs & rhs) {
return lhs & rhs;
}
test bitAnd {
try testBinary(bitAnd);
}
inline fn bitOr(comptime Type: type, lhs: Type, rhs: Type) @TypeOf(lhs | rhs) {
return lhs | rhs;
}
test bitOr {
try testBinary(bitOr);
}
inline fn bitXor(comptime Type: type, lhs: Type, rhs: Type) @TypeOf(lhs ^ rhs) {
return lhs ^ rhs;
}
test bitXor {
try testBinary(bitXor);
}