mirror of
https://github.com/ziglang/zig.git
synced 2026-02-20 08:14:48 +00:00
short std.builtin enum literals in std lib
This commit is contained in:
parent
3c14327011
commit
00be934569
@ -1898,10 +1898,10 @@ pub const LibExeObjStep = struct {
|
||||
}
|
||||
|
||||
switch (self.build_mode) {
|
||||
builtin.Mode.Debug => {},
|
||||
builtin.Mode.ReleaseSafe => zig_args.append("--release-safe") catch unreachable,
|
||||
builtin.Mode.ReleaseFast => zig_args.append("--release-fast") catch unreachable,
|
||||
builtin.Mode.ReleaseSmall => zig_args.append("--release-small") catch unreachable,
|
||||
.Debug => {},
|
||||
.ReleaseSafe => zig_args.append("--release-safe") catch unreachable,
|
||||
.ReleaseFast => zig_args.append("--release-fast") catch unreachable,
|
||||
.ReleaseSmall => zig_args.append("--release-small") catch unreachable,
|
||||
}
|
||||
|
||||
try zig_args.append("--cache-dir");
|
||||
|
||||
@ -120,7 +120,7 @@ fn usage() void {
|
||||
}
|
||||
|
||||
fn mode(comptime x: comptime_int) comptime_int {
|
||||
return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
|
||||
return if (builtin.mode == .Debug) x / 64 else x;
|
||||
}
|
||||
|
||||
// TODO(#1358): Replace with builtin formatted padding when available.
|
||||
|
||||
@ -370,8 +370,8 @@ pub const Elf = struct {
|
||||
};
|
||||
|
||||
elf.endian = switch (try in.readByte()) {
|
||||
1 => builtin.Endian.Little,
|
||||
2 => builtin.Endian.Big,
|
||||
1 => .Little,
|
||||
2 => .Big,
|
||||
else => return error.InvalidFormat,
|
||||
};
|
||||
|
||||
|
||||
@ -25,13 +25,13 @@ pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void {
|
||||
const info = @typeInfo(@TypeOf(key));
|
||||
|
||||
switch (info.Pointer.size) {
|
||||
builtin.TypeInfo.Pointer.Size.One => switch (strat) {
|
||||
.One => switch (strat) {
|
||||
.Shallow => hash(hasher, @ptrToInt(key), .Shallow),
|
||||
.Deep => hash(hasher, key.*, .Shallow),
|
||||
.DeepRecursive => hash(hasher, key.*, .DeepRecursive),
|
||||
},
|
||||
|
||||
builtin.TypeInfo.Pointer.Size.Slice => switch (strat) {
|
||||
.Slice => switch (strat) {
|
||||
.Shallow => {
|
||||
hashPointer(hasher, key.ptr, .Shallow);
|
||||
hash(hasher, key.len, .Shallow);
|
||||
@ -40,9 +40,7 @@ pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void {
|
||||
.DeepRecursive => hashArray(hasher, key, .DeepRecursive),
|
||||
},
|
||||
|
||||
builtin.TypeInfo.Pointer.Size.Many,
|
||||
builtin.TypeInfo.Pointer.Size.C,
|
||||
=> switch (strat) {
|
||||
.Many, .C, => switch (strat) {
|
||||
.Shallow => hash(hasher, @ptrToInt(key), .Shallow),
|
||||
else => @compileError(
|
||||
\\ unknown-length pointers and C pointers cannot be hashed deeply.
|
||||
|
||||
@ -168,7 +168,7 @@ fn usage() void {
|
||||
}
|
||||
|
||||
fn mode(comptime x: comptime_int) comptime_int {
|
||||
return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
|
||||
return if (builtin.mode == .Debug) x / 64 else x;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
|
||||
@ -11,7 +11,7 @@ pub const CityHash32 = struct {
|
||||
fn fetch32(ptr: [*]const u8) u32 {
|
||||
var v: u32 = undefined;
|
||||
@memcpy(@ptrCast([*]u8, &v), ptr, 4);
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
return @byteSwap(u32, v);
|
||||
return v;
|
||||
}
|
||||
@ -174,7 +174,7 @@ pub const CityHash64 = struct {
|
||||
fn fetch32(ptr: [*]const u8) u32 {
|
||||
var v: u32 = undefined;
|
||||
@memcpy(@ptrCast([*]u8, &v), ptr, 4);
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
return @byteSwap(u32, v);
|
||||
return v;
|
||||
}
|
||||
@ -182,7 +182,7 @@ pub const CityHash64 = struct {
|
||||
fn fetch64(ptr: [*]const u8) u64 {
|
||||
var v: u64 = undefined;
|
||||
@memcpy(@ptrCast([*]u8, &v), ptr, 8);
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
return @byteSwap(u64, v);
|
||||
return v;
|
||||
}
|
||||
@ -369,7 +369,7 @@ fn SMHasherTest(comptime hash_fn: var, comptime hashbits: u32) u32 {
|
||||
key[i] = @intCast(u8, i);
|
||||
|
||||
var h = hash_fn(key[0..i], 256 - i);
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
h = @byteSwap(@TypeOf(h), h);
|
||||
@memcpy(@ptrCast([*]u8, &hashes[i * hashbytes]), @ptrCast([*]u8, &h), hashbytes);
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ pub const Murmur2_32 = struct {
|
||||
var h1: u32 = seed ^ len;
|
||||
for (@ptrCast([*]align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {
|
||||
var k1: u32 = v;
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
k1 = @byteSwap(u32, k1);
|
||||
k1 *%= m;
|
||||
k1 ^= k1 >> 24;
|
||||
@ -102,7 +102,7 @@ pub const Murmur2_64 = struct {
|
||||
var h1: u64 = seed ^ (len *% m);
|
||||
for (@ptrCast([*]align(1) const u64, str.ptr)[0..@intCast(usize, len >> 3)]) |v| {
|
||||
var k1: u64 = v;
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
k1 = @byteSwap(u64, k1);
|
||||
k1 *%= m;
|
||||
k1 ^= k1 >> 47;
|
||||
@ -115,7 +115,7 @@ pub const Murmur2_64 = struct {
|
||||
if (rest > 0) {
|
||||
var k1: u64 = 0;
|
||||
@memcpy(@ptrCast([*]u8, &k1), @ptrCast([*]const u8, &str[@intCast(usize, offset)]), @intCast(usize, rest));
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
k1 = @byteSwap(u64, k1);
|
||||
h1 ^= k1;
|
||||
h1 *%= m;
|
||||
@ -182,7 +182,7 @@ pub const Murmur3_32 = struct {
|
||||
var h1: u32 = seed;
|
||||
for (@ptrCast([*]align(1) const u32, str.ptr)[0..(len >> 2)]) |v| {
|
||||
var k1: u32 = v;
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
k1 = @byteSwap(u32, k1);
|
||||
k1 *%= c1;
|
||||
k1 = rotl32(k1, 15);
|
||||
@ -294,7 +294,7 @@ fn SMHasherTest(comptime hash_fn: var, comptime hashbits: u32) u32 {
|
||||
key[i] = @truncate(u8, i);
|
||||
|
||||
var h = hash_fn(key[0..i], 256 - i);
|
||||
if (builtin.endian == builtin.Endian.Big)
|
||||
if (builtin.endian == .Big)
|
||||
h = @byteSwap(@TypeOf(h), h);
|
||||
@memcpy(@ptrCast([*]u8, &hashes[i * hashbytes]), @ptrCast([*]u8, &h), hashbytes);
|
||||
}
|
||||
@ -308,7 +308,7 @@ test "murmur2_32" {
|
||||
var v1: u64 = 0x1234567812345678;
|
||||
var v0le: u32 = v0;
|
||||
var v1le: u64 = v1;
|
||||
if (builtin.endian == builtin.Endian.Big) {
|
||||
if (builtin.endian == .Big) {
|
||||
v0le = @byteSwap(u32, v0le);
|
||||
v1le = @byteSwap(u64, v1le);
|
||||
}
|
||||
@ -322,7 +322,7 @@ test "murmur2_64" {
|
||||
var v1: u64 = 0x1234567812345678;
|
||||
var v0le: u32 = v0;
|
||||
var v1le: u64 = v1;
|
||||
if (builtin.endian == builtin.Endian.Big) {
|
||||
if (builtin.endian == .Big) {
|
||||
v0le = @byteSwap(u32, v0le);
|
||||
v1le = @byteSwap(u64, v1le);
|
||||
}
|
||||
@ -336,7 +336,7 @@ test "murmur3_32" {
|
||||
var v1: u64 = 0x1234567812345678;
|
||||
var v0le: u32 = v0;
|
||||
var v1le: u64 = v1;
|
||||
if (builtin.endian == builtin.Endian.Big) {
|
||||
if (builtin.endian == .Big) {
|
||||
v0le = @byteSwap(u32, v0le);
|
||||
v1le = @byteSwap(u64, v1le);
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ const Wyhash = std.hash.Wyhash;
|
||||
const Allocator = mem.Allocator;
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const want_modification_safety = builtin.mode != builtin.Mode.ReleaseFast;
|
||||
const want_modification_safety = builtin.mode != .ReleaseFast;
|
||||
const debug_u32 = if (want_modification_safety) u32 else void;
|
||||
|
||||
pub fn AutoHashMap(comptime K: type, comptime V: type) type {
|
||||
|
||||
@ -348,11 +348,11 @@ pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
const n = if (self.bit_count >= bits) @intCast(u3, bits) else self.bit_count;
|
||||
const shift = u7_bit_count - n;
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
out_buffer = @as(Buf, self.bit_buffer >> shift);
|
||||
self.bit_buffer <<= n;
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
const value = (self.bit_buffer << shift) >> shift;
|
||||
out_buffer = @as(Buf, value);
|
||||
self.bit_buffer >>= n;
|
||||
@ -376,7 +376,7 @@ pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
};
|
||||
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
if (n >= u8_bit_count) {
|
||||
out_buffer <<= @intCast(u3, u8_bit_count - 1);
|
||||
out_buffer <<= 1;
|
||||
@ -392,7 +392,7 @@ pub fn BitInStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
self.bit_buffer = @truncate(u7, next_byte << @intCast(u3, n - 1));
|
||||
self.bit_count = shift;
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
if (n >= u8_bit_count) {
|
||||
out_buffer |= @as(Buf, next_byte) << @intCast(BufShift, out_bits.*);
|
||||
out_bits.* += u8_bit_count;
|
||||
@ -666,8 +666,8 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
|
||||
const high_byte_shift = @intCast(BufShift, buf_bit_count - u8_bit_count);
|
||||
var in_buffer = switch (endian) {
|
||||
builtin.Endian.Big => buf_value << @intCast(BufShift, buf_bit_count - bits),
|
||||
builtin.Endian.Little => buf_value,
|
||||
.Big => buf_value << @intCast(BufShift, buf_bit_count - bits),
|
||||
.Little => buf_value,
|
||||
};
|
||||
var in_bits = bits;
|
||||
|
||||
@ -675,13 +675,13 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
const bits_remaining = u8_bit_count - self.bit_count;
|
||||
const n = @intCast(u3, if (bits_remaining > bits) bits else bits_remaining);
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
const shift = @intCast(BufShift, high_byte_shift + self.bit_count);
|
||||
const v = @intCast(u8, in_buffer >> shift);
|
||||
self.bit_buffer |= v;
|
||||
in_buffer <<= n;
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
const v = @truncate(u8, in_buffer) << @intCast(u3, self.bit_count);
|
||||
self.bit_buffer |= v;
|
||||
in_buffer >>= n;
|
||||
@ -701,13 +701,13 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
//copy bytes until we can't fill one anymore, then leave the rest in bit_buffer
|
||||
while (in_bits >= u8_bit_count) {
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
const v = @intCast(u8, in_buffer >> high_byte_shift);
|
||||
try self.out_stream.writeByte(v);
|
||||
in_buffer <<= @intCast(u3, u8_bit_count - 1);
|
||||
in_buffer <<= 1;
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
const v = @truncate(u8, in_buffer);
|
||||
try self.out_stream.writeByte(v);
|
||||
in_buffer >>= @intCast(u3, u8_bit_count - 1);
|
||||
@ -720,8 +720,8 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type {
|
||||
if (in_bits > 0) {
|
||||
self.bit_count = @intCast(u4, in_bits);
|
||||
self.bit_buffer = switch (endian) {
|
||||
builtin.Endian.Big => @truncate(u8, in_buffer >> high_byte_shift),
|
||||
builtin.Endian.Little => @truncate(u8, in_buffer),
|
||||
.Big => @truncate(u8, in_buffer >> high_byte_shift),
|
||||
.Little => @truncate(u8, in_buffer),
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -858,10 +858,10 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
|
||||
var result = @as(U, 0);
|
||||
for (buffer) |byte, i| {
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
result = (result << u8_bit_count) | byte;
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
result |= @as(U, byte) << @intCast(Log2U, u8_bit_count * i);
|
||||
},
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ const expect = std.testing.expect;
|
||||
/// - pow(-inf, y) = pow(-0, -y)
|
||||
/// - pow(x, y) = nan for finite x < 0 and finite non-integer y
|
||||
pub fn pow(comptime T: type, x: T, y: T) T {
|
||||
if (@typeInfo(T) == builtin.TypeId.Int) {
|
||||
if (@typeInfo(T) == .Int) {
|
||||
return math.powi(T, x, y) catch unreachable;
|
||||
}
|
||||
|
||||
|
||||
@ -25,7 +25,7 @@ pub fn powi(comptime T: type, x: T, y: T) (error{
|
||||
}!T) {
|
||||
const info = @typeInfo(T);
|
||||
|
||||
comptime assert(@typeInfo(T) == builtin.TypeId.Int);
|
||||
comptime assert(@typeInfo(T) == .Int);
|
||||
|
||||
// powi(x, +-0) = 1 for any x
|
||||
if (y == 0 or y == -0) {
|
||||
|
||||
@ -746,12 +746,12 @@ test "mem.indexOf" {
|
||||
pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: builtin.Endian) ReturnType {
|
||||
var result: ReturnType = 0;
|
||||
switch (endian) {
|
||||
builtin.Endian.Big => {
|
||||
.Big => {
|
||||
for (bytes) |b| {
|
||||
result = (result << 8) | b;
|
||||
}
|
||||
},
|
||||
builtin.Endian.Little => {
|
||||
.Little => {
|
||||
const ShiftType = math.Log2Int(ReturnType);
|
||||
for (bytes) |b, index| {
|
||||
result = result | (@as(ReturnType, b) << @intCast(ShiftType, index * 8));
|
||||
@ -779,13 +779,13 @@ pub fn readIntForeign(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)
|
||||
}
|
||||
|
||||
pub const readIntLittle = switch (builtin.endian) {
|
||||
builtin.Endian.Little => readIntNative,
|
||||
builtin.Endian.Big => readIntForeign,
|
||||
.Little => readIntNative,
|
||||
.Big => readIntForeign,
|
||||
};
|
||||
|
||||
pub const readIntBig = switch (builtin.endian) {
|
||||
builtin.Endian.Little => readIntForeign,
|
||||
builtin.Endian.Big => readIntNative,
|
||||
.Little => readIntForeign,
|
||||
.Big => readIntNative,
|
||||
};
|
||||
|
||||
/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
|
||||
@ -809,13 +809,13 @@ pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T {
|
||||
}
|
||||
|
||||
pub const readIntSliceLittle = switch (builtin.endian) {
|
||||
builtin.Endian.Little => readIntSliceNative,
|
||||
builtin.Endian.Big => readIntSliceForeign,
|
||||
.Little => readIntSliceNative,
|
||||
.Big => readIntSliceForeign,
|
||||
};
|
||||
|
||||
pub const readIntSliceBig = switch (builtin.endian) {
|
||||
builtin.Endian.Little => readIntSliceForeign,
|
||||
builtin.Endian.Big => readIntSliceNative,
|
||||
.Little => readIntSliceForeign,
|
||||
.Big => readIntSliceNative,
|
||||
};
|
||||
|
||||
/// Reads an integer from memory with bit count specified by T.
|
||||
@ -892,13 +892,13 @@ pub fn writeIntForeign(comptime T: type, buf: *[@divExact(T.bit_count, 8)]u8, va
|
||||
}
|
||||
|
||||
pub const writeIntLittle = switch (builtin.endian) {
|
||||
builtin.Endian.Little => writeIntNative,
|
||||
builtin.Endian.Big => writeIntForeign,
|
||||
.Little => writeIntNative,
|
||||
.Big => writeIntForeign,
|
||||
};
|
||||
|
||||
pub const writeIntBig = switch (builtin.endian) {
|
||||
builtin.Endian.Little => writeIntForeign,
|
||||
builtin.Endian.Big => writeIntNative,
|
||||
.Little => writeIntForeign,
|
||||
.Big => writeIntNative,
|
||||
};
|
||||
|
||||
/// Writes an integer to memory, storing it in twos-complement.
|
||||
@ -950,13 +950,13 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {
|
||||
}
|
||||
|
||||
pub const writeIntSliceNative = switch (builtin.endian) {
|
||||
builtin.Endian.Little => writeIntSliceLittle,
|
||||
builtin.Endian.Big => writeIntSliceBig,
|
||||
.Little => writeIntSliceLittle,
|
||||
.Big => writeIntSliceBig,
|
||||
};
|
||||
|
||||
pub const writeIntSliceForeign = switch (builtin.endian) {
|
||||
builtin.Endian.Little => writeIntSliceBig,
|
||||
builtin.Endian.Big => writeIntSliceLittle,
|
||||
.Little => writeIntSliceBig,
|
||||
.Big => writeIntSliceLittle,
|
||||
};
|
||||
|
||||
/// Writes a twos-complement integer to memory, with the specified endianness.
|
||||
@ -967,10 +967,10 @@ pub const writeIntSliceForeign = switch (builtin.endian) {
|
||||
/// use writeInt instead.
|
||||
pub fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: builtin.Endian) void {
|
||||
comptime assert(T.bit_count % 8 == 0);
|
||||
switch (endian) {
|
||||
builtin.Endian.Little => return writeIntSliceLittle(T, buffer, value),
|
||||
builtin.Endian.Big => return writeIntSliceBig(T, buffer, value),
|
||||
}
|
||||
return switch (endian) {
|
||||
.Little => writeIntSliceLittle(T, buffer, value),
|
||||
.Big => writeIntSliceBig(T, buffer, value),
|
||||
};
|
||||
}
|
||||
|
||||
test "writeIntBig and writeIntLittle" {
|
||||
@ -1506,54 +1506,54 @@ test "rotate" {
|
||||
/// Converts a little-endian integer to host endianness.
|
||||
pub fn littleToNative(comptime T: type, x: T) T {
|
||||
return switch (builtin.endian) {
|
||||
builtin.Endian.Little => x,
|
||||
builtin.Endian.Big => @byteSwap(T, x),
|
||||
.Little => x,
|
||||
.Big => @byteSwap(T, x),
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts a big-endian integer to host endianness.
|
||||
pub fn bigToNative(comptime T: type, x: T) T {
|
||||
return switch (builtin.endian) {
|
||||
builtin.Endian.Little => @byteSwap(T, x),
|
||||
builtin.Endian.Big => x,
|
||||
.Little => @byteSwap(T, x),
|
||||
.Big => x,
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts an integer from specified endianness to host endianness.
|
||||
pub fn toNative(comptime T: type, x: T, endianness_of_x: builtin.Endian) T {
|
||||
return switch (endianness_of_x) {
|
||||
builtin.Endian.Little => littleToNative(T, x),
|
||||
builtin.Endian.Big => bigToNative(T, x),
|
||||
.Little => littleToNative(T, x),
|
||||
.Big => bigToNative(T, x),
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts an integer which has host endianness to the desired endianness.
|
||||
pub fn nativeTo(comptime T: type, x: T, desired_endianness: builtin.Endian) T {
|
||||
return switch (desired_endianness) {
|
||||
builtin.Endian.Little => nativeToLittle(T, x),
|
||||
builtin.Endian.Big => nativeToBig(T, x),
|
||||
.Little => nativeToLittle(T, x),
|
||||
.Big => nativeToBig(T, x),
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts an integer which has host endianness to little endian.
|
||||
pub fn nativeToLittle(comptime T: type, x: T) T {
|
||||
return switch (builtin.endian) {
|
||||
builtin.Endian.Little => x,
|
||||
builtin.Endian.Big => @byteSwap(T, x),
|
||||
.Little => x,
|
||||
.Big => @byteSwap(T, x),
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts an integer which has host endianness to big endian.
|
||||
pub fn nativeToBig(comptime T: type, x: T) T {
|
||||
return switch (builtin.endian) {
|
||||
builtin.Endian.Little => @byteSwap(T, x),
|
||||
builtin.Endian.Big => x,
|
||||
.Little => @byteSwap(T, x),
|
||||
.Big => x,
|
||||
};
|
||||
}
|
||||
|
||||
fn AsBytesReturnType(comptime P: type) type {
|
||||
if (comptime !trait.isSingleItemPtr(P))
|
||||
@compileError("expected single item " ++ "pointer, passed " ++ @typeName(P));
|
||||
@compileError("expected single item pointer, passed " ++ @typeName(P));
|
||||
|
||||
const size = @as(usize, @sizeOf(meta.Child(P)));
|
||||
const alignment = comptime meta.alignment(P);
|
||||
@ -1578,8 +1578,8 @@ pub fn asBytes(ptr: var) AsBytesReturnType(@TypeOf(ptr)) {
|
||||
test "asBytes" {
|
||||
const deadbeef = @as(u32, 0xDEADBEEF);
|
||||
const deadbeef_bytes = switch (builtin.endian) {
|
||||
builtin.Endian.Big => "\xDE\xAD\xBE\xEF",
|
||||
builtin.Endian.Little => "\xEF\xBE\xAD\xDE",
|
||||
.Big => "\xDE\xAD\xBE\xEF",
|
||||
.Little => "\xEF\xBE\xAD\xDE",
|
||||
};
|
||||
|
||||
testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes));
|
||||
@ -1617,21 +1617,21 @@ pub fn toBytes(value: var) [@sizeOf(@TypeOf(value))]u8 {
|
||||
test "toBytes" {
|
||||
var my_bytes = toBytes(@as(u32, 0x12345678));
|
||||
switch (builtin.endian) {
|
||||
builtin.Endian.Big => testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),
|
||||
builtin.Endian.Little => testing.expect(eql(u8, &my_bytes, "\x78\x56\x34\x12")),
|
||||
.Big => testing.expect(eql(u8, &my_bytes, "\x12\x34\x56\x78")),
|
||||
.Little => testing.expect(eql(u8, &my_bytes, "\x78\x56\x34\x12")),
|
||||
}
|
||||
|
||||
my_bytes[0] = '\x99';
|
||||
switch (builtin.endian) {
|
||||
builtin.Endian.Big => testing.expect(eql(u8, &my_bytes, "\x99\x34\x56\x78")),
|
||||
builtin.Endian.Little => testing.expect(eql(u8, &my_bytes, "\x99\x56\x34\x12")),
|
||||
.Big => testing.expect(eql(u8, &my_bytes, "\x99\x34\x56\x78")),
|
||||
.Little => testing.expect(eql(u8, &my_bytes, "\x99\x56\x34\x12")),
|
||||
}
|
||||
}
|
||||
|
||||
fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
|
||||
const size = @as(usize, @sizeOf(T));
|
||||
|
||||
if (comptime !trait.is(builtin.TypeId.Pointer)(B) or
|
||||
if (comptime !trait.is(.Pointer)(B) or
|
||||
(meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8))
|
||||
{
|
||||
@compileError("expected *[N]u8 " ++ ", passed " ++ @typeName(B));
|
||||
@ -1651,15 +1651,15 @@ pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @Typ
|
||||
test "bytesAsValue" {
|
||||
const deadbeef = @as(u32, 0xDEADBEEF);
|
||||
const deadbeef_bytes = switch (builtin.endian) {
|
||||
builtin.Endian.Big => "\xDE\xAD\xBE\xEF",
|
||||
builtin.Endian.Little => "\xEF\xBE\xAD\xDE",
|
||||
.Big => "\xDE\xAD\xBE\xEF",
|
||||
.Little => "\xEF\xBE\xAD\xDE",
|
||||
};
|
||||
|
||||
testing.expect(deadbeef == bytesAsValue(u32, deadbeef_bytes).*);
|
||||
|
||||
var codeface_bytes: [4]u8 = switch (builtin.endian) {
|
||||
builtin.Endian.Big => "\xC0\xDE\xFA\xCE",
|
||||
builtin.Endian.Little => "\xCE\xFA\xDE\xC0",
|
||||
.Big => "\xC0\xDE\xFA\xCE",
|
||||
.Little => "\xCE\xFA\xDE\xC0",
|
||||
}.*;
|
||||
var codeface = bytesAsValue(u32, &codeface_bytes);
|
||||
testing.expect(codeface.* == 0xC0DEFACE);
|
||||
@ -1692,8 +1692,8 @@ pub fn bytesToValue(comptime T: type, bytes: var) T {
|
||||
}
|
||||
test "bytesToValue" {
|
||||
const deadbeef_bytes = switch (builtin.endian) {
|
||||
builtin.Endian.Big => "\xDE\xAD\xBE\xEF",
|
||||
builtin.Endian.Little => "\xEF\xBE\xAD\xDE",
|
||||
.Big => "\xDE\xAD\xBE\xEF",
|
||||
.Little => "\xEF\xBE\xAD\xDE",
|
||||
};
|
||||
|
||||
const deadbeef = bytesToValue(u32, deadbeef_bytes);
|
||||
|
||||
@ -24,7 +24,7 @@ const twords = extern union {
|
||||
all: i128,
|
||||
s: S,
|
||||
|
||||
const S = if (builtin.endian == builtin.Endian.Little)
|
||||
const S = if (builtin.endian == .Little)
|
||||
struct {
|
||||
low: u64,
|
||||
high: u64,
|
||||
|
||||
@ -25,7 +25,7 @@ const twords = extern union {
|
||||
all: i128,
|
||||
s: S,
|
||||
|
||||
const S = if (builtin.endian == builtin.Endian.Little)
|
||||
const S = if (builtin.endian == .Little)
|
||||
struct {
|
||||
low: i64,
|
||||
high: i64,
|
||||
|
||||
@ -24,7 +24,7 @@ const twords = extern union {
|
||||
all: i128,
|
||||
s: S,
|
||||
|
||||
const S = if (builtin.endian == builtin.Endian.Little)
|
||||
const S = if (builtin.endian == .Little)
|
||||
struct {
|
||||
low: u64,
|
||||
high: u64,
|
||||
|
||||
@ -45,7 +45,7 @@ const twords = extern union {
|
||||
all: i128,
|
||||
s: S,
|
||||
|
||||
const S = if (builtin.endian == builtin.Endian.Little)
|
||||
const S = if (builtin.endian == .Little)
|
||||
struct {
|
||||
low: u64,
|
||||
high: u64,
|
||||
|
||||
@ -2,8 +2,8 @@ const builtin = @import("builtin");
|
||||
const is_test = builtin.is_test;
|
||||
|
||||
const low = switch (builtin.endian) {
|
||||
builtin.Endian.Big => 1,
|
||||
builtin.Endian.Little => 0,
|
||||
.Big => 1,
|
||||
.Little => 0,
|
||||
};
|
||||
const high = 1 - low;
|
||||
|
||||
|
||||
@ -63,15 +63,11 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
|
||||
|
||||
.Pointer => |pointer| {
|
||||
switch (pointer.size) {
|
||||
.One,
|
||||
.Many,
|
||||
.C,
|
||||
=> {
|
||||
.One, .Many, .C => {
|
||||
if (actual != expected) {
|
||||
std.debug.panic("expected {*}, found {*}", .{ expected, actual });
|
||||
}
|
||||
},
|
||||
|
||||
.Slice => {
|
||||
if (actual.ptr != expected.ptr) {
|
||||
std.debug.panic("expected slice ptr {}, found {}", .{ expected.ptr, actual.ptr });
|
||||
|
||||
@ -8,7 +8,7 @@ pub fn doClientRequest(default: usize, request: usize, a1: usize, a2: usize, a3:
|
||||
}
|
||||
|
||||
switch (builtin.arch) {
|
||||
builtin.Arch.i386 => {
|
||||
.i386 => {
|
||||
return asm volatile (
|
||||
\\ roll $3, %%edi ; roll $13, %%edi
|
||||
\\ roll $29, %%edi ; roll $19, %%edi
|
||||
@ -19,7 +19,7 @@ pub fn doClientRequest(default: usize, request: usize, a1: usize, a2: usize, a3:
|
||||
: "cc", "memory"
|
||||
);
|
||||
},
|
||||
builtin.Arch.x86_64 => {
|
||||
.x86_64 => {
|
||||
return asm volatile (
|
||||
\\ rolq $3, %%rdi ; rolq $13, %%rdi
|
||||
\\ rolq $61, %%rdi ; rolq $51, %%rdi
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user