| author | |
| committer | |
| log | a966275e509670e750ef54a37f7202078aa4cf07 |
| tree | abd0b5da3a788fea0a82908370685a42d61c531e |
| parent | 67e6d9bc30dabc295262b1aee8fe5eb044ba5cdc |
See #30710 files changed, 117 insertions(+), 96 deletions(-)
example/guess_number/main.zig+2-1| ... | ... | @@ -1,3 +1,4 @@ |
| 1 | const builtin = @import("builtin"); | |
| 1 | 2 | const std = @import("std"); |
| 2 | 3 | const io = std.io; |
| 3 | 4 | const fmt = std.fmt; |
| ... | ... | @@ -15,7 +16,7 @@ pub fn main() -> %void { |
| 15 | 16 | |
| 16 | 17 | var seed_bytes: [@sizeOf(usize)]u8 = undefined; |
| 17 | 18 | %%os.getRandomBytes(seed_bytes[0..]); |
| 18 | const seed = std.mem.readInt(seed_bytes, usize, true); | |
| 19 | const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big); | |
| 19 | 20 | var rand = Rand.init(seed); |
| 20 | 21 | |
| 21 | 22 | const answer = rand.range(u8, 0, 100) + 1; |
src/codegen.cpp+13-1| ... | ... | @@ -5129,7 +5129,19 @@ static void define_builtin_compile_vars(CodeGen *g) { |
| 5129 | 5129 | assert(FloatModeOptimized == 0); |
| 5130 | 5130 | assert(FloatModeStrict == 1); |
| 5131 | 5131 | } |
| 5132 | buf_appendf(contents, "pub const is_big_endian = %s;\n", bool_to_str(g->is_big_endian)); | |
| 5132 | { | |
| 5133 | buf_appendf(contents, | |
| 5134 | "pub const Endian = enum {\n" | |
| 5135 | " Big,\n" | |
| 5136 | " Little,\n" | |
| 5137 | "};\n\n"); | |
| 5138 | assert(FloatModeOptimized == 0); | |
| 5139 | assert(FloatModeStrict == 1); | |
| 5140 | } | |
| 5141 | { | |
| 5142 | const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; | |
| 5143 | buf_appendf(contents, "pub const endian = %s;\n", endian_str); | |
| 5144 | } | |
| 5133 | 5145 | buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build)); |
| 5134 | 5146 | buf_appendf(contents, "pub const os = Os.%s;\n", cur_os); |
| 5135 | 5147 | buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch); |
std/debug.zig+9-9| ... | ... | @@ -303,7 +303,7 @@ const Constant = struct { |
| 303 | 303 | return error.InvalidDebugInfo; |
| 304 | 304 | if (self.signed) |
| 305 | 305 | return error.InvalidDebugInfo; |
| 306 | return mem.readInt(self.payload, u64, false); | |
| 306 | return mem.readInt(self.payload, u64, builtin.Endian.Little); | |
| 307 | 307 | } |
| 308 | 308 | }; |
| 309 | 309 | |
| ... | ... | @@ -479,7 +479,7 @@ fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, si |
| 479 | 479 | } |
| 480 | 480 | |
| 481 | 481 | fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { |
| 482 | const block_len = %return in_stream.readVarInt(false, usize, size); | |
| 482 | const block_len = %return in_stream.readVarInt(builtin.Endian.Little, usize, size); | |
| 483 | 483 | return parseFormValueBlockLen(allocator, in_stream, block_len); |
| 484 | 484 | } |
| 485 | 485 | |
| ... | ... | @@ -669,10 +669,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 669 | 669 | continue; |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | const version = %return in_stream.readInt(st.elf.is_big_endian, u16); | |
| 672 | const version = %return in_stream.readInt(st.elf.endian, u16); | |
| 673 | 673 | if (version != 2) return error.InvalidDebugInfo; |
| 674 | 674 | |
| 675 | const prologue_length = %return in_stream.readInt(st.elf.is_big_endian, u32); | |
| 675 | const prologue_length = %return in_stream.readInt(st.elf.endian, u32); | |
| 676 | 676 | const prog_start_offset = (%return in_file.getPos()) + prologue_length; |
| 677 | 677 | |
| 678 | 678 | const minimum_instruction_length = %return in_stream.readByte(); |
| ... | ... | @@ -739,7 +739,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 739 | 739 | return error.MissingDebugInfo; |
| 740 | 740 | }, |
| 741 | 741 | DW.LNE_set_address => { |
| 742 | const addr = %return in_stream.readInt(st.elf.is_big_endian, usize); | |
| 742 | const addr = %return in_stream.readInt(st.elf.endian, usize); | |
| 743 | 743 | prog.address = addr; |
| 744 | 744 | }, |
| 745 | 745 | DW.LNE_define_file => { |
| ... | ... | @@ -801,7 +801,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe |
| 801 | 801 | prog.address += inc_addr; |
| 802 | 802 | }, |
| 803 | 803 | DW.LNS_fixed_advance_pc => { |
| 804 | const arg = %return in_stream.readInt(st.elf.is_big_endian, u16); | |
| 804 | const arg = %return in_stream.readInt(st.elf.endian, u16); | |
| 805 | 805 | prog.address += arg; |
| 806 | 806 | }, |
| 807 | 807 | DW.LNS_set_prologue_end => { |
| ... | ... | @@ -839,13 +839,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 839 | 839 | return; |
| 840 | 840 | const next_offset = unit_length + (if (is_64) usize(12) else usize(4)); |
| 841 | 841 | |
| 842 | const version = %return in_stream.readInt(st.elf.is_big_endian, u16); | |
| 842 | const version = %return in_stream.readInt(st.elf.endian, u16); | |
| 843 | 843 | if (version < 2 or version > 5) return error.InvalidDebugInfo; |
| 844 | 844 | |
| 845 | 845 | const debug_abbrev_offset = if (is_64) { |
| 846 | %return in_stream.readInt(st.elf.is_big_endian, u64) | |
| 846 | %return in_stream.readInt(st.elf.endian, u64) | |
| 847 | 847 | } else { |
| 848 | %return in_stream.readInt(st.elf.is_big_endian, u32) | |
| 848 | %return in_stream.readInt(st.elf.endian, u32) | |
| 849 | 849 | }; |
| 850 | 850 | |
| 851 | 851 | const address_size = %return in_stream.readByte(); |
std/elf.zig+40-39| ... | ... | @@ -1,3 +1,4 @@ |
| 1 | const builtin = @import("builtin"); | |
| 1 | 2 | const std = @import("index.zig"); |
| 2 | 3 | const io = std.io; |
| 3 | 4 | const math = std.math; |
| ... | ... | @@ -67,7 +68,7 @@ pub const Elf = struct { |
| 67 | 68 | in_file: &io.File, |
| 68 | 69 | auto_close_stream: bool, |
| 69 | 70 | is_64: bool, |
| 70 | is_big_endian: bool, | |
| 71 | endian: builtin.Endian, | |
| 71 | 72 | file_type: FileType, |
| 72 | 73 | arch: Arch, |
| 73 | 74 | entry_addr: u64, |
| ... | ... | @@ -105,9 +106,9 @@ pub const Elf = struct { |
| 105 | 106 | else => return error.InvalidFormat, |
| 106 | 107 | }; |
| 107 | 108 | |
| 108 | elf.is_big_endian = switch (%return in.readByte()) { | |
| 109 | 1 => false, | |
| 110 | 2 => true, | |
| 109 | elf.endian = switch (%return in.readByte()) { | |
| 110 | 1 => builtin.Endian.Little, | |
| 111 | 2 => builtin.Endian.Big, | |
| 111 | 112 | else => return error.InvalidFormat, |
| 112 | 113 | }; |
| 113 | 114 | |
| ... | ... | @@ -117,7 +118,7 @@ pub const Elf = struct { |
| 117 | 118 | // skip over padding |
| 118 | 119 | %return elf.in_file.seekForward(9); |
| 119 | 120 | |
| 120 | elf.file_type = switch (%return in.readInt(elf.is_big_endian, u16)) { | |
| 121 | elf.file_type = switch (%return in.readInt(elf.endian, u16)) { | |
| 121 | 122 | 1 => FileType.Relocatable, |
| 122 | 123 | 2 => FileType.Executable, |
| 123 | 124 | 3 => FileType.Shared, |
| ... | ... | @@ -125,7 +126,7 @@ pub const Elf = struct { |
| 125 | 126 | else => return error.InvalidFormat, |
| 126 | 127 | }; |
| 127 | 128 | |
| 128 | elf.arch = switch (%return in.readInt(elf.is_big_endian, u16)) { | |
| 129 | elf.arch = switch (%return in.readInt(elf.endian, u16)) { | |
| 129 | 130 | 0x02 => Arch.Sparc, |
| 130 | 131 | 0x03 => Arch.x86, |
| 131 | 132 | 0x08 => Arch.Mips, |
| ... | ... | @@ -138,34 +139,34 @@ pub const Elf = struct { |
| 138 | 139 | else => return error.InvalidFormat, |
| 139 | 140 | }; |
| 140 | 141 | |
| 141 | const elf_version = %return in.readInt(elf.is_big_endian, u32); | |
| 142 | const elf_version = %return in.readInt(elf.endian, u32); | |
| 142 | 143 | if (elf_version != 1) return error.InvalidFormat; |
| 143 | 144 | |
| 144 | 145 | if (elf.is_64) { |
| 145 | elf.entry_addr = %return in.readInt(elf.is_big_endian, u64); | |
| 146 | elf.program_header_offset = %return in.readInt(elf.is_big_endian, u64); | |
| 147 | elf.section_header_offset = %return in.readInt(elf.is_big_endian, u64); | |
| 146 | elf.entry_addr = %return in.readInt(elf.endian, u64); | |
| 147 | elf.program_header_offset = %return in.readInt(elf.endian, u64); | |
| 148 | elf.section_header_offset = %return in.readInt(elf.endian, u64); | |
| 148 | 149 | } else { |
| 149 | elf.entry_addr = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 150 | elf.program_header_offset = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 151 | elf.section_header_offset = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 150 | elf.entry_addr = u64(%return in.readInt(elf.endian, u32)); | |
| 151 | elf.program_header_offset = u64(%return in.readInt(elf.endian, u32)); | |
| 152 | elf.section_header_offset = u64(%return in.readInt(elf.endian, u32)); | |
| 152 | 153 | } |
| 153 | 154 | |
| 154 | 155 | // skip over flags |
| 155 | 156 | %return elf.in_file.seekForward(4); |
| 156 | 157 | |
| 157 | const header_size = %return in.readInt(elf.is_big_endian, u16); | |
| 158 | const header_size = %return in.readInt(elf.endian, u16); | |
| 158 | 159 | if ((elf.is_64 and header_size != 64) or |
| 159 | 160 | (!elf.is_64 and header_size != 52)) |
| 160 | 161 | { |
| 161 | 162 | return error.InvalidFormat; |
| 162 | 163 | } |
| 163 | 164 | |
| 164 | const ph_entry_size = %return in.readInt(elf.is_big_endian, u16); | |
| 165 | const ph_entry_count = %return in.readInt(elf.is_big_endian, u16); | |
| 166 | const sh_entry_size = %return in.readInt(elf.is_big_endian, u16); | |
| 167 | const sh_entry_count = %return in.readInt(elf.is_big_endian, u16); | |
| 168 | elf.string_section_index = u64(%return in.readInt(elf.is_big_endian, u16)); | |
| 165 | const ph_entry_size = %return in.readInt(elf.endian, u16); | |
| 166 | const ph_entry_count = %return in.readInt(elf.endian, u16); | |
| 167 | const sh_entry_size = %return in.readInt(elf.endian, u16); | |
| 168 | const sh_entry_count = %return in.readInt(elf.endian, u16); | |
| 169 | elf.string_section_index = u64(%return in.readInt(elf.endian, u16)); | |
| 169 | 170 | |
| 170 | 171 | if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat; |
| 171 | 172 | |
| ... | ... | @@ -188,32 +189,32 @@ pub const Elf = struct { |
| 188 | 189 | if (sh_entry_size != 64) return error.InvalidFormat; |
| 189 | 190 | |
| 190 | 191 | for (elf.section_headers) |*section| { |
| 191 | section.name = %return in.readInt(elf.is_big_endian, u32); | |
| 192 | section.sh_type = %return in.readInt(elf.is_big_endian, u32); | |
| 193 | section.flags = %return in.readInt(elf.is_big_endian, u64); | |
| 194 | section.addr = %return in.readInt(elf.is_big_endian, u64); | |
| 195 | section.offset = %return in.readInt(elf.is_big_endian, u64); | |
| 196 | section.size = %return in.readInt(elf.is_big_endian, u64); | |
| 197 | section.link = %return in.readInt(elf.is_big_endian, u32); | |
| 198 | section.info = %return in.readInt(elf.is_big_endian, u32); | |
| 199 | section.addr_align = %return in.readInt(elf.is_big_endian, u64); | |
| 200 | section.ent_size = %return in.readInt(elf.is_big_endian, u64); | |
| 192 | section.name = %return in.readInt(elf.endian, u32); | |
| 193 | section.sh_type = %return in.readInt(elf.endian, u32); | |
| 194 | section.flags = %return in.readInt(elf.endian, u64); | |
| 195 | section.addr = %return in.readInt(elf.endian, u64); | |
| 196 | section.offset = %return in.readInt(elf.endian, u64); | |
| 197 | section.size = %return in.readInt(elf.endian, u64); | |
| 198 | section.link = %return in.readInt(elf.endian, u32); | |
| 199 | section.info = %return in.readInt(elf.endian, u32); | |
| 200 | section.addr_align = %return in.readInt(elf.endian, u64); | |
| 201 | section.ent_size = %return in.readInt(elf.endian, u64); | |
| 201 | 202 | } |
| 202 | 203 | } else { |
| 203 | 204 | if (sh_entry_size != 40) return error.InvalidFormat; |
| 204 | 205 | |
| 205 | 206 | for (elf.section_headers) |*section| { |
| 206 | 207 | // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ? |
| 207 | section.name = %return in.readInt(elf.is_big_endian, u32); | |
| 208 | section.sh_type = %return in.readInt(elf.is_big_endian, u32); | |
| 209 | section.flags = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 210 | section.addr = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 211 | section.offset = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 212 | section.size = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 213 | section.link = %return in.readInt(elf.is_big_endian, u32); | |
| 214 | section.info = %return in.readInt(elf.is_big_endian, u32); | |
| 215 | section.addr_align = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 216 | section.ent_size = u64(%return in.readInt(elf.is_big_endian, u32)); | |
| 208 | section.name = %return in.readInt(elf.endian, u32); | |
| 209 | section.sh_type = %return in.readInt(elf.endian, u32); | |
| 210 | section.flags = u64(%return in.readInt(elf.endian, u32)); | |
| 211 | section.addr = u64(%return in.readInt(elf.endian, u32)); | |
| 212 | section.offset = u64(%return in.readInt(elf.endian, u32)); | |
| 213 | section.size = u64(%return in.readInt(elf.endian, u32)); | |
| 214 | section.link = %return in.readInt(elf.endian, u32); | |
| 215 | section.info = %return in.readInt(elf.endian, u32); | |
| 216 | section.addr_align = u64(%return in.readInt(elf.endian, u32)); | |
| 217 | section.ent_size = u64(%return in.readInt(elf.endian, u32)); | |
| 217 | 218 | } |
| 218 | 219 | } |
| 219 | 220 |
std/endian.zig+2-2| ... | ... | @@ -9,8 +9,8 @@ pub fn swapIfBe(comptime T: type, x: T) -> T { |
| 9 | 9 | swapIf(true, T, x) |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T { | |
| 13 | if (builtin.is_big_endian == is_be) swap(T, x) else x | |
| 12 | pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T { | |
| 13 | if (builtin.endian == endian) swap(T, x) else x | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | pub fn swap(comptime T: type, x: T) -> T { |
std/io.zig+6-6| ... | ... | @@ -441,26 +441,26 @@ pub const InStream = struct { |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | pub fn readIntLe(self: &InStream, comptime T: type) -> %T { |
| 444 | return self.readInt(false, T); | |
| 444 | return self.readInt(builtin.Endian.Little, T); | |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | pub fn readIntBe(self: &InStream, comptime T: type) -> %T { |
| 448 | return self.readInt(true, T); | |
| 448 | return self.readInt(builtin.Endian.Big, T); | |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | pub fn readInt(self: &InStream, is_be: bool, comptime T: type) -> %T { | |
| 451 | pub fn readInt(self: &InStream, endian: builtin.Endian, comptime T: type) -> %T { | |
| 452 | 452 | var bytes: [@sizeOf(T)]u8 = undefined; |
| 453 | 453 | %return self.readNoEof(bytes[0..]); |
| 454 | return mem.readInt(bytes, T, is_be); | |
| 454 | return mem.readInt(bytes, T, endian); | |
| 455 | 455 | } |
| 456 | 456 | |
| 457 | pub fn readVarInt(self: &InStream, is_be: bool, comptime T: type, size: usize) -> %T { | |
| 457 | pub fn readVarInt(self: &InStream, endian: builtin.Endian, comptime T: type, size: usize) -> %T { | |
| 458 | 458 | assert(size <= @sizeOf(T)); |
| 459 | 459 | assert(size <= 8); |
| 460 | 460 | var input_buf: [8]u8 = undefined; |
| 461 | 461 | const input_slice = input_buf[0..size]; |
| 462 | 462 | %return self.readNoEof(input_slice); |
| 463 | return mem.readInt(input_slice, T, is_be); | |
| 463 | return mem.readInt(input_slice, T, endian); | |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 |
std/mem.zig+39-32| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | const debug = @import("debug.zig"); |
| 2 | 2 | const assert = debug.assert; |
| 3 | 3 | const math = @import("math/index.zig"); |
| 4 | const builtin = @import("builtin"); | |
| 4 | 5 | |
| 5 | 6 | pub const Cmp = math.Cmp; |
| 6 | 7 | |
| ... | ... | @@ -181,20 +182,23 @@ test "mem.indexOf" { |
| 181 | 182 | /// T specifies the return type, which must be large enough to store |
| 182 | 183 | /// the result. |
| 183 | 184 | /// See also ::readIntBE or ::readIntLE. |
| 184 | pub fn readInt(bytes: []const u8, comptime T: type, big_endian: bool) -> T { | |
| 185 | pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) -> T { | |
| 185 | 186 | if (T.bit_count == 8) { |
| 186 | 187 | return bytes[0]; |
| 187 | 188 | } |
| 188 | 189 | var result: T = 0; |
| 189 | if (big_endian) { | |
| 190 | for (bytes) |b| { | |
| 191 | result = (result << 8) | b; | |
| 192 | } | |
| 193 | } else { | |
| 194 | const ShiftType = math.Log2Int(T); | |
| 195 | for (bytes) |b, index| { | |
| 196 | result = result | (T(b) << ShiftType(index * 8)); | |
| 197 | } | |
| 190 | switch (endian) { | |
| 191 | builtin.Endian.Big => { | |
| 192 | for (bytes) |b| { | |
| 193 | result = (result << 8) | b; | |
| 194 | } | |
| 195 | }, | |
| 196 | builtin.Endian.Little => { | |
| 197 | const ShiftType = math.Log2Int(T); | |
| 198 | for (bytes) |b, index| { | |
| 199 | result = result | (T(b) << ShiftType(index * 8)); | |
| 200 | } | |
| 201 | }, | |
| 198 | 202 | } |
| 199 | 203 | return result; |
| 200 | 204 | } |
| ... | ... | @@ -230,22 +234,25 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) -> T { |
| 230 | 234 | /// Writes an integer to memory with size equal to bytes.len. Pads with zeroes |
| 231 | 235 | /// to fill the entire buffer provided. |
| 232 | 236 | /// value must be an integer. |
| 233 | pub fn writeInt(buf: []u8, value: var, big_endian: bool) { | |
| 237 | pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) { | |
| 234 | 238 | const uint = @IntType(false, @typeOf(value).bit_count); |
| 235 | 239 | var bits = @truncate(uint, value); |
| 236 | if (big_endian) { | |
| 237 | var index: usize = buf.len; | |
| 238 | while (index != 0) { | |
| 239 | index -= 1; | |
| 240 | ||
| 241 | buf[index] = @truncate(u8, bits); | |
| 242 | bits >>= 8; | |
| 243 | } | |
| 244 | } else { | |
| 245 | for (buf) |*b| { | |
| 246 | *b = @truncate(u8, bits); | |
| 247 | bits >>= 8; | |
| 248 | } | |
| 240 | switch (endian) { | |
| 241 | builtin.Endian.Big => { | |
| 242 | var index: usize = buf.len; | |
| 243 | while (index != 0) { | |
| 244 | index -= 1; | |
| 245 | ||
| 246 | buf[index] = @truncate(u8, bits); | |
| 247 | bits >>= 8; | |
| 248 | } | |
| 249 | }, | |
| 250 | builtin.Endian.Little => { | |
| 251 | for (buf) |*b| { | |
| 252 | *b = @truncate(u8, bits); | |
| 253 | bits >>= 8; | |
| 254 | } | |
| 255 | }, | |
| 249 | 256 | } |
| 250 | 257 | assert(bits == 0); |
| 251 | 258 | } |
| ... | ... | @@ -377,21 +384,21 @@ test "testReadInt" { |
| 377 | 384 | fn testReadIntImpl() { |
| 378 | 385 | { |
| 379 | 386 | const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 }; |
| 380 | assert(readInt(bytes, u32, true) == 0x12345678); | |
| 387 | assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678); | |
| 381 | 388 | assert(readIntBE(u32, bytes) == 0x12345678); |
| 382 | 389 | assert(readIntBE(i32, bytes) == 0x12345678); |
| 383 | assert(readInt(bytes, u32, false) == 0x78563412); | |
| 390 | assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412); | |
| 384 | 391 | assert(readIntLE(u32, bytes) == 0x78563412); |
| 385 | 392 | assert(readIntLE(i32, bytes) == 0x78563412); |
| 386 | 393 | } |
| 387 | 394 | { |
| 388 | 395 | const buf = []u8{0x00, 0x00, 0x12, 0x34}; |
| 389 | const answer = readInt(buf, u64, true); | |
| 396 | const answer = readInt(buf, u64, builtin.Endian.Big); | |
| 390 | 397 | assert(answer == 0x00001234); |
| 391 | 398 | } |
| 392 | 399 | { |
| 393 | 400 | const buf = []u8{0x12, 0x34, 0x00, 0x00}; |
| 394 | const answer = readInt(buf, u64, false); | |
| 401 | const answer = readInt(buf, u64, builtin.Endian.Little); | |
| 395 | 402 | assert(answer == 0x00003412); |
| 396 | 403 | } |
| 397 | 404 | { |
| ... | ... | @@ -410,16 +417,16 @@ test "testWriteInt" { |
| 410 | 417 | fn testWriteIntImpl() { |
| 411 | 418 | var bytes: [4]u8 = undefined; |
| 412 | 419 | |
| 413 | writeInt(bytes[0..], u32(0x12345678), true); | |
| 420 | writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big); | |
| 414 | 421 | assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 })); |
| 415 | 422 | |
| 416 | writeInt(bytes[0..], u32(0x78563412), false); | |
| 423 | writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little); | |
| 417 | 424 | assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 })); |
| 418 | 425 | |
| 419 | writeInt(bytes[0..], u16(0x1234), true); | |
| 426 | writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big); | |
| 420 | 427 | assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 })); |
| 421 | 428 | |
| 422 | writeInt(bytes[0..], u16(0x1234), false); | |
| 429 | writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little); | |
| 423 | 430 | assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 })); |
| 424 | 431 | } |
| 425 | 432 |
std/os/child_process.zig+2-2| ... | ... | @@ -722,14 +722,14 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8); |
| 722 | 722 | |
| 723 | 723 | fn writeIntFd(fd: i32, value: ErrInt) -> %void { |
| 724 | 724 | var bytes: [@sizeOf(ErrInt)]u8 = undefined; |
| 725 | mem.writeInt(bytes[0..], value, true); | |
| 725 | mem.writeInt(bytes[0..], value, builtin.endian); | |
| 726 | 726 | os.posixWrite(fd, bytes[0..]) %% return error.SystemResources; |
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | fn readIntFd(fd: i32) -> %ErrInt { |
| 730 | 730 | var bytes: [@sizeOf(ErrInt)]u8 = undefined; |
| 731 | 731 | os.posixRead(fd, bytes[0..]) %% return error.SystemResources; |
| 732 | return mem.readInt(bytes[0..], ErrInt, true); | |
| 732 | return mem.readInt(bytes[0..], ErrInt, builtin.endian); | |
| 733 | 733 | } |
| 734 | 734 | |
| 735 | 735 | extern fn sigchld_handler(_: i32) { |
std/rand.zig+3-3| ... | ... | @@ -50,12 +50,12 @@ pub const Rand = struct { |
| 50 | 50 | pub fn fillBytes(r: &Rand, buf: []u8) { |
| 51 | 51 | var bytes_left = buf.len; |
| 52 | 52 | while (bytes_left >= @sizeOf(usize)) { |
| 53 | mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), false); | |
| 53 | mem.writeInt(buf[buf.len - bytes_left..], r.rng.get(), builtin.Endian.Little); | |
| 54 | 54 | bytes_left -= @sizeOf(usize); |
| 55 | 55 | } |
| 56 | 56 | if (bytes_left > 0) { |
| 57 | 57 | var rand_val_array: [@sizeOf(usize)]u8 = undefined; |
| 58 | mem.writeInt(rand_val_array[0..], r.rng.get(), false); | |
| 58 | mem.writeInt(rand_val_array[0..], r.rng.get(), builtin.Endian.Little); | |
| 59 | 59 | while (bytes_left > 0) { |
| 60 | 60 | buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left]; |
| 61 | 61 | bytes_left -= 1; |
| ... | ... | @@ -98,7 +98,7 @@ pub const Rand = struct { |
| 98 | 98 | |
| 99 | 99 | while (true) { |
| 100 | 100 | r.fillBytes(rand_val_array[0..]); |
| 101 | const rand_val = mem.readInt(rand_val_array, T, false); | |
| 101 | const rand_val = mem.readInt(rand_val_array, T, builtin.Endian.Little); | |
| 102 | 102 | if (rand_val < upper_bound) { |
| 103 | 103 | return start + (rand_val % total_range); |
| 104 | 104 | } |
std/special/compiler_rt/udivmod.zig+1-1| ... | ... | @@ -1,7 +1,7 @@ |
| 1 | 1 | const builtin = @import("builtin"); |
| 2 | 2 | const is_test = builtin.is_test; |
| 3 | 3 | |
| 4 | const low = if (builtin.is_big_endian) 1 else 0; | |
| 4 | const low = switch (builtin.endian) { builtin.Endian.Big => 1, builtin.Endian.Little => 0 }; | |
| 5 | 5 | const high = 1 - low; |
| 6 | 6 | |
| 7 | 7 | pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) -> DoubleInt { |