authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 10:35:55-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 10:36:31-05:00
loga966275e509670e750ef54a37f7202078aa4cf07
treeabd0b5da3a788fea0a82908370685a42d61c531e
parent67e6d9bc30dabc295262b1aee8fe5eb044ba5cdc

rename builtin.is_big_endian to builtin.endian

See #307

10 files changed, 117 insertions(+), 96 deletions(-)

example/guess_number/main.zig+2-1
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
1const std = @import("std");2const std = @import("std");
2const io = std.io;3const io = std.io;
3const fmt = std.fmt;4const fmt = std.fmt;
...@@ -15,7 +16,7 @@ pub fn main() -> %void {...@@ -15,7 +16,7 @@ pub fn main() -> %void {
1516
16 var seed_bytes: [@sizeOf(usize)]u8 = undefined;17 var seed_bytes: [@sizeOf(usize)]u8 = undefined;
17 %%os.getRandomBytes(seed_bytes[0..]);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 var rand = Rand.init(seed);20 var rand = Rand.init(seed);
2021
21 const answer = rand.range(u8, 0, 100) + 1;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,7 +5129,19 @@ static void define_builtin_compile_vars(CodeGen *g) {
5129 assert(FloatModeOptimized == 0);5129 assert(FloatModeOptimized == 0);
5130 assert(FloatModeStrict == 1);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 buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));5145 buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
5134 buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);5146 buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);
5135 buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch);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,7 +303,7 @@ const Constant = struct {
303 return error.InvalidDebugInfo;303 return error.InvalidDebugInfo;
304 if (self.signed)304 if (self.signed)
305 return error.InvalidDebugInfo;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};
309309
...@@ -479,7 +479,7 @@ fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, si...@@ -479,7 +479,7 @@ fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, si
479}479}
480480
481fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {481fn 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 return parseFormValueBlockLen(allocator, in_stream, block_len);483 return parseFormValueBlockLen(allocator, in_stream, block_len);
484}484}
485485
...@@ -669,10 +669,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe...@@ -669,10 +669,10 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
669 continue;669 continue;
670 }670 }
671671
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 if (version != 2) return error.InvalidDebugInfo;673 if (version != 2) return error.InvalidDebugInfo;
674674
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 const prog_start_offset = (%return in_file.getPos()) + prologue_length;676 const prog_start_offset = (%return in_file.getPos()) + prologue_length;
677677
678 const minimum_instruction_length = %return in_stream.readByte();678 const minimum_instruction_length = %return in_stream.readByte();
...@@ -739,7 +739,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe...@@ -739,7 +739,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
739 return error.MissingDebugInfo;739 return error.MissingDebugInfo;
740 },740 },
741 DW.LNE_set_address => {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 prog.address = addr;743 prog.address = addr;
744 },744 },
745 DW.LNE_define_file => {745 DW.LNE_define_file => {
...@@ -801,7 +801,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe...@@ -801,7 +801,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
801 prog.address += inc_addr;801 prog.address += inc_addr;
802 },802 },
803 DW.LNS_fixed_advance_pc => {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 prog.address += arg;805 prog.address += arg;
806 },806 },
807 DW.LNS_set_prologue_end => {807 DW.LNS_set_prologue_end => {
...@@ -839,13 +839,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {...@@ -839,13 +839,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
839 return;839 return;
840 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));840 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
841841
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 if (version < 2 or version > 5) return error.InvalidDebugInfo;843 if (version < 2 or version > 5) return error.InvalidDebugInfo;
844844
845 const debug_abbrev_offset = if (is_64) {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 } else {847 } else {
848 %return in_stream.readInt(st.elf.is_big_endian, u32)848 %return in_stream.readInt(st.elf.endian, u32)
849 };849 };
850850
851 const address_size = %return in_stream.readByte();851 const address_size = %return in_stream.readByte();
std/elf.zig+40-39
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
1const std = @import("index.zig");2const std = @import("index.zig");
2const io = std.io;3const io = std.io;
3const math = std.math;4const math = std.math;
...@@ -67,7 +68,7 @@ pub const Elf = struct {...@@ -67,7 +68,7 @@ pub const Elf = struct {
67 in_file: &io.File,68 in_file: &io.File,
68 auto_close_stream: bool,69 auto_close_stream: bool,
69 is_64: bool,70 is_64: bool,
70 is_big_endian: bool,71 endian: builtin.Endian,
71 file_type: FileType,72 file_type: FileType,
72 arch: Arch,73 arch: Arch,
73 entry_addr: u64,74 entry_addr: u64,
...@@ -105,9 +106,9 @@ pub const Elf = struct {...@@ -105,9 +106,9 @@ pub const Elf = struct {
105 else => return error.InvalidFormat,106 else => return error.InvalidFormat,
106 };107 };
107108
108 elf.is_big_endian = switch (%return in.readByte()) {109 elf.endian = switch (%return in.readByte()) {
109 1 => false,110 1 => builtin.Endian.Little,
110 2 => true,111 2 => builtin.Endian.Big,
111 else => return error.InvalidFormat,112 else => return error.InvalidFormat,
112 };113 };
113114
...@@ -117,7 +118,7 @@ pub const Elf = struct {...@@ -117,7 +118,7 @@ pub const Elf = struct {
117 // skip over padding118 // skip over padding
118 %return elf.in_file.seekForward(9);119 %return elf.in_file.seekForward(9);
119120
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 1 => FileType.Relocatable,122 1 => FileType.Relocatable,
122 2 => FileType.Executable,123 2 => FileType.Executable,
123 3 => FileType.Shared,124 3 => FileType.Shared,
...@@ -125,7 +126,7 @@ pub const Elf = struct {...@@ -125,7 +126,7 @@ pub const Elf = struct {
125 else => return error.InvalidFormat,126 else => return error.InvalidFormat,
126 };127 };
127128
128 elf.arch = switch (%return in.readInt(elf.is_big_endian, u16)) {129 elf.arch = switch (%return in.readInt(elf.endian, u16)) {
129 0x02 => Arch.Sparc,130 0x02 => Arch.Sparc,
130 0x03 => Arch.x86,131 0x03 => Arch.x86,
131 0x08 => Arch.Mips,132 0x08 => Arch.Mips,
...@@ -138,34 +139,34 @@ pub const Elf = struct {...@@ -138,34 +139,34 @@ pub const Elf = struct {
138 else => return error.InvalidFormat,139 else => return error.InvalidFormat,
139 };140 };
140141
141 const elf_version = %return in.readInt(elf.is_big_endian, u32);142 const elf_version = %return in.readInt(elf.endian, u32);
142 if (elf_version != 1) return error.InvalidFormat;143 if (elf_version != 1) return error.InvalidFormat;
143144
144 if (elf.is_64) {145 if (elf.is_64) {
145 elf.entry_addr = %return in.readInt(elf.is_big_endian, u64);146 elf.entry_addr = %return in.readInt(elf.endian, u64);
146 elf.program_header_offset = %return in.readInt(elf.is_big_endian, u64);147 elf.program_header_offset = %return in.readInt(elf.endian, u64);
147 elf.section_header_offset = %return in.readInt(elf.is_big_endian, u64);148 elf.section_header_offset = %return in.readInt(elf.endian, u64);
148 } else {149 } else {
149 elf.entry_addr = u64(%return in.readInt(elf.is_big_endian, u32));150 elf.entry_addr = u64(%return in.readInt(elf.endian, u32));
150 elf.program_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));151 elf.program_header_offset = u64(%return in.readInt(elf.endian, u32));
151 elf.section_header_offset = u64(%return in.readInt(elf.is_big_endian, u32));152 elf.section_header_offset = u64(%return in.readInt(elf.endian, u32));
152 }153 }
153154
154 // skip over flags155 // skip over flags
155 %return elf.in_file.seekForward(4);156 %return elf.in_file.seekForward(4);
156157
157 const header_size = %return in.readInt(elf.is_big_endian, u16);158 const header_size = %return in.readInt(elf.endian, u16);
158 if ((elf.is_64 and header_size != 64) or159 if ((elf.is_64 and header_size != 64) or
159 (!elf.is_64 and header_size != 52))160 (!elf.is_64 and header_size != 52))
160 {161 {
161 return error.InvalidFormat;162 return error.InvalidFormat;
162 }163 }
163164
164 const ph_entry_size = %return in.readInt(elf.is_big_endian, u16);165 const ph_entry_size = %return in.readInt(elf.endian, u16);
165 const ph_entry_count = %return in.readInt(elf.is_big_endian, u16);166 const ph_entry_count = %return in.readInt(elf.endian, u16);
166 const sh_entry_size = %return in.readInt(elf.is_big_endian, u16);167 const sh_entry_size = %return in.readInt(elf.endian, u16);
167 const sh_entry_count = %return in.readInt(elf.is_big_endian, u16);168 const sh_entry_count = %return in.readInt(elf.endian, u16);
168 elf.string_section_index = u64(%return in.readInt(elf.is_big_endian, u16));169 elf.string_section_index = u64(%return in.readInt(elf.endian, u16));
169170
170 if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat;171 if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat;
171172
...@@ -188,32 +189,32 @@ pub const Elf = struct {...@@ -188,32 +189,32 @@ pub const Elf = struct {
188 if (sh_entry_size != 64) return error.InvalidFormat;189 if (sh_entry_size != 64) return error.InvalidFormat;
189190
190 for (elf.section_headers) |*section| {191 for (elf.section_headers) |*section| {
191 section.name = %return in.readInt(elf.is_big_endian, u32);192 section.name = %return in.readInt(elf.endian, u32);
192 section.sh_type = %return in.readInt(elf.is_big_endian, u32);193 section.sh_type = %return in.readInt(elf.endian, u32);
193 section.flags = %return in.readInt(elf.is_big_endian, u64);194 section.flags = %return in.readInt(elf.endian, u64);
194 section.addr = %return in.readInt(elf.is_big_endian, u64);195 section.addr = %return in.readInt(elf.endian, u64);
195 section.offset = %return in.readInt(elf.is_big_endian, u64);196 section.offset = %return in.readInt(elf.endian, u64);
196 section.size = %return in.readInt(elf.is_big_endian, u64);197 section.size = %return in.readInt(elf.endian, u64);
197 section.link = %return in.readInt(elf.is_big_endian, u32);198 section.link = %return in.readInt(elf.endian, u32);
198 section.info = %return in.readInt(elf.is_big_endian, u32);199 section.info = %return in.readInt(elf.endian, u32);
199 section.addr_align = %return in.readInt(elf.is_big_endian, u64);200 section.addr_align = %return in.readInt(elf.endian, u64);
200 section.ent_size = %return in.readInt(elf.is_big_endian, u64);201 section.ent_size = %return in.readInt(elf.endian, u64);
201 }202 }
202 } else {203 } else {
203 if (sh_entry_size != 40) return error.InvalidFormat;204 if (sh_entry_size != 40) return error.InvalidFormat;
204205
205 for (elf.section_headers) |*section| {206 for (elf.section_headers) |*section| {
206 // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ?207 // TODO (multiple occurences) allow implicit cast from %u32 -> %u64 ?
207 section.name = %return in.readInt(elf.is_big_endian, u32);208 section.name = %return in.readInt(elf.endian, u32);
208 section.sh_type = %return in.readInt(elf.is_big_endian, u32);209 section.sh_type = %return in.readInt(elf.endian, u32);
209 section.flags = u64(%return in.readInt(elf.is_big_endian, u32));210 section.flags = u64(%return in.readInt(elf.endian, u32));
210 section.addr = u64(%return in.readInt(elf.is_big_endian, u32));211 section.addr = u64(%return in.readInt(elf.endian, u32));
211 section.offset = u64(%return in.readInt(elf.is_big_endian, u32));212 section.offset = u64(%return in.readInt(elf.endian, u32));
212 section.size = u64(%return in.readInt(elf.is_big_endian, u32));213 section.size = u64(%return in.readInt(elf.endian, u32));
213 section.link = %return in.readInt(elf.is_big_endian, u32);214 section.link = %return in.readInt(elf.endian, u32);
214 section.info = %return in.readInt(elf.is_big_endian, u32);215 section.info = %return in.readInt(elf.endian, u32);
215 section.addr_align = u64(%return in.readInt(elf.is_big_endian, u32));216 section.addr_align = u64(%return in.readInt(elf.endian, u32));
216 section.ent_size = u64(%return in.readInt(elf.is_big_endian, u32));217 section.ent_size = u64(%return in.readInt(elf.endian, u32));
217 }218 }
218 }219 }
219220
std/endian.zig+2-2
...@@ -9,8 +9,8 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {...@@ -9,8 +9,8 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {
9 swapIf(true, T, x)9 swapIf(true, T, x)
10}10}
1111
12pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {12pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T {
13 if (builtin.is_big_endian == is_be) swap(T, x) else x13 if (builtin.endian == endian) swap(T, x) else x
14}14}
1515
16pub fn swap(comptime T: type, x: T) -> T {16pub fn swap(comptime T: type, x: T) -> T {
std/io.zig+6-6
...@@ -441,26 +441,26 @@ pub const InStream = struct {...@@ -441,26 +441,26 @@ pub const InStream = struct {
441 }441 }
442442
443 pub fn readIntLe(self: &InStream, comptime T: type) -> %T {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 }
446446
447 pub fn readIntBe(self: &InStream, comptime T: type) -> %T {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 }
450450
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 var bytes: [@sizeOf(T)]u8 = undefined;452 var bytes: [@sizeOf(T)]u8 = undefined;
453 %return self.readNoEof(bytes[0..]);453 %return self.readNoEof(bytes[0..]);
454 return mem.readInt(bytes, T, is_be);454 return mem.readInt(bytes, T, endian);
455 }455 }
456456
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 assert(size <= @sizeOf(T));458 assert(size <= @sizeOf(T));
459 assert(size <= 8);459 assert(size <= 8);
460 var input_buf: [8]u8 = undefined;460 var input_buf: [8]u8 = undefined;
461 const input_slice = input_buf[0..size];461 const input_slice = input_buf[0..size];
462 %return self.readNoEof(input_slice);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 }
465465
466466
std/mem.zig+39-32
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const debug = @import("debug.zig");1const debug = @import("debug.zig");
2const assert = debug.assert;2const assert = debug.assert;
3const math = @import("math/index.zig");3const math = @import("math/index.zig");
4const builtin = @import("builtin");
45
5pub const Cmp = math.Cmp;6pub const Cmp = math.Cmp;
67
...@@ -181,20 +182,23 @@ test "mem.indexOf" {...@@ -181,20 +182,23 @@ test "mem.indexOf" {
181/// T specifies the return type, which must be large enough to store182/// T specifies the return type, which must be large enough to store
182/// the result.183/// the result.
183/// See also ::readIntBE or ::readIntLE.184/// See also ::readIntBE or ::readIntLE.
184pub fn readInt(bytes: []const u8, comptime T: type, big_endian: bool) -> T {185pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) -> T {
185 if (T.bit_count == 8) {186 if (T.bit_count == 8) {
186 return bytes[0];187 return bytes[0];
187 }188 }
188 var result: T = 0;189 var result: T = 0;
189 if (big_endian) {190 switch (endian) {
190 for (bytes) |b| {191 builtin.Endian.Big => {
191 result = (result << 8) | b;192 for (bytes) |b| {
192 }193 result = (result << 8) | b;
193 } else {194 }
194 const ShiftType = math.Log2Int(T);195 },
195 for (bytes) |b, index| {196 builtin.Endian.Little => {
196 result = result | (T(b) << ShiftType(index * 8));197 const ShiftType = math.Log2Int(T);
197 }198 for (bytes) |b, index| {
199 result = result | (T(b) << ShiftType(index * 8));
200 }
201 },
198 }202 }
199 return result;203 return result;
200}204}
...@@ -230,22 +234,25 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) -> T {...@@ -230,22 +234,25 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) -> T {
230/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes234/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes
231/// to fill the entire buffer provided.235/// to fill the entire buffer provided.
232/// value must be an integer.236/// value must be an integer.
233pub fn writeInt(buf: []u8, value: var, big_endian: bool) {237pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) {
234 const uint = @IntType(false, @typeOf(value).bit_count);238 const uint = @IntType(false, @typeOf(value).bit_count);
235 var bits = @truncate(uint, value);239 var bits = @truncate(uint, value);
236 if (big_endian) {240 switch (endian) {
237 var index: usize = buf.len;241 builtin.Endian.Big => {
238 while (index != 0) {242 var index: usize = buf.len;
239 index -= 1;243 while (index != 0) {
240244 index -= 1;
241 buf[index] = @truncate(u8, bits);245
242 bits >>= 8;246 buf[index] = @truncate(u8, bits);
243 }247 bits >>= 8;
244 } else {248 }
245 for (buf) |*b| {249 },
246 *b = @truncate(u8, bits);250 builtin.Endian.Little => {
247 bits >>= 8;251 for (buf) |*b| {
248 }252 *b = @truncate(u8, bits);
253 bits >>= 8;
254 }
255 },
249 }256 }
250 assert(bits == 0);257 assert(bits == 0);
251}258}
...@@ -377,21 +384,21 @@ test "testReadInt" {...@@ -377,21 +384,21 @@ test "testReadInt" {
377fn testReadIntImpl() {384fn testReadIntImpl() {
378 {385 {
379 const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };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 assert(readIntBE(u32, bytes) == 0x12345678);388 assert(readIntBE(u32, bytes) == 0x12345678);
382 assert(readIntBE(i32, bytes) == 0x12345678);389 assert(readIntBE(i32, bytes) == 0x12345678);
383 assert(readInt(bytes, u32, false) == 0x78563412);390 assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
384 assert(readIntLE(u32, bytes) == 0x78563412);391 assert(readIntLE(u32, bytes) == 0x78563412);
385 assert(readIntLE(i32, bytes) == 0x78563412);392 assert(readIntLE(i32, bytes) == 0x78563412);
386 }393 }
387 {394 {
388 const buf = []u8{0x00, 0x00, 0x12, 0x34};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 assert(answer == 0x00001234);397 assert(answer == 0x00001234);
391 }398 }
392 {399 {
393 const buf = []u8{0x12, 0x34, 0x00, 0x00};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 assert(answer == 0x00003412);402 assert(answer == 0x00003412);
396 }403 }
397 {404 {
...@@ -410,16 +417,16 @@ test "testWriteInt" {...@@ -410,16 +417,16 @@ test "testWriteInt" {
410fn testWriteIntImpl() {417fn testWriteIntImpl() {
411 var bytes: [4]u8 = undefined;418 var bytes: [4]u8 = undefined;
412419
413 writeInt(bytes[0..], u32(0x12345678), true);420 writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
414 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));421 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
415422
416 writeInt(bytes[0..], u32(0x78563412), false);423 writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
417 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));424 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
418425
419 writeInt(bytes[0..], u16(0x1234), true);426 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
420 assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));427 assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
421428
422 writeInt(bytes[0..], u16(0x1234), false);429 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
423 assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));430 assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
424}431}
425432
std/os/child_process.zig+2-2
...@@ -722,14 +722,14 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8);...@@ -722,14 +722,14 @@ const ErrInt = @IntType(false, @sizeOf(error) * 8);
722722
723fn writeIntFd(fd: i32, value: ErrInt) -> %void {723fn writeIntFd(fd: i32, value: ErrInt) -> %void {
724 var bytes: [@sizeOf(ErrInt)]u8 = undefined;724 var bytes: [@sizeOf(ErrInt)]u8 = undefined;
725 mem.writeInt(bytes[0..], value, true);725 mem.writeInt(bytes[0..], value, builtin.endian);
726 os.posixWrite(fd, bytes[0..]) %% return error.SystemResources;726 os.posixWrite(fd, bytes[0..]) %% return error.SystemResources;
727}727}
728728
729fn readIntFd(fd: i32) -> %ErrInt {729fn readIntFd(fd: i32) -> %ErrInt {
730 var bytes: [@sizeOf(ErrInt)]u8 = undefined;730 var bytes: [@sizeOf(ErrInt)]u8 = undefined;
731 os.posixRead(fd, bytes[0..]) %% return error.SystemResources;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}
734734
735extern fn sigchld_handler(_: i32) {735extern fn sigchld_handler(_: i32) {
std/rand.zig+3-3
...@@ -50,12 +50,12 @@ pub const Rand = struct {...@@ -50,12 +50,12 @@ pub const Rand = struct {
50 pub fn fillBytes(r: &Rand, buf: []u8) {50 pub fn fillBytes(r: &Rand, buf: []u8) {
51 var bytes_left = buf.len;51 var bytes_left = buf.len;
52 while (bytes_left >= @sizeOf(usize)) {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 bytes_left -= @sizeOf(usize);54 bytes_left -= @sizeOf(usize);
55 }55 }
56 if (bytes_left > 0) {56 if (bytes_left > 0) {
57 var rand_val_array: [@sizeOf(usize)]u8 = undefined;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 while (bytes_left > 0) {59 while (bytes_left > 0) {
60 buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];60 buf[buf.len - bytes_left] = rand_val_array[@sizeOf(usize) - bytes_left];
61 bytes_left -= 1;61 bytes_left -= 1;
...@@ -98,7 +98,7 @@ pub const Rand = struct {...@@ -98,7 +98,7 @@ pub const Rand = struct {
9898
99 while (true) {99 while (true) {
100 r.fillBytes(rand_val_array[0..]);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 if (rand_val < upper_bound) {102 if (rand_val < upper_bound) {
103 return start + (rand_val % total_range);103 return start + (rand_val % total_range);
104 }104 }
std/special/compiler_rt/udivmod.zig+1-1
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
33
4const low = if (builtin.is_big_endian) 1 else 0;4const low = switch (builtin.endian) { builtin.Endian.Big => 1, builtin.Endian.Little => 0 };
5const high = 1 - low;5const high = 1 - low;
66
7pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) -> DoubleInt {7pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?&DoubleInt) -> DoubleInt {