authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-12 20:19:46-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-12 20:35:04-05:00
logb883bc873df7f1a8fa3a13800402e1ec8da74328
treecd53413c233c9e724332f1ce2cc5291a3d67a385
parent634d11ab28839ebc3caae2bf18cbc028a7a1c3e9
signaturelock-open Commit is signed but in an unrecognized format.

breaking API changes to all readInt/writeInt functions & more

* add `@bswap` builtin function. See #767 * comptime evaluation facilities are improved to be able to handle a `@ptrCast` with a backing array. * `@truncate` allows "truncating" a u0 value to any integer type, and the result is always comptime known to be `0`. * when specifying pointer alignment in a type expression, the alignment value of pointers which do not have addresses at runtime is ignored, and always has the default/ABI alignment * threw in a fix to freebsd/x86_64.zig to update syntax from language changes * some improvements are pending #863 closes #638 closes #1733 std lib API changes * io.InStream().readIntNe renamed to readIntNative * io.InStream().readIntLe renamed to readIntLittle * io.InStream().readIntBe renamed to readIntBig * introduced io.InStream().readIntForeign * io.InStream().readInt has parameter order changed * io.InStream().readVarInt has parameter order changed * io.InStream().writeIntNe renamed to writeIntNative * introduced io.InStream().writeIntForeign * io.InStream().writeIntLe renamed to writeIntLittle * io.InStream().writeIntBe renamed to writeIntBig * io.InStream().writeInt has parameter order changed * mem.readInt has different parameters and semantics * introduced mem.readIntNative * introduced mem.readIntForeign * mem.readIntBE renamed to mem.readIntBig and different API * mem.readIntLE renamed to mem.readIntLittle and different API * introduced mem.readIntSliceNative * introduced mem.readIntSliceForeign * introduced mem.readIntSliceLittle * introduced mem.readIntSliceBig * introduced mem.readIntSlice * mem.writeInt has different parameters and semantics * introduced mem.writeIntNative * introduced mem.writeIntForeign * mem.writeIntBE renamed to mem.readIntBig and different semantics * mem.writeIntLE renamed to mem.readIntLittle and different semantics * introduced mem.writeIntSliceForeign * introduced mem.writeIntSliceNative * introduced mem.writeIntSliceBig * introduced mem.writeIntSliceLittle * introduced mem.writeIntSlice * removed mem.endianSwapIfLe * removed mem.endianSwapIfBe * removed mem.endianSwapIf * added mem.littleToNative * added mem.bigToNative * added mem.toNative * added mem.nativeTo * added mem.nativeToLittle * added mem.nativeToBig

34 files changed, 797 insertions(+), 398 deletions(-)

doc/langref.html.in+9
......@@ -5312,6 +5312,15 @@ comptime {
53125312 </p>
53135313 {#header_close#}
53145314
5315 {#header_open|@bswap#}
5316 <pre>{#syntax#}@swap(comptime T: type, value: T) T{#endsyntax#}</pre>
5317 <p>{#syntax#}T{#endsyntax#} must be an integer type with bit count evenly divisible by 8.</p>
5318 <p>
5319 Swaps the byte order of the integer. This converts a big endian integer to a little endian integer,
5320 and converts a little endian integer to a big endian integer.
5321 </p>
5322 {#header_close#}
5323
53155324 {#header_open|@bytesToSlice#}
53165325 <pre>{#syntax#}@bytesToSlice(comptime Element: type, bytes: []u8) []Element{#endsyntax#}</pre>
53175326 <p>
example/guess_number/main.zig+1-1
......@@ -15,7 +15,7 @@ pub fn main() !void {
1515 std.debug.warn("unable to seed random number generator: {}", err);
1616 return err;
1717 };
18 const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
18 const seed = std.mem.readIntNative(u64, &seed_bytes);
1919 var prng = std.rand.DefaultPrng.init(seed);
2020
2121 const answer = prng.random.range(u8, 0, 100) + 1;
src-self-hosted/compilation.zig+1-1
......@@ -55,7 +55,7 @@ pub const ZigCompiler = struct {
5555
5656 var seed_bytes: [@sizeOf(u64)]u8 = undefined;
5757 try std.os.getRandomBytes(seed_bytes[0..]);
58 const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
58 const seed = mem.readIntNative(u64, &seed_bytes);
5959
6060 return ZigCompiler{
6161 .loop = loop,
src/all_types.hpp+13
......@@ -1415,6 +1415,7 @@ enum BuiltinFnId {
14151415 BuiltinFnIdErrorReturnTrace,
14161416 BuiltinFnIdAtomicRmw,
14171417 BuiltinFnIdAtomicLoad,
1418 BuiltinFnIdBswap,
14181419};
14191420
14201421struct BuiltinFnEntry {
......@@ -1487,6 +1488,7 @@ enum ZigLLVMFnId {
14871488 ZigLLVMFnIdFloor,
14881489 ZigLLVMFnIdCeil,
14891490 ZigLLVMFnIdSqrt,
1491 ZigLLVMFnIdBswap,
14901492};
14911493
14921494enum AddSubMul {
......@@ -1516,6 +1518,9 @@ struct ZigLLVMFnKey {
15161518 uint32_t bit_count;
15171519 bool is_signed;
15181520 } overflow_arithmetic;
1521 struct {
1522 uint32_t bit_count;
1523 } bswap;
15191524 } data;
15201525};
15211526
......@@ -2158,6 +2163,7 @@ enum IrInstructionId {
21582163 IrInstructionIdMergeErrRetTraces,
21592164 IrInstructionIdMarkErrRetTracePtr,
21602165 IrInstructionIdSqrt,
2166 IrInstructionIdBswap,
21612167 IrInstructionIdErrSetCast,
21622168 IrInstructionIdToBytes,
21632169 IrInstructionIdFromBytes,
......@@ -3251,6 +3257,13 @@ struct IrInstructionCheckRuntimeScope {
32513257 IrInstruction *is_comptime;
32523258};
32533259
3260struct IrInstructionBswap {
3261 IrInstruction base;
3262
3263 IrInstruction *type;
3264 IrInstruction *op;
3265};
3266
32543267static const size_t slice_ptr_index = 0;
32553268static const size_t slice_len_index = 1;
32563269
src/analyze.cpp+6-1
......@@ -401,7 +401,8 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
401401}
402402
403403ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_const,
404 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment, uint32_t bit_offset_in_host, uint32_t host_int_bytes)
404 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment,
405 uint32_t bit_offset_in_host, uint32_t host_int_bytes)
405406{
406407 assert(!type_is_invalid(child_type));
407408 assert(ptr_len == PtrLenSingle || child_type->id != ZigTypeIdOpaque);
......@@ -6110,6 +6111,8 @@ uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey x) {
61106111 return (uint32_t)(x.data.floating.bit_count) * (uint32_t)1953839089;
61116112 case ZigLLVMFnIdSqrt:
61126113 return (uint32_t)(x.data.floating.bit_count) * (uint32_t)2225366385;
6114 case ZigLLVMFnIdBswap:
6115 return (uint32_t)(x.data.bswap.bit_count) * (uint32_t)3661994335;
61136116 case ZigLLVMFnIdOverflowArithmetic:
61146117 return ((uint32_t)(x.data.overflow_arithmetic.bit_count) * 87135777) +
61156118 ((uint32_t)(x.data.overflow_arithmetic.add_sub_mul) * 31640542) +
......@@ -6128,6 +6131,8 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
61286131 return a.data.clz.bit_count == b.data.clz.bit_count;
61296132 case ZigLLVMFnIdPopCount:
61306133 return a.data.pop_count.bit_count == b.data.pop_count.bit_count;
6134 case ZigLLVMFnIdBswap:
6135 return a.data.bswap.bit_count == b.data.bswap.bit_count;
61316136 case ZigLLVMFnIdFloor:
61326137 case ZigLLVMFnIdCeil:
61336138 case ZigLLVMFnIdSqrt:
src/codegen.cpp+31
......@@ -3814,6 +3814,11 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
38143814 n_args = 1;
38153815 key.id = ZigLLVMFnIdPopCount;
38163816 key.data.pop_count.bit_count = (uint32_t)int_type->data.integral.bit_count;
3817 } else if (fn_id == BuiltinFnIdBswap) {
3818 fn_name = "bswap";
3819 n_args = 1;
3820 key.id = ZigLLVMFnIdBswap;
3821 key.data.bswap.bit_count = (uint32_t)int_type->data.integral.bit_count;
38173822 } else {
38183823 zig_unreachable();
38193824 }
......@@ -5098,6 +5103,29 @@ static LLVMValueRef ir_render_sqrt(CodeGen *g, IrExecutable *executable, IrInstr
50985103 return LLVMBuildCall(g->builder, fn_val, &op, 1, "");
50995104}
51005105
5106static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInstructionBswap *instruction) {
5107 LLVMValueRef op = ir_llvm_value(g, instruction->op);
5108 ZigType *int_type = instruction->base.value.type;
5109 assert(int_type->id == ZigTypeIdInt);
5110 if (int_type->data.integral.bit_count % 16 == 0) {
5111 LLVMValueRef fn_val = get_int_builtin_fn(g, instruction->base.value.type, BuiltinFnIdBswap);
5112 return LLVMBuildCall(g->builder, fn_val, &op, 1, "");
5113 }
5114 // Not an even number of bytes, so we zext 1 byte, then bswap, shift right 1 byte, truncate
5115 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,
5116 int_type->data.integral.bit_count + 8);
5117 // aabbcc
5118 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, extended_type->type_ref, "");
5119 // 00aabbcc
5120 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);
5121 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");
5122 // ccbbaa00
5123 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,
5124 LLVMConstInt(extended_type->type_ref, 8, false), "");
5125 // 00ccbbaa
5126 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");
5127}
5128
51015129static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
51025130 AstNode *source_node = instruction->source_node;
51035131 Scope *scope = instruction->scope;
......@@ -5335,6 +5363,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
53355363 return ir_render_mark_err_ret_trace_ptr(g, executable, (IrInstructionMarkErrRetTracePtr *)instruction);
53365364 case IrInstructionIdSqrt:
53375365 return ir_render_sqrt(g, executable, (IrInstructionSqrt *)instruction);
5366 case IrInstructionIdBswap:
5367 return ir_render_bswap(g, executable, (IrInstructionBswap *)instruction);
53385368 }
53395369 zig_unreachable();
53405370}
......@@ -6757,6 +6787,7 @@ static void define_builtin_fns(CodeGen *g) {
67576787 create_builtin_fn(g, BuiltinFnIdToBytes, "sliceToBytes", 1);
67586788 create_builtin_fn(g, BuiltinFnIdFromBytes, "bytesToSlice", 2);
67596789 create_builtin_fn(g, BuiltinFnIdThis, "This", 0);
6790 create_builtin_fn(g, BuiltinFnIdBswap, "bswap", 2);
67606791}
67616792
67626793static const char *bool_to_str(bool b) {
src/ir.cpp+146-10
......@@ -856,6 +856,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionSqrt *) {
856856 return IrInstructionIdSqrt;
857857}
858858
859static constexpr IrInstructionId ir_instruction_id(IrInstructionBswap *) {
860 return IrInstructionIdBswap;
861}
862
859863static constexpr IrInstructionId ir_instruction_id(IrInstructionCheckRuntimeScope *) {
860864 return IrInstructionIdCheckRuntimeScope;
861865}
......@@ -2705,6 +2709,17 @@ static IrInstruction *ir_build_sqrt(IrBuilder *irb, Scope *scope, AstNode *sourc
27052709 return &instruction->base;
27062710}
27072711
2712static IrInstruction *ir_build_bswap(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type, IrInstruction *op) {
2713 IrInstructionBswap *instruction = ir_build_instruction<IrInstructionBswap>(irb, scope, source_node);
2714 instruction->type = type;
2715 instruction->op = op;
2716
2717 if (type != nullptr) ir_ref_instruction(type, irb->current_basic_block);
2718 ir_ref_instruction(op, irb->current_basic_block);
2719
2720 return &instruction->base;
2721}
2722
27082723static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *scope_is_comptime, IrInstruction *is_comptime) {
27092724 IrInstructionCheckRuntimeScope *instruction = ir_build_instruction<IrInstructionCheckRuntimeScope>(irb, scope, source_node);
27102725 instruction->scope_is_comptime = scope_is_comptime;
......@@ -4689,6 +4704,21 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46894704 IrInstruction *result = ir_build_enum_to_int(irb, scope, node, arg0_value);
46904705 return ir_lval_wrap(irb, scope, result, lval);
46914706 }
4707 case BuiltinFnIdBswap:
4708 {
4709 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4710 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4711 if (arg0_value == irb->codegen->invalid_instruction)
4712 return arg0_value;
4713
4714 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4715 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4716 if (arg1_value == irb->codegen->invalid_instruction)
4717 return arg1_value;
4718
4719 IrInstruction *result = ir_build_bswap(irb, scope, node, arg0_value, arg1_value);
4720 return ir_lval_wrap(irb, scope, result, lval);
4721 }
46924722 }
46934723 zig_unreachable();
46944724}
......@@ -13674,18 +13704,55 @@ static Error ir_read_const_ptr(IrAnalyze *ira, AstNode *source_node,
1367413704 return ErrorNone;
1367513705 }
1367613706
13677 if (dst_size > src_size) {
13678 ir_add_error_node(ira, source_node,
13679 buf_sprintf("attempt to read %zu bytes from pointer to %s which is %zu bytes",
13680 dst_size, buf_ptr(&pointee->type->name), src_size));
13681 return ErrorSemanticAnalyzeFail;
13707 if (dst_size <= src_size) {
13708 Buf buf = BUF_INIT;
13709 buf_resize(&buf, src_size);
13710 buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), pointee);
13711 buf_read_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), out_val);
13712 return ErrorNone;
1368213713 }
1368313714
13684 Buf buf = BUF_INIT;
13685 buf_resize(&buf, src_size);
13686 buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), pointee);
13687 buf_read_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), out_val);
13688 return ErrorNone;
13715 switch (ptr_val->data.x_ptr.special) {
13716 case ConstPtrSpecialInvalid:
13717 zig_unreachable();
13718 case ConstPtrSpecialRef: {
13719 ir_add_error_node(ira, source_node,
13720 buf_sprintf("attempt to read %zu bytes from pointer to %s which is %zu bytes",
13721 dst_size, buf_ptr(&pointee->type->name), src_size));
13722 return ErrorSemanticAnalyzeFail;
13723 }
13724 case ConstPtrSpecialBaseArray: {
13725 ConstExprValue *array_val = ptr_val->data.x_ptr.data.base_array.array_val;
13726 assert(array_val->type->id == ZigTypeIdArray);
13727 if (array_val->data.x_array.special != ConstArraySpecialNone)
13728 zig_panic("TODO");
13729 size_t elem_size = src_size;
13730 src_size = elem_size *
13731 (array_val->type->data.array.len - ptr_val->data.x_ptr.data.base_array.elem_index);
13732 if (dst_size > src_size) {
13733 ir_add_error_node(ira, source_node,
13734 buf_sprintf("attempt to read %zu bytes from %s at index %" ZIG_PRI_usize " which is %zu bytes",
13735 dst_size, buf_ptr(&array_val->type->name), ptr_val->data.x_ptr.data.base_array.elem_index,
13736 src_size));
13737 return ErrorSemanticAnalyzeFail;
13738 }
13739 size_t elem_count = (dst_size % elem_size == 0) ? (dst_size / elem_size) : (dst_size / elem_size + 1);
13740 Buf buf = BUF_INIT;
13741 buf_resize(&buf, elem_count * elem_size);
13742 for (size_t i = 0; i < elem_count; i += 1) {
13743 ConstExprValue *elem_val = &array_val->data.x_array.data.s_none.elements[i];
13744 buf_write_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf) + (i * elem_size), elem_val);
13745 }
13746 buf_read_value_bytes(ira->codegen, (uint8_t*)buf_ptr(&buf), out_val);
13747 return ErrorNone;
13748 }
13749 case ConstPtrSpecialBaseStruct:
13750 case ConstPtrSpecialDiscard:
13751 case ConstPtrSpecialHardCodedAddr:
13752 case ConstPtrSpecialFunction:
13753 zig_panic("TODO");
13754 }
13755 zig_unreachable();
1368913756}
1369013757
1369113758static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
......@@ -18054,6 +18121,12 @@ static IrInstruction *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruct
1805418121 return ira->codegen->invalid_instruction;
1805518122 }
1805618123
18124 if (src_type->data.integral.bit_count == 0) {
18125 IrInstruction *result = ir_const(ira, &instruction->base, dest_type);
18126 bigint_init_unsigned(&result->value.data.x_bigint, 0);
18127 return result;
18128 }
18129
1805718130 if (src_type->data.integral.is_signed != dest_type->data.integral.is_signed) {
1805818131 const char *sign_str = dest_type->data.integral.is_signed ? "signed" : "unsigned";
1805918132 ir_add_error(ira, target, buf_sprintf("expected %s integer type, found '%s'", sign_str, buf_ptr(&src_type->name)));
......@@ -20299,6 +20372,9 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
2029920372 return ira->codegen->invalid_instruction;
2030020373 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown)))
2030120374 return ira->codegen->invalid_instruction;
20375 if (!type_has_bits(child_type)) {
20376 align_bytes = 0;
20377 }
2030220378 } else {
2030320379 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
2030420380 return ira->codegen->invalid_instruction;
......@@ -20898,6 +20974,63 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS
2089820974 return result;
2089920975}
2090020976
20977static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstructionBswap *instruction) {
20978 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);
20979 if (type_is_invalid(int_type))
20980 return ira->codegen->invalid_instruction;
20981
20982 IrInstruction *op = instruction->op->child;
20983 if (type_is_invalid(op->value.type))
20984 return ira->codegen->invalid_instruction;
20985
20986 if (int_type->id != ZigTypeIdInt) {
20987 ir_add_error(ira, instruction->type,
20988 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
20989 return ira->codegen->invalid_instruction;
20990 }
20991
20992 if (int_type->data.integral.bit_count % 8 != 0) {
20993 ir_add_error(ira, instruction->type,
20994 buf_sprintf("@bswap integer type '%s' has %" PRIu32 " bits which is not evenly divisible by 8",
20995 buf_ptr(&int_type->name), int_type->data.integral.bit_count));
20996 return ira->codegen->invalid_instruction;
20997 }
20998
20999 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
21000 if (type_is_invalid(casted_op->value.type))
21001 return ira->codegen->invalid_instruction;
21002
21003 if (int_type->data.integral.bit_count == 0) {
21004 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
21005 bigint_init_unsigned(&result->value.data.x_bigint, 0);
21006 return result;
21007 }
21008
21009 if (int_type->data.integral.bit_count == 8) {
21010 return casted_op;
21011 }
21012
21013 if (instr_is_comptime(casted_op)) {
21014 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);
21015 if (!val)
21016 return ira->codegen->invalid_instruction;
21017
21018 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
21019 size_t buf_size = int_type->data.integral.bit_count / 8;
21020 uint8_t *buf = allocate_nonzero<uint8_t>(buf_size);
21021 bigint_write_twos_complement(&val->data.x_bigint, buf, int_type->data.integral.bit_count, true);
21022 bigint_read_twos_complement(&result->value.data.x_bigint, buf, int_type->data.integral.bit_count, false,
21023 int_type->data.integral.is_signed);
21024 return result;
21025 }
21026
21027 IrInstruction *result = ir_build_bswap(&ira->new_irb, instruction->base.scope,
21028 instruction->base.source_node, nullptr, casted_op);
21029 result->value.type = int_type;
21030 return result;
21031}
21032
21033
2090121034static IrInstruction *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstructionEnumToInt *instruction) {
2090221035 Error err;
2090321036 IrInstruction *target = instruction->target->child;
......@@ -21233,6 +21366,8 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
2123321366 return ir_analyze_instruction_mark_err_ret_trace_ptr(ira, (IrInstructionMarkErrRetTracePtr *)instruction);
2123421367 case IrInstructionIdSqrt:
2123521368 return ir_analyze_instruction_sqrt(ira, (IrInstructionSqrt *)instruction);
21369 case IrInstructionIdBswap:
21370 return ir_analyze_instruction_bswap(ira, (IrInstructionBswap *)instruction);
2123621371 case IrInstructionIdIntToErr:
2123721372 return ir_analyze_instruction_int_to_err(ira, (IrInstructionIntToErr *)instruction);
2123821373 case IrInstructionIdErrToInt:
......@@ -21454,6 +21589,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2145421589 case IrInstructionIdCoroPromise:
2145521590 case IrInstructionIdPromiseResultType:
2145621591 case IrInstructionIdSqrt:
21592 case IrInstructionIdBswap:
2145721593 case IrInstructionIdAtomicLoad:
2145821594 case IrInstructionIdIntCast:
2145921595 case IrInstructionIdFloatCast:
src/ir_print.cpp+15
......@@ -1323,6 +1323,18 @@ static void ir_print_sqrt(IrPrint *irp, IrInstructionSqrt *instruction) {
13231323 fprintf(irp->f, ")");
13241324}
13251325
1326static void ir_print_bswap(IrPrint *irp, IrInstructionBswap *instruction) {
1327 fprintf(irp->f, "@bswap(");
1328 if (instruction->type != nullptr) {
1329 ir_print_other_instruction(irp, instruction->type);
1330 } else {
1331 fprintf(irp->f, "null");
1332 }
1333 fprintf(irp->f, ",");
1334 ir_print_other_instruction(irp, instruction->op);
1335 fprintf(irp->f, ")");
1336}
1337
13261338static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
13271339 ir_print_prefix(irp, instruction);
13281340 switch (instruction->id) {
......@@ -1736,6 +1748,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17361748 case IrInstructionIdSqrt:
17371749 ir_print_sqrt(irp, (IrInstructionSqrt *)instruction);
17381750 break;
1751 case IrInstructionIdBswap:
1752 ir_print_bswap(irp, (IrInstructionBswap *)instruction);
1753 break;
17391754 case IrInstructionIdAtomicLoad:
17401755 ir_print_atomic_load(irp, (IrInstructionAtomicLoad *)instruction);
17411756 break;
std/coff.zig+22-22
......@@ -51,7 +51,7 @@ pub const Coff = struct {
5151
5252 // Seek to PE File Header (coff header)
5353 try self.in_file.seekTo(pe_pointer_offset);
54 const pe_magic_offset = try in.readIntLe(u32);
54 const pe_magic_offset = try in.readIntLittle(u32);
5555 try self.in_file.seekTo(pe_magic_offset);
5656
5757 var pe_header_magic: [4]u8 = undefined;
......@@ -60,13 +60,13 @@ pub const Coff = struct {
6060 return error.InvalidPEHeader;
6161
6262 self.coff_header = CoffHeader{
63 .machine = try in.readIntLe(u16),
64 .number_of_sections = try in.readIntLe(u16),
65 .timedate_stamp = try in.readIntLe(u32),
66 .pointer_to_symbol_table = try in.readIntLe(u32),
67 .number_of_symbols = try in.readIntLe(u32),
68 .size_of_optional_header = try in.readIntLe(u16),
69 .characteristics = try in.readIntLe(u16),
63 .machine = try in.readIntLittle(u16),
64 .number_of_sections = try in.readIntLittle(u16),
65 .timedate_stamp = try in.readIntLittle(u32),
66 .pointer_to_symbol_table = try in.readIntLittle(u32),
67 .number_of_symbols = try in.readIntLittle(u32),
68 .size_of_optional_header = try in.readIntLittle(u16),
69 .characteristics = try in.readIntLittle(u16),
7070 };
7171
7272 switch (self.coff_header.machine) {
......@@ -79,7 +79,7 @@ pub const Coff = struct {
7979
8080 fn loadOptionalHeader(self: *Coff, file_stream: *os.File.InStream) !void {
8181 const in = &file_stream.stream;
82 self.pe_header.magic = try in.readIntLe(u16);
82 self.pe_header.magic = try in.readIntLittle(u16);
8383 // For now we're only interested in finding the reference to the .pdb,
8484 // so we'll skip most of this header, which size is different in 32
8585 // 64 bits by the way.
......@@ -93,14 +93,14 @@ pub const Coff = struct {
9393
9494 try self.in_file.seekForward(skip_size);
9595
96 const number_of_rva_and_sizes = try in.readIntLe(u32);
96 const number_of_rva_and_sizes = try in.readIntLittle(u32);
9797 if (number_of_rva_and_sizes != IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
9898 return error.InvalidPEHeader;
9999
100100 for (self.pe_header.data_directory) |*data_dir| {
101101 data_dir.* = OptionalHeader.DataDirectory{
102 .virtual_address = try in.readIntLe(u32),
103 .size = try in.readIntLe(u32),
102 .virtual_address = try in.readIntLittle(u32),
103 .size = try in.readIntLittle(u32),
104104 };
105105 }
106106 }
......@@ -124,7 +124,7 @@ pub const Coff = struct {
124124 if (!mem.eql(u8, cv_signature, "RSDS"))
125125 return error.InvalidPEMagic;
126126 try in.readNoEof(self.guid[0..]);
127 self.age = try in.readIntLe(u32);
127 self.age = try in.readIntLittle(u32);
128128
129129 // Finally read the null-terminated string.
130130 var byte = try in.readByte();
......@@ -157,15 +157,15 @@ pub const Coff = struct {
157157 try self.sections.append(Section{
158158 .header = SectionHeader{
159159 .name = name,
160 .misc = SectionHeader.Misc{ .physical_address = try in.readIntLe(u32) },
161 .virtual_address = try in.readIntLe(u32),
162 .size_of_raw_data = try in.readIntLe(u32),
163 .pointer_to_raw_data = try in.readIntLe(u32),
164 .pointer_to_relocations = try in.readIntLe(u32),
165 .pointer_to_line_numbers = try in.readIntLe(u32),
166 .number_of_relocations = try in.readIntLe(u16),
167 .number_of_line_numbers = try in.readIntLe(u16),
168 .characteristics = try in.readIntLe(u32),
160 .misc = SectionHeader.Misc{ .physical_address = try in.readIntLittle(u32) },
161 .virtual_address = try in.readIntLittle(u32),
162 .size_of_raw_data = try in.readIntLittle(u32),
163 .pointer_to_raw_data = try in.readIntLittle(u32),
164 .pointer_to_relocations = try in.readIntLittle(u32),
165 .pointer_to_line_numbers = try in.readIntLittle(u32),
166 .number_of_relocations = try in.readIntLittle(u16),
167 .number_of_line_numbers = try in.readIntLittle(u16),
168 .characteristics = try in.readIntLittle(u32),
169169 },
170170 });
171171 }
std/crypto/blake2.zig+7-4
......@@ -123,7 +123,8 @@ fn Blake2s(comptime out_len: usize) type {
123123 const rr = d.h[0 .. out_len / 32];
124124
125125 for (rr) |s, j| {
126 mem.writeInt(out[4 * j .. 4 * j + 4], s, builtin.Endian.Little);
126 // TODO https://github.com/ziglang/zig/issues/863
127 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
127128 }
128129 }
129130
......@@ -134,7 +135,8 @@ fn Blake2s(comptime out_len: usize) type {
134135 var v: [16]u32 = undefined;
135136
136137 for (m) |*r, i| {
137 r.* = mem.readIntLE(u32, b[4 * i .. 4 * i + 4]);
138 // TODO https://github.com/ziglang/zig/issues/863
139 r.* = mem.readIntSliceLittle(u32, b[4 * i .. 4 * i + 4]);
138140 }
139141
140142 var k: usize = 0;
......@@ -356,7 +358,8 @@ fn Blake2b(comptime out_len: usize) type {
356358 const rr = d.h[0 .. out_len / 64];
357359
358360 for (rr) |s, j| {
359 mem.writeInt(out[8 * j .. 8 * j + 8], s, builtin.Endian.Little);
361 // TODO https://github.com/ziglang/zig/issues/863
362 mem.writeIntSliceLittle(u64, out[8 * j .. 8 * j + 8], s);
360363 }
361364 }
362365
......@@ -367,7 +370,7 @@ fn Blake2b(comptime out_len: usize) type {
367370 var v: [16]u64 = undefined;
368371
369372 for (m) |*r, i| {
370 r.* = mem.readIntLE(u64, b[8 * i .. 8 * i + 8]);
373 r.* = mem.readIntSliceLittle(u64, b[8 * i .. 8 * i + 8]);
371374 }
372375
373376 var k: usize = 0;
std/crypto/chacha20.zig+27-26
......@@ -59,7 +59,8 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {
5959 }
6060
6161 for (x) |_, i| {
62 mem.writeInt(out[4 * i .. 4 * i + 4], x[i] +% input[i], builtin.Endian.Little);
62 // TODO https://github.com/ziglang/zig/issues/863
63 mem.writeIntSliceLittle(u32, out[4 * i .. 4 * i + 4], x[i] +% input[i]);
6364 }
6465}
6566
......@@ -70,10 +71,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo
7071
7172 const c = "expand 32-byte k";
7273 const constant_le = []u32{
73 mem.readIntLE(u32, c[0..4]),
74 mem.readIntLE(u32, c[4..8]),
75 mem.readIntLE(u32, c[8..12]),
76 mem.readIntLE(u32, c[12..16]),
74 mem.readIntSliceLittle(u32, c[0..4]),
75 mem.readIntSliceLittle(u32, c[4..8]),
76 mem.readIntSliceLittle(u32, c[8..12]),
77 mem.readIntSliceLittle(u32, c[12..16]),
7778 };
7879
7980 mem.copy(u32, ctx[0..], constant_le[0..4]);
......@@ -117,19 +118,19 @@ pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce:
117118 var k: [8]u32 = undefined;
118119 var c: [4]u32 = undefined;
119120
120 k[0] = mem.readIntLE(u32, key[0..4]);
121 k[1] = mem.readIntLE(u32, key[4..8]);
122 k[2] = mem.readIntLE(u32, key[8..12]);
123 k[3] = mem.readIntLE(u32, key[12..16]);
124 k[4] = mem.readIntLE(u32, key[16..20]);
125 k[5] = mem.readIntLE(u32, key[20..24]);
126 k[6] = mem.readIntLE(u32, key[24..28]);
127 k[7] = mem.readIntLE(u32, key[28..32]);
121 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
122 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
123 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
124 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
125 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
126 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
127 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
128 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
128129
129130 c[0] = counter;
130 c[1] = mem.readIntLE(u32, nonce[0..4]);
131 c[2] = mem.readIntLE(u32, nonce[4..8]);
132 c[3] = mem.readIntLE(u32, nonce[8..12]);
131 c[1] = mem.readIntSliceLittle(u32, nonce[0..4]);
132 c[2] = mem.readIntSliceLittle(u32, nonce[4..8]);
133 c[3] = mem.readIntSliceLittle(u32, nonce[8..12]);
133134 chaCha20_internal(out, in, k, c);
134135}
135136
......@@ -144,19 +145,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
144145 var k: [8]u32 = undefined;
145146 var c: [4]u32 = undefined;
146147
147 k[0] = mem.readIntLE(u32, key[0..4]);
148 k[1] = mem.readIntLE(u32, key[4..8]);
149 k[2] = mem.readIntLE(u32, key[8..12]);
150 k[3] = mem.readIntLE(u32, key[12..16]);
151 k[4] = mem.readIntLE(u32, key[16..20]);
152 k[5] = mem.readIntLE(u32, key[20..24]);
153 k[6] = mem.readIntLE(u32, key[24..28]);
154 k[7] = mem.readIntLE(u32, key[28..32]);
148 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
149 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
150 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
151 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
152 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
153 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
154 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
155 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
155156
156157 c[0] = @truncate(u32, counter);
157158 c[1] = @truncate(u32, counter >> 32);
158 c[2] = mem.readIntLE(u32, nonce[0..4]);
159 c[3] = mem.readIntLE(u32, nonce[4..8]);
159 c[2] = mem.readIntSliceLittle(u32, nonce[0..4]);
160 c[3] = mem.readIntSliceLittle(u32, nonce[4..8]);
160161
161162 const block_size = (1 << 6);
162163 const big_block = (block_size << 32);
std/crypto/md5.zig+2-1
......@@ -112,7 +112,8 @@ pub const Md5 = struct {
112112 d.round(d.buf[0..]);
113113
114114 for (d.s) |s, j| {
115 mem.writeInt(out[4 * j .. 4 * j + 4], s, builtin.Endian.Little);
115 // TODO https://github.com/ziglang/zig/issues/863
116 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
116117 }
117118 }
118119
std/crypto/poly1305.zig+14-13
......@@ -6,8 +6,8 @@ const std = @import("../index.zig");
66const builtin = @import("builtin");
77
88const Endian = builtin.Endian;
9const readInt = std.mem.readInt;
10const writeInt = std.mem.writeInt;
9const readIntSliceLittle = std.mem.readIntSliceLittle;
10const writeIntSliceLittle = std.mem.writeIntSliceLittle;
1111
1212pub const Poly1305 = struct {
1313 const Self = @This();
......@@ -59,19 +59,19 @@ pub const Poly1305 = struct {
5959 {
6060 var i: usize = 0;
6161 while (i < 1) : (i += 1) {
62 ctx.r[0] = readInt(key[0..4], u32, Endian.Little) & 0x0fffffff;
62 ctx.r[0] = readIntSliceLittle(u32, key[0..4]) & 0x0fffffff;
6363 }
6464 }
6565 {
6666 var i: usize = 1;
6767 while (i < 4) : (i += 1) {
68 ctx.r[i] = readInt(key[i * 4 .. i * 4 + 4], u32, Endian.Little) & 0x0ffffffc;
68 ctx.r[i] = readIntSliceLittle(u32, key[i * 4 .. i * 4 + 4]) & 0x0ffffffc;
6969 }
7070 }
7171 {
7272 var i: usize = 0;
7373 while (i < 4) : (i += 1) {
74 ctx.pad[i] = readInt(key[i * 4 + 16 .. i * 4 + 16 + 4], u32, Endian.Little);
74 ctx.pad[i] = readIntSliceLittle(u32, key[i * 4 + 16 .. i * 4 + 16 + 4]);
7575 }
7676 }
7777
......@@ -168,10 +168,10 @@ pub const Poly1305 = struct {
168168 const nb_blocks = nmsg.len >> 4;
169169 var i: usize = 0;
170170 while (i < nb_blocks) : (i += 1) {
171 ctx.c[0] = readInt(nmsg[0..4], u32, Endian.Little);
172 ctx.c[1] = readInt(nmsg[4..8], u32, Endian.Little);
173 ctx.c[2] = readInt(nmsg[8..12], u32, Endian.Little);
174 ctx.c[3] = readInt(nmsg[12..16], u32, Endian.Little);
171 ctx.c[0] = readIntSliceLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntSliceLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntSliceLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntSliceLittle(u32, nmsg[12..16]);
175175 polyBlock(ctx);
176176 nmsg = nmsg[16..];
177177 }
......@@ -210,10 +210,11 @@ pub const Poly1305 = struct {
210210 const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000
211211 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000
212212
213 writeInt(out[0..], @truncate(u32, uu0), Endian.Little);
214 writeInt(out[4..], @truncate(u32, uu1), Endian.Little);
215 writeInt(out[8..], @truncate(u32, uu2), Endian.Little);
216 writeInt(out[12..], @truncate(u32, uu3), Endian.Little);
213 // TODO https://github.com/ziglang/zig/issues/863
214 writeIntSliceLittle(u32, out[0..], @truncate(u32, uu0));
215 writeIntSliceLittle(u32, out[4..], @truncate(u32, uu1));
216 writeIntSliceLittle(u32, out[8..], @truncate(u32, uu2));
217 writeIntSliceLittle(u32, out[12..], @truncate(u32, uu3));
217218
218219 ctx.secureZero();
219220 }
std/crypto/sha1.zig+2-1
......@@ -109,7 +109,8 @@ pub const Sha1 = struct {
109109 d.round(d.buf[0..]);
110110
111111 for (d.s) |s, j| {
112 mem.writeInt(out[4 * j .. 4 * j + 4], s, builtin.Endian.Big);
112 // TODO https://github.com/ziglang/zig/issues/863
113 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
113114 }
114115 }
115116
std/crypto/sha2.zig+4-2
......@@ -167,7 +167,8 @@ fn Sha2_32(comptime params: Sha2Params32) type {
167167 const rr = d.s[0 .. params.out_len / 32];
168168
169169 for (rr) |s, j| {
170 mem.writeInt(out[4 * j .. 4 * j + 4], s, builtin.Endian.Big);
170 // TODO https://github.com/ziglang/zig/issues/863
171 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
171172 }
172173 }
173174
......@@ -508,7 +509,8 @@ fn Sha2_64(comptime params: Sha2Params64) type {
508509 const rr = d.s[0 .. params.out_len / 64];
509510
510511 for (rr) |s, j| {
511 mem.writeInt(out[8 * j .. 8 * j + 8], s, builtin.Endian.Big);
512 // TODO https://github.com/ziglang/zig/issues/863
513 mem.writeIntSliceBig(u64, out[8 * j .. 8 * j + 8], s);
512514 }
513515 }
514516
std/crypto/sha3.zig+3-2
......@@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
120120 var c = []const u64{0} ** 5;
121121
122122 for (s) |*r, i| {
123 r.* = mem.readIntLE(u64, d[8 * i .. 8 * i + 8]);
123 r.* = mem.readIntSliceLittle(u64, d[8 * i .. 8 * i + 8]);
124124 }
125125
126126 comptime var x: usize = 0;
......@@ -167,7 +167,8 @@ fn keccak_f(comptime F: usize, d: []u8) void {
167167 }
168168
169169 for (s) |r, i| {
170 mem.writeInt(d[8 * i .. 8 * i + 8], r, builtin.Endian.Little);
170 // TODO https://github.com/ziglang/zig/issues/863
171 mem.writeIntSliceLittle(u64, d[8 * i .. 8 * i + 8], r);
171172 }
172173}
173174
std/crypto/x25519.zig+21-20
......@@ -7,8 +7,8 @@ const builtin = @import("builtin");
77const fmt = std.fmt;
88
99const Endian = builtin.Endian;
10const readInt = std.mem.readInt;
11const writeInt = std.mem.writeInt;
10const readIntSliceLittle = std.mem.readIntSliceLittle;
11const writeIntSliceLittle = std.mem.writeIntSliceLittle;
1212
1313// Based on Supercop's ref10 implementation.
1414pub const X25519 = struct {
......@@ -255,16 +255,16 @@ const Fe = struct {
255255
256256 var t: [10]i64 = undefined;
257257
258 t[0] = readInt(s[0..4], u32, Endian.Little);
259 t[1] = readInt(s[4..7], u32, Endian.Little) << 6;
260 t[2] = readInt(s[7..10], u32, Endian.Little) << 5;
261 t[3] = readInt(s[10..13], u32, Endian.Little) << 3;
262 t[4] = readInt(s[13..16], u32, Endian.Little) << 2;
263 t[5] = readInt(s[16..20], u32, Endian.Little);
264 t[6] = readInt(s[20..23], u32, Endian.Little) << 7;
265 t[7] = readInt(s[23..26], u32, Endian.Little) << 5;
266 t[8] = readInt(s[26..29], u32, Endian.Little) << 4;
267 t[9] = (readInt(s[29..32], u32, Endian.Little) & 0x7fffff) << 2;
258 t[0] = readIntSliceLittle(u32, s[0..4]);
259 t[1] = u32(readIntSliceLittle(u24, s[4..7])) << 6;
260 t[2] = u32(readIntSliceLittle(u24, s[7..10])) << 5;
261 t[3] = u32(readIntSliceLittle(u24, s[10..13])) << 3;
262 t[4] = u32(readIntSliceLittle(u24, s[13..16])) << 2;
263 t[5] = readIntSliceLittle(u32, s[16..20]);
264 t[6] = u32(readIntSliceLittle(u24, s[20..23])) << 7;
265 t[7] = u32(readIntSliceLittle(u24, s[23..26])) << 5;
266 t[8] = u32(readIntSliceLittle(u24, s[26..29])) << 4;
267 t[9] = (u32(readIntSliceLittle(u24, s[29..32])) & 0x7fffff) << 2;
268268
269269 carry1(h, t[0..]);
270270 }
......@@ -544,14 +544,15 @@ const Fe = struct {
544544 ut[i] = @bitCast(u32, @intCast(i32, t[i]));
545545 }
546546
547 writeInt(s[0..], (ut[0] >> 0) | (ut[1] << 26), Endian.Little);
548 writeInt(s[4..], (ut[1] >> 6) | (ut[2] << 19), Endian.Little);
549 writeInt(s[8..], (ut[2] >> 13) | (ut[3] << 13), Endian.Little);
550 writeInt(s[12..], (ut[3] >> 19) | (ut[4] << 6), Endian.Little);
551 writeInt(s[16..], (ut[5] >> 0) | (ut[6] << 25), Endian.Little);
552 writeInt(s[20..], (ut[6] >> 7) | (ut[7] << 19), Endian.Little);
553 writeInt(s[24..], (ut[7] >> 13) | (ut[8] << 12), Endian.Little);
554 writeInt(s[28..], (ut[8] >> 20) | (ut[9] << 6), Endian.Little);
547 // TODO https://github.com/ziglang/zig/issues/863
548 writeIntSliceLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
549 writeIntSliceLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
550 writeIntSliceLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
551 writeIntSliceLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
552 writeIntSliceLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
553 writeIntSliceLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
554 writeIntSliceLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
555 writeIntSliceLittle(u32, s[28..], (ut[8] >> 20) | (ut[9] << 6));
555556
556557 std.mem.secureZero(i64, t[0..]);
557558 }
std/debug/index.zig+31-29
......@@ -523,7 +523,7 @@ fn populateModule(di: *DebugInfo, mod: *Module) !void {
523523
524524 const modi = di.pdb.getStreamById(mod.mod_info.ModuleSymStream) orelse return error.MissingDebugInfo;
525525
526 const signature = try modi.stream.readIntLe(u32);
526 const signature = try modi.stream.readIntLittle(u32);
527527 if (signature != 4)
528528 return error.InvalidDebugInfo;
529529
......@@ -757,9 +757,9 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
757757 try di.pdb.openFile(di.coff, path);
758758
759759 var pdb_stream = di.pdb.getStream(pdb.StreamType.Pdb) orelse return error.InvalidDebugInfo;
760 const version = try pdb_stream.stream.readIntLe(u32);
761 const signature = try pdb_stream.stream.readIntLe(u32);
762 const age = try pdb_stream.stream.readIntLe(u32);
760 const version = try pdb_stream.stream.readIntLittle(u32);
761 const signature = try pdb_stream.stream.readIntLittle(u32);
762 const age = try pdb_stream.stream.readIntLittle(u32);
763763 var guid: [16]u8 = undefined;
764764 try pdb_stream.stream.readNoEof(guid[0..]);
765765 if (!mem.eql(u8, di.coff.guid, guid) or di.coff.age != age)
......@@ -767,7 +767,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
767767 // We validated the executable and pdb match.
768768
769769 const string_table_index = str_tab_index: {
770 const name_bytes_len = try pdb_stream.stream.readIntLe(u32);
770 const name_bytes_len = try pdb_stream.stream.readIntLittle(u32);
771771 const name_bytes = try allocator.alloc(u8, name_bytes_len);
772772 try pdb_stream.stream.readNoEof(name_bytes);
773773
......@@ -797,8 +797,8 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
797797 };
798798 const bucket_list = try allocator.alloc(Bucket, present.len);
799799 for (present) |_| {
800 const name_offset = try pdb_stream.stream.readIntLe(u32);
801 const name_index = try pdb_stream.stream.readIntLe(u32);
800 const name_offset = try pdb_stream.stream.readIntLittle(u32);
801 const name_index = try pdb_stream.stream.readIntLittle(u32);
802802 const name = mem.toSlice(u8, name_bytes.ptr + name_offset);
803803 if (mem.eql(u8, name, "/names")) {
804804 break :str_tab_index name_index;
......@@ -859,7 +859,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
859859 var sect_contribs = ArrayList(pdb.SectionContribEntry).init(allocator);
860860 var sect_cont_offset: usize = 0;
861861 if (section_contrib_size != 0) {
862 const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.stream.readIntLe(u32));
862 const ver = @intToEnum(pdb.SectionContrSubstreamVersion, try dbi.stream.readIntLittle(u32));
863863 if (ver != pdb.SectionContrSubstreamVersion.Ver60)
864864 return error.InvalidDebugInfo;
865865 sect_cont_offset += @sizeOf(u32);
......@@ -879,11 +879,11 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo {
879879}
880880
881881fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
882 const num_words = try stream.readIntLe(u32);
882 const num_words = try stream.readIntLittle(u32);
883883 var word_i: usize = 0;
884884 var list = ArrayList(usize).init(allocator);
885885 while (word_i != num_words) : (word_i += 1) {
886 const word = try stream.readIntLe(u32);
886 const word = try stream.readIntLittle(u32);
887887 var bit_i: u5 = 0;
888888 while (true) : (bit_i += 1) {
889889 if (word & (u32(1) << bit_i) != 0) {
......@@ -1200,7 +1200,7 @@ const Constant = struct {
12001200 fn asUnsignedLe(self: *const Constant) !u64 {
12011201 if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
12021202 if (self.signed) return error.InvalidDebugInfo;
1203 return mem.readInt(self.payload, u64, builtin.Endian.Little);
1203 return mem.readIntSliceLittle(u64, self.payload);
12041204 }
12051205};
12061206
......@@ -1381,7 +1381,7 @@ fn parseFormValueBlockLen(allocator: *mem.Allocator, in_stream: var, size: usize
13811381}
13821382
13831383fn parseFormValueBlock(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue {
1384 const block_len = try in_stream.readVarInt(builtin.Endian.Little, usize, size);
1384 const block_len = try in_stream.readVarInt(usize, builtin.Endian.Little, size);
13851385 return parseFormValueBlockLen(allocator, in_stream, block_len);
13861386}
13871387
......@@ -1395,11 +1395,11 @@ fn parseFormValueConstant(allocator: *mem.Allocator, in_stream: var, signed: boo
13951395}
13961396
13971397fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 {
1398 return if (is_64) try in_stream.readIntLe(u64) else u64(try in_stream.readIntLe(u32));
1398 return if (is_64) try in_stream.readIntLittle(u64) else u64(try in_stream.readIntLittle(u32));
13991399}
14001400
14011401fn parseFormValueTargetAddrSize(in_stream: var) !u64 {
1402 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable;
1402 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLittle(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLittle(u64) else unreachable;
14031403}
14041404
14051405fn parseFormValueRefLen(allocator: *mem.Allocator, in_stream: var, size: usize) !FormValue {
......@@ -1408,7 +1408,7 @@ fn parseFormValueRefLen(allocator: *mem.Allocator, in_stream: var, size: usize)
14081408}
14091409
14101410fn parseFormValueRef(allocator: *mem.Allocator, in_stream: var, comptime T: type) !FormValue {
1411 const block_len = try in_stream.readIntLe(T);
1411 const block_len = try in_stream.readIntLittle(T);
14121412 return parseFormValueRefLen(allocator, in_stream, block_len);
14131413}
14141414
......@@ -1450,7 +1450,7 @@ fn parseFormValue(allocator: *mem.Allocator, in_stream: var, form_id: u64, is_64
14501450 },
14511451
14521452 DW.FORM_ref_addr => FormValue{ .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
1453 DW.FORM_ref_sig8 => FormValue{ .RefSig8 = try in_stream.readIntLe(u64) },
1453 DW.FORM_ref_sig8 => FormValue{ .RefSig8 = try in_stream.readIntLittle(u64) },
14541454
14551455 DW.FORM_string => FormValue{ .String = try readStringRaw(allocator, in_stream) },
14561456 DW.FORM_strp => FormValue{ .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
......@@ -1747,11 +1747,11 @@ fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_addr
17471747 continue;
17481748 }
17491749
1750 const version = try di.dwarf_in_stream.readInt(di.endian, u16);
1750 const version = try di.dwarf_in_stream.readInt(u16, di.endian);
17511751 // TODO support 3 and 5
17521752 if (version != 2 and version != 4) return error.InvalidDebugInfo;
17531753
1754 const prologue_length = if (is_64) try di.dwarf_in_stream.readInt(di.endian, u64) else try di.dwarf_in_stream.readInt(di.endian, u32);
1754 const prologue_length = if (is_64) try di.dwarf_in_stream.readInt(u64, di.endian) else try di.dwarf_in_stream.readInt(u32, di.endian);
17551755 const prog_start_offset = (try di.dwarf_seekable_stream.getPos()) + prologue_length;
17561756
17571757 const minimum_instruction_length = try di.dwarf_in_stream.readByte();
......@@ -1820,7 +1820,7 @@ fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_addr
18201820 return error.MissingDebugInfo;
18211821 },
18221822 DW.LNE_set_address => {
1823 const addr = try di.dwarf_in_stream.readInt(di.endian, usize);
1823 const addr = try di.dwarf_in_stream.readInt(usize, di.endian);
18241824 prog.address = addr;
18251825 },
18261826 DW.LNE_define_file => {
......@@ -1882,7 +1882,7 @@ fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_addr
18821882 prog.address += inc_addr;
18831883 },
18841884 DW.LNS_fixed_advance_pc => {
1885 const arg = try di.dwarf_in_stream.readInt(di.endian, u16);
1885 const arg = try di.dwarf_in_stream.readInt(u16, di.endian);
18861886 prog.address += arg;
18871887 },
18881888 DW.LNS_set_prologue_end => {},
......@@ -1914,10 +1914,10 @@ fn scanAllCompileUnits(di: *DwarfInfo) !void {
19141914 if (unit_length == 0) return;
19151915 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
19161916
1917 const version = try di.dwarf_in_stream.readInt(di.endian, u16);
1917 const version = try di.dwarf_in_stream.readInt(u16, di.endian);
19181918 if (version < 2 or version > 5) return error.InvalidDebugInfo;
19191919
1920 const debug_abbrev_offset = if (is_64) try di.dwarf_in_stream.readInt(di.endian, u64) else try di.dwarf_in_stream.readInt(di.endian, u32);
1920 const debug_abbrev_offset = if (is_64) try di.dwarf_in_stream.readInt(u64, di.endian) else try di.dwarf_in_stream.readInt(u32, di.endian);
19211921
19221922 const address_size = try di.dwarf_in_stream.readByte();
19231923 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
......@@ -1978,8 +1978,8 @@ fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit {
19781978 if (di.debug_ranges) |debug_ranges| {
19791979 try di.dwarf_seekable_stream.seekTo(debug_ranges.offset + ranges_offset);
19801980 while (true) {
1981 const begin_addr = try di.dwarf_in_stream.readIntLe(usize);
1982 const end_addr = try di.dwarf_in_stream.readIntLe(usize);
1981 const begin_addr = try di.dwarf_in_stream.readIntLittle(usize);
1982 const end_addr = try di.dwarf_in_stream.readIntLittle(usize);
19831983 if (begin_addr == 0 and end_addr == 0) {
19841984 break;
19851985 }
......@@ -2001,7 +2001,8 @@ fn findCompileUnit(di: *DwarfInfo, target_address: u64) !*const CompileUnit {
20012001}
20022002
20032003fn readIntMem(ptr: *[*]const u8, comptime T: type, endian: builtin.Endian) T {
2004 const result = mem.readInt(ptr.*[0..@sizeOf(T)], T, endian);
2004 // TODO https://github.com/ziglang/zig/issues/863
2005 const result = mem.readIntSlice(T, ptr.*[0..@sizeOf(T)], endian);
20052006 ptr.* += @sizeOf(T);
20062007 return result;
20072008}
......@@ -2017,11 +2018,12 @@ fn readByteSignedMem(ptr: *[*]const u8) i8 {
20172018}
20182019
20192020fn readInitialLengthMem(ptr: *[*]const u8, is_64: *bool) !u64 {
2020 const first_32_bits = mem.readIntLE(u32, ptr.*[0..4]);
2021 // TODO this code can be improved with https://github.com/ziglang/zig/issues/863
2022 const first_32_bits = mem.readIntSliceLittle(u32, ptr.*[0..4]);
20212023 is_64.* = (first_32_bits == 0xffffffff);
20222024 if (is_64.*) {
20232025 ptr.* += 4;
2024 const result = mem.readIntLE(u64, ptr.*[0..8]);
2026 const result = mem.readIntSliceLittle(u64, ptr.*[0..8]);
20252027 ptr.* += 8;
20262028 return result;
20272029 } else {
......@@ -2084,10 +2086,10 @@ fn readILeb128Mem(ptr: *[*]const u8) !i64 {
20842086}
20852087
20862088fn readInitialLength(comptime E: type, in_stream: *io.InStream(E), is_64: *bool) !u64 {
2087 const first_32_bits = try in_stream.readIntLe(u32);
2089 const first_32_bits = try in_stream.readIntLittle(u32);
20882090 is_64.* = (first_32_bits == 0xffffffff);
20892091 if (is_64.*) {
2090 return in_stream.readIntLe(u64);
2092 return in_stream.readIntLittle(u64);
20912093 } else {
20922094 if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo;
20932095 return u64(first_32_bits);
std/elf.zig+35-35
......@@ -412,7 +412,7 @@ pub const Elf = struct {
412412 // skip over padding
413413 try seekable_stream.seekForward(9);
414414
415 elf.file_type = switch (try in.readInt(elf.endian, u16)) {
415 elf.file_type = switch (try in.readInt(u16, elf.endian)) {
416416 1 => FileType.Relocatable,
417417 2 => FileType.Executable,
418418 3 => FileType.Shared,
......@@ -420,7 +420,7 @@ pub const Elf = struct {
420420 else => return error.InvalidFormat,
421421 };
422422
423 elf.arch = switch (try in.readInt(elf.endian, u16)) {
423 elf.arch = switch (try in.readInt(u16, elf.endian)) {
424424 0x02 => Arch.Sparc,
425425 0x03 => Arch.x86,
426426 0x08 => Arch.Mips,
......@@ -433,32 +433,32 @@ pub const Elf = struct {
433433 else => return error.InvalidFormat,
434434 };
435435
436 const elf_version = try in.readInt(elf.endian, u32);
436 const elf_version = try in.readInt(u32, elf.endian);
437437 if (elf_version != 1) return error.InvalidFormat;
438438
439439 if (elf.is_64) {
440 elf.entry_addr = try in.readInt(elf.endian, u64);
441 elf.program_header_offset = try in.readInt(elf.endian, u64);
442 elf.section_header_offset = try in.readInt(elf.endian, u64);
440 elf.entry_addr = try in.readInt(u64, elf.endian);
441 elf.program_header_offset = try in.readInt(u64, elf.endian);
442 elf.section_header_offset = try in.readInt(u64, elf.endian);
443443 } else {
444 elf.entry_addr = u64(try in.readInt(elf.endian, u32));
445 elf.program_header_offset = u64(try in.readInt(elf.endian, u32));
446 elf.section_header_offset = u64(try in.readInt(elf.endian, u32));
444 elf.entry_addr = u64(try in.readInt(u32, elf.endian));
445 elf.program_header_offset = u64(try in.readInt(u32, elf.endian));
446 elf.section_header_offset = u64(try in.readInt(u32, elf.endian));
447447 }
448448
449449 // skip over flags
450450 try seekable_stream.seekForward(4);
451451
452 const header_size = try in.readInt(elf.endian, u16);
452 const header_size = try in.readInt(u16, elf.endian);
453453 if ((elf.is_64 and header_size != 64) or (!elf.is_64 and header_size != 52)) {
454454 return error.InvalidFormat;
455455 }
456456
457 const ph_entry_size = try in.readInt(elf.endian, u16);
458 const ph_entry_count = try in.readInt(elf.endian, u16);
459 const sh_entry_size = try in.readInt(elf.endian, u16);
460 const sh_entry_count = try in.readInt(elf.endian, u16);
461 elf.string_section_index = u64(try in.readInt(elf.endian, u16));
457 const ph_entry_size = try in.readInt(u16, elf.endian);
458 const ph_entry_count = try in.readInt(u16, elf.endian);
459 const sh_entry_size = try in.readInt(u16, elf.endian);
460 const sh_entry_count = try in.readInt(u16, elf.endian);
461 elf.string_section_index = u64(try in.readInt(u16, elf.endian));
462462
463463 if (elf.string_section_index >= sh_entry_count) return error.InvalidFormat;
464464
......@@ -481,32 +481,32 @@ pub const Elf = struct {
481481 if (sh_entry_size != 64) return error.InvalidFormat;
482482
483483 for (elf.section_headers) |*elf_section| {
484 elf_section.name = try in.readInt(elf.endian, u32);
485 elf_section.sh_type = try in.readInt(elf.endian, u32);
486 elf_section.flags = try in.readInt(elf.endian, u64);
487 elf_section.addr = try in.readInt(elf.endian, u64);
488 elf_section.offset = try in.readInt(elf.endian, u64);
489 elf_section.size = try in.readInt(elf.endian, u64);
490 elf_section.link = try in.readInt(elf.endian, u32);
491 elf_section.info = try in.readInt(elf.endian, u32);
492 elf_section.addr_align = try in.readInt(elf.endian, u64);
493 elf_section.ent_size = try in.readInt(elf.endian, u64);
484 elf_section.name = try in.readInt(u32, elf.endian);
485 elf_section.sh_type = try in.readInt(u32, elf.endian);
486 elf_section.flags = try in.readInt(u64, elf.endian);
487 elf_section.addr = try in.readInt(u64, elf.endian);
488 elf_section.offset = try in.readInt(u64, elf.endian);
489 elf_section.size = try in.readInt(u64, elf.endian);
490 elf_section.link = try in.readInt(u32, elf.endian);
491 elf_section.info = try in.readInt(u32, elf.endian);
492 elf_section.addr_align = try in.readInt(u64, elf.endian);
493 elf_section.ent_size = try in.readInt(u64, elf.endian);
494494 }
495495 } else {
496496 if (sh_entry_size != 40) return error.InvalidFormat;
497497
498498 for (elf.section_headers) |*elf_section| {
499499 // TODO (multiple occurrences) allow implicit cast from %u32 -> %u64 ?
500 elf_section.name = try in.readInt(elf.endian, u32);
501 elf_section.sh_type = try in.readInt(elf.endian, u32);
502 elf_section.flags = u64(try in.readInt(elf.endian, u32));
503 elf_section.addr = u64(try in.readInt(elf.endian, u32));
504 elf_section.offset = u64(try in.readInt(elf.endian, u32));
505 elf_section.size = u64(try in.readInt(elf.endian, u32));
506 elf_section.link = try in.readInt(elf.endian, u32);
507 elf_section.info = try in.readInt(elf.endian, u32);
508 elf_section.addr_align = u64(try in.readInt(elf.endian, u32));
509 elf_section.ent_size = u64(try in.readInt(elf.endian, u32));
500 elf_section.name = try in.readInt(u32, elf.endian);
501 elf_section.sh_type = try in.readInt(u32, elf.endian);
502 elf_section.flags = u64(try in.readInt(u32, elf.endian));
503 elf_section.addr = u64(try in.readInt(u32, elf.endian));
504 elf_section.offset = u64(try in.readInt(u32, elf.endian));
505 elf_section.size = u64(try in.readInt(u32, elf.endian));
506 elf_section.link = try in.readInt(u32, elf.endian);
507 elf_section.info = try in.readInt(u32, elf.endian);
508 elf_section.addr_align = u64(try in.readInt(u32, elf.endian));
509 elf_section.ent_size = u64(try in.readInt(u32, elf.endian));
510510 }
511511 }
512512
std/event/io.zig+9-5
......@@ -39,18 +39,22 @@ pub fn InStream(comptime ReadError: type) type {
3939 if (amt_read < buf.len) return error.EndOfStream;
4040 }
4141
42 pub async fn readIntLe(self: *Self, comptime T: type) !T {
43 return await (async self.readInt(builtin.Endian.Little, T) catch unreachable);
42 pub async fn readIntLittle(self: *Self, comptime T: type) !T {
43 var bytes: [@sizeOf(T)]u8 = undefined;
44 try await (async self.readNoEof(bytes[0..]) catch unreachable);
45 return mem.readIntLittle(T, &bytes);
4446 }
4547
4648 pub async fn readIntBe(self: *Self, comptime T: type) !T {
47 return await (async self.readInt(builtin.Endian.Big, T) catch unreachable);
49 var bytes: [@sizeOf(T)]u8 = undefined;
50 try await (async self.readNoEof(bytes[0..]) catch unreachable);
51 return mem.readIntBig(T, &bytes);
4852 }
4953
50 pub async fn readInt(self: *Self, endian: builtin.Endian, comptime T: type) !T {
54 pub async fn readInt(self: *Self, comptime T: type, endian: builtin.Endian) !T {
5155 var bytes: [@sizeOf(T)]u8 = undefined;
5256 try await (async self.readNoEof(bytes[0..]) catch unreachable);
53 return mem.readInt(bytes, T, endian);
57 return mem.readInt(T, &bytes, endian);
5458 }
5559
5660 pub async fn readStruct(self: *Self, comptime T: type) !T {
std/hash/siphash.zig+7-7
......@@ -42,8 +42,8 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
4242 pub fn init(key: []const u8) Self {
4343 debug.assert(key.len >= 16);
4444
45 const k0 = mem.readInt(key[0..8], u64, Endian.Little);
46 const k1 = mem.readInt(key[8..16], u64, Endian.Little);
45 const k0 = mem.readIntSliceLittle(u64, key[0..8]);
46 const k1 = mem.readIntSliceLittle(u64, key[8..16]);
4747
4848 var d = Self{
4949 .v0 = k0 ^ 0x736f6d6570736575,
......@@ -121,7 +121,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
121121 fn round(d: *Self, b: []const u8) void {
122122 debug.assert(b.len == 8);
123123
124 const m = mem.readInt(b[0..], u64, Endian.Little);
124 const m = mem.readIntSliceLittle(u64, b[0..]);
125125 d.v3 ^= m;
126126
127127 comptime var i: usize = 0;
......@@ -162,7 +162,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
162162const test_key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
163163
164164test "siphash64-2-4 sanity" {
165 const vectors = [][]const u8{
165 const vectors = [][8]u8{
166166 "\x31\x0e\x0e\xdd\x47\xdb\x6f\x72", // ""
167167 "\xfd\x67\xdc\x93\xc5\x39\xf8\x74", // "\x00"
168168 "\x5a\x4f\xa9\xd9\x09\x80\x6c\x0d", // "\x00\x01" ... etc
......@@ -235,13 +235,13 @@ test "siphash64-2-4 sanity" {
235235 for (vectors) |vector, i| {
236236 buffer[i] = @intCast(u8, i);
237237
238 const expected = mem.readInt(vector, u64, Endian.Little);
238 const expected = mem.readIntLittle(u64, &vector);
239239 debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
240240 }
241241}
242242
243243test "siphash128-2-4 sanity" {
244 const vectors = [][]const u8{
244 const vectors = [][16]u8{
245245 "\xa3\x81\x7f\x04\xba\x25\xa8\xe6\x6d\xf6\x72\x14\xc7\x55\x02\x93",
246246 "\xda\x87\xc1\xd8\x6b\x99\xaf\x44\x34\x76\x59\x11\x9b\x22\xfc\x45",
247247 "\x81\x77\x22\x8d\xa4\xa4\x5d\xc7\xfc\xa3\x8b\xde\xf6\x0a\xff\xe4",
......@@ -314,7 +314,7 @@ test "siphash128-2-4 sanity" {
314314 for (vectors) |vector, i| {
315315 buffer[i] = @intCast(u8, i);
316316
317 const expected = mem.readInt(vector, u128, Endian.Little);
317 const expected = mem.readIntLittle(u128, &vector);
318318 debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
319319 }
320320}
std/heap.zig+1-1
......@@ -66,7 +66,7 @@ pub const DirectAllocator = struct {
6666 }
6767 }
6868
69 fn alloc(allocator: *Allocator, n: usize, alignment: u29) ![]u8 {
69 fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
7070 const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
7171
7272 switch (builtin.os) {
std/io.zig+38-22
......@@ -152,35 +152,42 @@ pub fn InStream(comptime ReadError: type) type {
152152 }
153153
154154 /// Reads a native-endian integer
155 pub fn readIntNe(self: *Self, comptime T: type) !T {
156 return self.readInt(builtin.endian, T);
155 pub fn readIntNative(self: *Self, comptime T: type) !T {
156 var bytes: [@sizeOf(T)]u8 = undefined;
157 try self.readNoEof(bytes[0..]);
158 return mem.readIntSliceNative(T, bytes);
157159 }
158160
159 pub fn readIntLe(self: *Self, comptime T: type) !T {
161 /// Reads a foreign-endian integer
162 pub fn readIntForeign(self: *Self, comptime T: type) !T {
160163 var bytes: [@sizeOf(T)]u8 = undefined;
161164 try self.readNoEof(bytes[0..]);
162 return mem.readIntLE(T, bytes);
165 return mem.readIntSliceForeign(T, bytes);
163166 }
164167
165 pub fn readIntBe(self: *Self, comptime T: type) !T {
168 pub fn readIntLittle(self: *Self, comptime T: type) !T {
166169 var bytes: [@sizeOf(T)]u8 = undefined;
167170 try self.readNoEof(bytes[0..]);
168 return mem.readIntBE(T, bytes);
171 return mem.readIntSliceLittle(T, bytes);
169172 }
170173
171 pub fn readInt(self: *Self, endian: builtin.Endian, comptime T: type) !T {
174 pub fn readIntBig(self: *Self, comptime T: type) !T {
172175 var bytes: [@sizeOf(T)]u8 = undefined;
173176 try self.readNoEof(bytes[0..]);
174 return mem.readInt(bytes, T, endian);
177 return mem.readIntSliceBig(T, bytes);
175178 }
176179
177 pub fn readVarInt(self: *Self, endian: builtin.Endian, comptime T: type, size: usize) !T {
180 pub fn readInt(self: *Self, comptime T: type, endian: builtin.Endian) !T {
181 var bytes: [@sizeOf(T)]u8 = undefined;
182 try self.readNoEof(bytes[0..]);
183 return mem.readIntSlice(T, bytes, endian);
184 }
185
186 pub fn readVarInt(self: *Self, comptime T: type, endian: builtin.Endian, size: usize) !T {
178187 assert(size <= @sizeOf(T));
179 assert(size <= 8);
180 var input_buf: [8]u8 = undefined;
181 const input_slice = input_buf[0..size];
182 try self.readNoEof(input_slice);
183 return mem.readInt(input_slice, T, endian);
188 var bytes: [@sizeOf(T)]u8 = undefined;
189 try self.readNoEof(bytes[0..]);
190 return mem.readIntSlice(T, bytes, endian);
184191 }
185192
186193 pub fn skipBytes(self: *Self, num_bytes: usize) !void {
......@@ -229,25 +236,34 @@ pub fn OutStream(comptime WriteError: type) type {
229236 }
230237
231238 /// Write a native-endian integer.
232 pub fn writeIntNe(self: *Self, comptime T: type, value: T) Error!void {
233 return self.writeInt(builtin.endian, T, value);
239 pub fn writeIntNative(self: *Self, comptime T: type, value: T) Error!void {
240 var bytes: [@sizeOf(T)]u8 = undefined;
241 mem.writeIntNative(T, &bytes, value);
242 return self.writeFn(self, bytes);
243 }
244
245 /// Write a foreign-endian integer.
246 pub fn writeIntForeign(self: *Self, comptime T: type, value: T) Error!void {
247 var bytes: [@sizeOf(T)]u8 = undefined;
248 mem.writeIntForeign(T, &bytes, value);
249 return self.writeFn(self, bytes);
234250 }
235251
236 pub fn writeIntLe(self: *Self, comptime T: type, value: T) Error!void {
252 pub fn writeIntLittle(self: *Self, comptime T: type, value: T) Error!void {
237253 var bytes: [@sizeOf(T)]u8 = undefined;
238 mem.writeIntLE(T, &bytes, value);
254 mem.writeIntLittle(T, &bytes, value);
239255 return self.writeFn(self, bytes);
240256 }
241257
242 pub fn writeIntBe(self: *Self, comptime T: type, value: T) Error!void {
258 pub fn writeIntBig(self: *Self, comptime T: type, value: T) Error!void {
243259 var bytes: [@sizeOf(T)]u8 = undefined;
244 mem.writeIntBE(T, &bytes, value);
260 mem.writeIntBig(T, &bytes, value);
245261 return self.writeFn(self, bytes);
246262 }
247263
248 pub fn writeInt(self: *Self, endian: builtin.Endian, comptime T: type, value: T) Error!void {
264 pub fn writeInt(self: *Self, comptime T: type, value: T, endian: builtin.Endian) Error!void {
249265 var bytes: [@sizeOf(T)]u8 = undefined;
250 mem.writeInt(bytes[0..], value, endian);
266 mem.writeInt(T, &bytes, value, endian);
251267 return self.writeFn(self, bytes);
252268 }
253269 };
std/mem.zig+263-164
......@@ -407,186 +407,250 @@ test "mem.indexOf" {
407407 assert(lastIndexOfScalar(u8, "boo", 'o').? == 2);
408408}
409409
410/// Reads an integer from memory with size equal to bytes.len.
411/// T specifies the return type, which must be large enough to store
412/// the result.
413/// See also ::readIntBE or ::readIntLE.
414pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) T {
415 if (T.bit_count == 8) {
416 return bytes[0];
417 }
418 var result: T = 0;
419 switch (endian) {
420 builtin.Endian.Big => {
421 for (bytes) |b| {
422 result = (result << 8) | b;
423 }
424 },
425 builtin.Endian.Little => {
426 const ShiftType = math.Log2Int(T);
427 for (bytes) |b, index| {
428 result = result | (T(b) << @intCast(ShiftType, index * 8));
429 }
430 },
431 }
432 return result;
433}
410/// Reads an integer from memory with bit count specified by T.
411/// The bit count of T must be evenly divisible by 8.
412/// This function cannot fail and cannot cause undefined behavior.
413/// Assumes the endianness of memory is native. This means the function can
414/// simply pointer cast memory.
415pub fn readIntNative(comptime T: type, bytes: *const [@sizeOf(T)]u8) T {
416 comptime assert(T.bit_count % 8 == 0);
417 return @ptrCast(*align(1) const T, bytes).*;
418}
419
420/// Reads an integer from memory with bit count specified by T.
421/// The bit count of T must be evenly divisible by 8.
422/// This function cannot fail and cannot cause undefined behavior.
423/// Assumes the endianness of memory is foreign, so it must byte-swap.
424pub fn readIntForeign(comptime T: type, bytes: *const [@sizeOf(T)]u8) T {
425 comptime assert(T.bit_count % 8 == 0);
426 return @bswap(T, @ptrCast(*align(1) const T, bytes).*);
427}
428
429pub const readIntLittle = switch (builtin.endian) {
430 builtin.Endian.Little => readIntNative,
431 builtin.Endian.Big => readIntForeign,
432};
434433
435/// Reads a big-endian int of type T from bytes.
436/// bytes.len must be exactly @sizeOf(T).
437pub fn readIntBE(comptime T: type, bytes: []const u8) T {
438 if (T.is_signed) {
439 return @bitCast(T, readIntBE(@IntType(false, T.bit_count), bytes));
440 }
441 assert(bytes.len == @sizeOf(T));
442 if (T == u8) return bytes[0];
443 var result: T = 0;
444 {
445 comptime var i = 0;
446 inline while (i < @sizeOf(T)) : (i += 1) {
447 result = (result << 8) | T(bytes[i]);
448 }
434pub const readIntBig = switch (builtin.endian) {
435 builtin.Endian.Little => readIntForeign,
436 builtin.Endian.Big => readIntNative,
437};
438
439/// Asserts that bytes.len >= @sizeOf(T). Reads the integer starting from index 0
440/// and ignores extra bytes.
441/// Note that @sizeOf(u24) is 3.
442/// The bit count of T must be evenly divisible by 8.
443/// Assumes the endianness of memory is native. This means the function can
444/// simply pointer cast memory.
445pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {
446 assert(@sizeOf(u24) == 3);
447 assert(bytes.len >= @sizeOf(T));
448 // TODO https://github.com/ziglang/zig/issues/863
449 return readIntNative(T, @ptrCast(*const [@sizeOf(T)]u8, bytes.ptr));
450}
451
452/// Asserts that bytes.len >= @sizeOf(T). Reads the integer starting from index 0
453/// and ignores extra bytes.
454/// Note that @sizeOf(u24) is 3.
455/// The bit count of T must be evenly divisible by 8.
456/// Assumes the endianness of memory is foreign, so it must byte-swap.
457pub fn readIntSliceForeign(comptime T: type, bytes: []const u8) T {
458 assert(@sizeOf(u24) == 3);
459 assert(bytes.len >= @sizeOf(T));
460 // TODO https://github.com/ziglang/zig/issues/863
461 return readIntForeign(T, @ptrCast(*const [@sizeOf(T)]u8, bytes.ptr));
462}
463
464pub const readIntSliceLittle = switch (builtin.endian) {
465 builtin.Endian.Little => readIntSliceNative,
466 builtin.Endian.Big => readIntSliceForeign,
467};
468
469pub const readIntSliceBig = switch (builtin.endian) {
470 builtin.Endian.Little => readIntSliceForeign,
471 builtin.Endian.Big => readIntSliceNative,
472};
473
474/// Reads an integer from memory with bit count specified by T.
475/// The bit count of T must be evenly divisible by 8.
476/// This function cannot fail and cannot cause undefined behavior.
477pub fn readInt(comptime T: type, bytes: *const [@sizeOf(T)]u8, endian: builtin.Endian) T {
478 if (endian == builtin.endian) {
479 return readIntNative(T, bytes);
480 } else {
481 return readIntForeign(T, bytes);
449482 }
450 return result;
451483}
452484
453/// Reads a little-endian int of type T from bytes.
454/// bytes.len must be exactly @sizeOf(T).
455pub fn readIntLE(comptime T: type, bytes: []const u8) T {
456 if (T.is_signed) {
457 return @bitCast(T, readIntLE(@IntType(false, T.bit_count), bytes));
458 }
459 assert(bytes.len == @sizeOf(T));
460 if (T == u8) return bytes[0];
461 var result: T = 0;
462 {
463 comptime var i = 0;
464 inline while (i < @sizeOf(T)) : (i += 1) {
465 result |= T(bytes[i]) << i * 8;
466 }
467 }
468 return result;
485/// Asserts that bytes.len >= @sizeOf(T). Reads the integer starting from index 0
486/// and ignores extra bytes.
487/// Note that @sizeOf(u24) is 3.
488/// The bit count of T must be evenly divisible by 8.
489pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {
490 assert(@sizeOf(u24) == 3);
491 assert(bytes.len >= @sizeOf(T));
492 // TODO https://github.com/ziglang/zig/issues/863
493 return readInt(T, @ptrCast(*const [@sizeOf(T)]u8, bytes.ptr), endian);
469494}
470495
471test "readIntBE/LE" {
472 assert(readIntBE(u0, []u8{}) == 0x0);
473 assert(readIntLE(u0, []u8{}) == 0x0);
496test "readIntBig and readIntLittle" {
497 assert(readIntSliceBig(u0, []u8{}) == 0x0);
498 assert(readIntSliceLittle(u0, []u8{}) == 0x0);
474499
475 assert(readIntBE(u8, []u8{0x32}) == 0x32);
476 assert(readIntLE(u8, []u8{0x12}) == 0x12);
500 assert(readIntSliceBig(u8, []u8{0x32}) == 0x32);
501 assert(readIntSliceLittle(u8, []u8{0x12}) == 0x12);
477502
478 assert(readIntBE(u16, []u8{ 0x12, 0x34 }) == 0x1234);
479 assert(readIntLE(u16, []u8{ 0x12, 0x34 }) == 0x3412);
503 assert(readIntSliceBig(u16, []u8{ 0x12, 0x34 }) == 0x1234);
504 assert(readIntSliceLittle(u16, []u8{ 0x12, 0x34 }) == 0x3412);
480505
481 assert(readIntBE(u72, []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024);
482 assert(readIntLE(u72, []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec);
506 assert(readIntSliceBig(u72, []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024);
507 assert(readIntSliceLittle(u72, []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec);
483508
484 assert(readIntBE(i8, []u8{0xff}) == -1);
485 assert(readIntLE(i8, []u8{0xfe}) == -2);
509 assert(readIntSliceBig(i8, []u8{0xff}) == -1);
510 assert(readIntSliceLittle(i8, []u8{0xfe}) == -2);
486511
487 assert(readIntBE(i16, []u8{ 0xff, 0xfd }) == -3);
488 assert(readIntLE(i16, []u8{ 0xfc, 0xff }) == -4);
512 assert(readIntSliceBig(i16, []u8{ 0xff, 0xfd }) == -3);
513 assert(readIntSliceLittle(i16, []u8{ 0xfc, 0xff }) == -4);
489514}
490515
491/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes
492/// to fill the entire buffer provided.
493/// value must be an integer.
494pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) void {
495 const uint = @IntType(false, @typeOf(value).bit_count);
496 var bits = @truncate(uint, value);
497 switch (endian) {
498 builtin.Endian.Big => {
499 var index: usize = buf.len;
500 while (index != 0) {
501 index -= 1;
516/// Writes an integer to memory, storing it in twos-complement.
517/// This function always succeeds, has defined behavior for all inputs, and
518/// accepts any integer bit width.
519/// This function stores in native endian, which means it is implemented as a simple
520/// memory store.
521pub fn writeIntNative(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {
522 @ptrCast(*align(1) T, buf).* = value;
523}
502524
503 buf[index] = @truncate(u8, bits);
504 bits >>= 8;
505 }
506 },
507 builtin.Endian.Little => {
508 for (buf) |*b| {
509 b.* = @truncate(u8, bits);
510 bits >>= 8;
511 }
512 },
525/// Writes an integer to memory, storing it in twos-complement.
526/// This function always succeeds, has defined behavior for all inputs, but
527/// the integer bit width must be divisible by 8.
528/// This function stores in foreign endian, which means it does a @bswap first.
529pub fn writeIntForeign(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {
530 @ptrCast(*align(1) T, buf).* = @bswap(T, value);
531}
532
533pub const writeIntLittle = switch (builtin.endian) {
534 builtin.Endian.Little => writeIntNative,
535 builtin.Endian.Big => writeIntForeign,
536};
537
538pub const writeIntBig = switch (builtin.endian) {
539 builtin.Endian.Little => writeIntForeign,
540 builtin.Endian.Big => writeIntNative,
541};
542
543/// Writes an integer to memory, storing it in twos-complement.
544/// This function always succeeds, has defined behavior for all inputs, but
545/// the integer bit width must be divisible by 8.
546pub fn writeInt(comptime T: type, buffer: *[@sizeOf(T)]u8, value: T, endian: builtin.Endian) void {
547 comptime assert(T.bit_count % 8 == 0);
548 if (endian == builtin.endian) {
549 return writeIntNative(T, buffer, value);
550 } else {
551 return writeIntForeign(T, buffer, value);
513552 }
514 assert(bits == 0);
515553}
516554
517pub fn writeIntBE(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {
518 assert(T.bit_count % 8 == 0);
555/// Writes a twos-complement little-endian integer to memory.
556/// Asserts that buf.len >= @sizeOf(T). Note that @sizeOf(u24) is 3.
557/// The bit count of T must be divisible by 8.
558/// Any extra bytes in buffer after writing the integer are set to zero. To
559/// avoid the branch to check for extra buffer bytes, use writeIntLittle
560/// instead.
561pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
562 comptime assert(@sizeOf(u24) == 3);
563 comptime assert(T.bit_count % 8 == 0);
564 assert(buffer.len >= @sizeOf(T));
565
566 // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough
519567 const uint = @IntType(false, T.bit_count);
520 if (uint == u0) {
521 return;
522 }
523 var bits = @bitCast(uint, value);
524 if (uint == u8) {
525 buf[0] = bits;
526 return;
568 var bits = @truncate(uint, value);
569 for (buffer) |*b| {
570 b.* = @truncate(u8, bits);
571 bits >>= 8;
527572 }
528 var index: usize = buf.len;
573}
574
575/// Writes a twos-complement big-endian integer to memory.
576/// Asserts that buffer.len >= @sizeOf(T). Note that @sizeOf(u24) is 3.
577/// The bit count of T must be divisible by 8.
578/// Any extra bytes in buffer before writing the integer are set to zero. To
579/// avoid the branch to check for extra buffer bytes, use writeIntBig instead.
580pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {
581 comptime assert(@sizeOf(u24) == 3);
582 comptime assert(T.bit_count % 8 == 0);
583 assert(buffer.len >= @sizeOf(T));
584
585 // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough
586 const uint = @IntType(false, T.bit_count);
587 var bits = @truncate(uint, value);
588 var index: usize = buffer.len;
529589 while (index != 0) {
530590 index -= 1;
531
532 buf[index] = @truncate(u8, bits);
591 buffer[index] = @truncate(u8, bits);
533592 bits >>= 8;
534593 }
535 assert(bits == 0);
536594}
537595
538pub fn writeIntLE(comptime T: type, buf: *[@sizeOf(T)]u8, value: T) void {
539 assert(T.bit_count % 8 == 0);
540 const uint = @IntType(false, T.bit_count);
541 if (uint == u0) {
542 return;
543 }
544 var bits = @bitCast(uint, value);
545 if (uint == u8) {
546 buf[0] = bits;
547 return;
548 }
549 for (buf) |*b| {
550 b.* = @truncate(u8, bits);
551 bits >>= 8;
596pub const writeIntSliceNative = switch (builtin.endian) {
597 builtin.Endian.Little => writeIntSliceLittle,
598 builtin.Endian.Big => writeIntSliceBig,
599};
600
601pub const writeIntSliceForeign = switch (builtin.endian) {
602 builtin.Endian.Little => writeIntSliceBig,
603 builtin.Endian.Big => writeIntSliceLittle,
604};
605
606/// Writes a twos-complement integer to memory, with the specified endianness.
607/// Asserts that buf.len >= @sizeOf(T). Note that @sizeOf(u24) is 3.
608/// The bit count of T must be evenly divisible by 8.
609/// Any extra bytes in buffer not part of the integer are set to zero, with
610/// respect to endianness. To avoid the branch to check for extra buffer bytes,
611/// use writeInt instead.
612pub fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: builtin.Endian) void {
613 comptime assert(T.bit_count % 8 == 0);
614 switch (endian) {
615 builtin.Endian.Little => return writeIntSliceLittle(T, buffer, value),
616 builtin.Endian.Big => return writeIntSliceBig(T, buffer, value),
552617 }
553 assert(bits == 0);
554618}
555619
556test "writeIntBE/LE" {
620test "writeIntBig and writeIntLittle" {
557621 var buf0: [0]u8 = undefined;
558622 var buf1: [1]u8 = undefined;
559623 var buf2: [2]u8 = undefined;
560624 var buf9: [9]u8 = undefined;
561625
562 writeIntBE(u0, &buf0, 0x0);
626 writeIntBig(u0, &buf0, 0x0);
563627 assert(eql_slice_u8(buf0[0..], []u8{}));
564 writeIntLE(u0, &buf0, 0x0);
628 writeIntLittle(u0, &buf0, 0x0);
565629 assert(eql_slice_u8(buf0[0..], []u8{}));
566630
567 writeIntBE(u8, &buf1, 0x12);
631 writeIntBig(u8, &buf1, 0x12);
568632 assert(eql_slice_u8(buf1[0..], []u8{0x12}));
569 writeIntLE(u8, &buf1, 0x34);
633 writeIntLittle(u8, &buf1, 0x34);
570634 assert(eql_slice_u8(buf1[0..], []u8{0x34}));
571635
572 writeIntBE(u16, &buf2, 0x1234);
636 writeIntBig(u16, &buf2, 0x1234);
573637 assert(eql_slice_u8(buf2[0..], []u8{ 0x12, 0x34 }));
574 writeIntLE(u16, &buf2, 0x5678);
638 writeIntLittle(u16, &buf2, 0x5678);
575639 assert(eql_slice_u8(buf2[0..], []u8{ 0x78, 0x56 }));
576640
577 writeIntBE(u72, &buf9, 0x123456789abcdef024);
641 writeIntBig(u72, &buf9, 0x123456789abcdef024);
578642 assert(eql_slice_u8(buf9[0..], []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }));
579 writeIntLE(u72, &buf9, 0xfedcba9876543210ec);
643 writeIntLittle(u72, &buf9, 0xfedcba9876543210ec);
580644 assert(eql_slice_u8(buf9[0..], []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }));
581645
582 writeIntBE(i8, &buf1, -1);
646 writeIntBig(i8, &buf1, -1);
583647 assert(eql_slice_u8(buf1[0..], []u8{0xff}));
584 writeIntLE(i8, &buf1, -2);
648 writeIntLittle(i8, &buf1, -2);
585649 assert(eql_slice_u8(buf1[0..], []u8{0xfe}));
586650
587 writeIntBE(i16, &buf2, -3);
651 writeIntBig(i16, &buf2, -3);
588652 assert(eql_slice_u8(buf2[0..], []u8{ 0xff, 0xfd }));
589 writeIntLE(i16, &buf2, -4);
653 writeIntLittle(i16, &buf2, -4);
590654 assert(eql_slice_u8(buf2[0..], []u8{ 0xfc, 0xff }));
591655}
592656
......@@ -735,12 +799,12 @@ fn testReadIntImpl() void {
735799 0x56,
736800 0x78,
737801 };
738 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
739 assert(readIntBE(u32, bytes) == 0x12345678);
740 assert(readIntBE(i32, bytes) == 0x12345678);
741 assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
742 assert(readIntLE(u32, bytes) == 0x78563412);
743 assert(readIntLE(i32, bytes) == 0x78563412);
802 assert(readInt(u32, &bytes, builtin.Endian.Big) == 0x12345678);
803 assert(readIntBig(u32, &bytes) == 0x12345678);
804 assert(readIntBig(i32, &bytes) == 0x12345678);
805 assert(readInt(u32, &bytes, builtin.Endian.Little) == 0x78563412);
806 assert(readIntLittle(u32, &bytes) == 0x78563412);
807 assert(readIntLittle(i32, &bytes) == 0x78563412);
744808 }
745809 {
746810 const buf = []u8{
......@@ -749,7 +813,7 @@ fn testReadIntImpl() void {
749813 0x12,
750814 0x34,
751815 };
752 const answer = readInt(buf, u64, builtin.Endian.Big);
816 const answer = readInt(u32, &buf, builtin.Endian.Big);
753817 assert(answer == 0x00001234);
754818 }
755819 {
......@@ -759,7 +823,7 @@ fn testReadIntImpl() void {
759823 0x00,
760824 0x00,
761825 };
762 const answer = readInt(buf, u64, builtin.Endian.Little);
826 const answer = readInt(u32, &buf, builtin.Endian.Little);
763827 assert(answer == 0x00003412);
764828 }
765829 {
......@@ -767,21 +831,33 @@ fn testReadIntImpl() void {
767831 0xff,
768832 0xfe,
769833 };
770 assert(readIntBE(u16, bytes) == 0xfffe);
771 assert(readIntBE(i16, bytes) == -0x0002);
772 assert(readIntLE(u16, bytes) == 0xfeff);
773 assert(readIntLE(i16, bytes) == -0x0101);
834 assert(readIntBig(u16, &bytes) == 0xfffe);
835 assert(readIntBig(i16, &bytes) == -0x0002);
836 assert(readIntLittle(u16, &bytes) == 0xfeff);
837 assert(readIntLittle(i16, &bytes) == -0x0101);
774838 }
775839}
776840
777test "testWriteInt" {
841test "std.mem.writeIntSlice" {
778842 testWriteIntImpl();
779843 comptime testWriteIntImpl();
780844}
781845fn testWriteIntImpl() void {
782846 var bytes: [8]u8 = undefined;
783847
784 writeInt(bytes[0..], u64(0x12345678CAFEBABE), builtin.Endian.Big);
848 writeIntSlice(u0, bytes[0..], 0, builtin.Endian.Big);
849 assert(eql(u8, bytes, []u8{
850 0x00, 0x00, 0x00, 0x00,
851 0x00, 0x00, 0x00, 0x00,
852 }));
853
854 writeIntSlice(u0, bytes[0..], 0, builtin.Endian.Little);
855 assert(eql(u8, bytes, []u8{
856 0x00, 0x00, 0x00, 0x00,
857 0x00, 0x00, 0x00, 0x00,
858 }));
859
860 writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, builtin.Endian.Big);
785861 assert(eql(u8, bytes, []u8{
786862 0x12,
787863 0x34,
......@@ -793,7 +869,7 @@ fn testWriteIntImpl() void {
793869 0xBE,
794870 }));
795871
796 writeInt(bytes[0..], u64(0xBEBAFECA78563412), builtin.Endian.Little);
872 writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, builtin.Endian.Little);
797873 assert(eql(u8, bytes, []u8{
798874 0x12,
799875 0x34,
......@@ -805,7 +881,7 @@ fn testWriteIntImpl() void {
805881 0xBE,
806882 }));
807883
808 writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
884 writeIntSlice(u32, bytes[0..], 0x12345678, builtin.Endian.Big);
809885 assert(eql(u8, bytes, []u8{
810886 0x00,
811887 0x00,
......@@ -817,7 +893,7 @@ fn testWriteIntImpl() void {
817893 0x78,
818894 }));
819895
820 writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
896 writeIntSlice(u32, bytes[0..], 0x78563412, builtin.Endian.Little);
821897 assert(eql(u8, bytes, []u8{
822898 0x12,
823899 0x34,
......@@ -829,7 +905,7 @@ fn testWriteIntImpl() void {
829905 0x00,
830906 }));
831907
832 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
908 writeIntSlice(u16, bytes[0..], 0x1234, builtin.Endian.Big);
833909 assert(eql(u8, bytes, []u8{
834910 0x00,
835911 0x00,
......@@ -841,7 +917,7 @@ fn testWriteIntImpl() void {
841917 0x34,
842918 }));
843919
844 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
920 writeIntSlice(u16, bytes[0..], 0x1234, builtin.Endian.Little);
845921 assert(eql(u8, bytes, []u8{
846922 0x34,
847923 0x12,
......@@ -939,29 +1015,52 @@ test "std.mem.rotate" {
9391015 }));
9401016}
9411017
942// TODO: When https://github.com/ziglang/zig/issues/649 is solved these can be done by
943// endian-casting the pointer and then dereferencing
1018/// Converts a little-endian integer to host endianness.
1019pub fn littleToNative(comptime T: type, x: T) T {
1020 return switch (builtin.endian) {
1021 builtin.Endian.Little => x,
1022 builtin.Endian.Big => @bswap(T, x),
1023 };
1024}
9441025
945pub fn endianSwapIfLe(comptime T: type, x: T) T {
946 return endianSwapIf(builtin.Endian.Little, T, x);
1026/// Converts a big-endian integer to host endianness.
1027pub fn bigToNative(comptime T: type, x: T) T {
1028 return switch (builtin.endian) {
1029 builtin.Endian.Little => @bswap(T, x),
1030 builtin.Endian.Big => x,
1031 };
9471032}
9481033
949pub fn endianSwapIfBe(comptime T: type, x: T) T {
950 return endianSwapIf(builtin.Endian.Big, T, x);
1034/// Converts an integer from specified endianness to host endianness.
1035pub fn toNative(comptime T: type, x: T, endianness_of_x: builtin.Endian) T {
1036 return switch (endianness_of_x) {
1037 builtin.Endian.Little => littleToNative(T, x),
1038 builtin.Endian.Big => bigToNative(T, x),
1039 };
9511040}
9521041
953pub fn endianSwapIf(endian: builtin.Endian, comptime T: type, x: T) T {
954 return if (builtin.endian == endian) endianSwap(T, x) else x;
1042/// Converts an integer which has host endianness to the desired endianness.
1043pub fn nativeTo(comptime T: type, x: T, desired_endianness: builtin.Endian) T {
1044 return switch (desired_endianness) {
1045 builtin.Endian.Little => nativeToLittle(T, x),
1046 builtin.Endian.Big => nativeToBig(T, x),
1047 };
9551048}
9561049
957pub fn endianSwap(comptime T: type, x: T) T {
958 var buf: [@sizeOf(T)]u8 = undefined;
959 mem.writeInt(buf[0..], x, builtin.Endian.Little);
960 return mem.readInt(buf, T, builtin.Endian.Big);
1050/// Converts an integer which has host endianness to little endian.
1051pub fn nativeToLittle(comptime T: type, x: T) T {
1052 return switch (builtin.endian) {
1053 builtin.Endian.Little => x,
1054 builtin.Endian.Big => @bswap(T, x),
1055 };
9611056}
9621057
963test "std.mem.endianSwap" {
964 assert(endianSwap(u32, 0xDEADBEEF) == 0xEFBEADDE);
1058/// Converts an integer which has host endianness to big endian.
1059pub fn nativeToBig(comptime T: type, x: T) T {
1060 return switch (builtin.endian) {
1061 builtin.Endian.Little => @bswap(T, x),
1062 builtin.Endian.Big => x,
1063 };
9651064}
9661065
9671066fn AsBytesReturnType(comptime P: type) type {
std/net.zig+6-6
......@@ -23,7 +23,7 @@ pub const Address = struct {
2323 .os_addr = posix.sockaddr{
2424 .in = posix.sockaddr_in{
2525 .family = posix.AF_INET,
26 .port = std.mem.endianSwapIfLe(u16, _port),
26 .port = mem.nativeToBig(u16, _port),
2727 .addr = ip4,
2828 .zero = []u8{0} ** 8,
2929 },
......@@ -37,7 +37,7 @@ pub const Address = struct {
3737 .os_addr = posix.sockaddr{
3838 .in6 = posix.sockaddr_in6{
3939 .family = posix.AF_INET6,
40 .port = std.mem.endianSwapIfLe(u16, _port),
40 .port = mem.nativeToBig(u16, _port),
4141 .flowinfo = 0,
4242 .addr = ip6.addr,
4343 .scope_id = ip6.scope_id,
......@@ -47,7 +47,7 @@ pub const Address = struct {
4747 }
4848
4949 pub fn port(self: Address) u16 {
50 return std.mem.endianSwapIfLe(u16, self.os_addr.in.port);
50 return mem.bigToNative(u16, self.os_addr.in.port);
5151 }
5252
5353 pub fn initPosix(addr: posix.sockaddr) Address {
......@@ -57,12 +57,12 @@ pub const Address = struct {
5757 pub fn format(self: *const Address, out_stream: var) !void {
5858 switch (self.os_addr.in.family) {
5959 posix.AF_INET => {
60 const native_endian_port = std.mem.endianSwapIfLe(u16, self.os_addr.in.port);
60 const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port);
6161 const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]);
6262 try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port);
6363 },
6464 posix.AF_INET6 => {
65 const native_endian_port = std.mem.endianSwapIfLe(u16, self.os_addr.in6.port);
65 const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port);
6666 try out_stream.print("[TODO render ip6 address]:{}", native_endian_port);
6767 },
6868 else => try out_stream.write("(unrecognized address family)"),
......@@ -193,7 +193,7 @@ pub fn parseIp6(buf: []const u8) !Ip6Addr {
193193}
194194
195195test "std.net.parseIp4" {
196 assert((try parseIp4("127.0.0.1")) == std.mem.endianSwapIfLe(u32, 0x7f000001));
196 assert((try parseIp4("127.0.0.1")) == mem.bigToNative(u32, 0x7f000001));
197197
198198 testParseIp4Fail("256.0.0.1", error.Overflow);
199199 testParseIp4Fail("x.0.0.1", error.InvalidCharacter);
std/os/child_process.zig+2-2
......@@ -807,10 +807,10 @@ const ErrInt = @IntType(false, @sizeOf(anyerror) * 8);
807807
808808fn writeIntFd(fd: i32, value: ErrInt) !void {
809809 const stream = &os.File.openHandle(fd).outStream().stream;
810 stream.writeIntNe(ErrInt, value) catch return error.SystemResources;
810 stream.writeIntNative(ErrInt, value) catch return error.SystemResources;
811811}
812812
813813fn readIntFd(fd: i32) !ErrInt {
814814 const stream = &os.File.openHandle(fd).inStream().stream;
815 return stream.readIntNe(ErrInt) catch return error.SystemResources;
815 return stream.readIntNative(ErrInt) catch return error.SystemResources;
816816}
std/os/freebsd/x86_64.zig+3-3
......@@ -98,12 +98,12 @@ pub nakedcc fn restore_rt() void {
9898}
9999
100100pub const msghdr = extern struct {
101 msg_name: &u8,
101 msg_name: *u8,
102102 msg_namelen: socklen_t,
103 msg_iov: &iovec,
103 msg_iov: *iovec,
104104 msg_iovlen: i32,
105105 __pad1: i32,
106 msg_control: &u8,
106 msg_control: *u8,
107107 msg_controllen: socklen_t,
108108 __pad2: socklen_t,
109109 msg_flags: i32,
std/pdb.zig+3-3
......@@ -508,11 +508,11 @@ const Msf = struct {
508508 allocator,
509509 );
510510
511 const stream_count = try self.directory.stream.readIntLe(u32);
511 const stream_count = try self.directory.stream.readIntLittle(u32);
512512
513513 const stream_sizes = try allocator.alloc(u32, stream_count);
514514 for (stream_sizes) |*s| {
515 const size = try self.directory.stream.readIntLe(u32);
515 const size = try self.directory.stream.readIntLittle(u32);
516516 s.* = blockCountFromSize(size, superblock.BlockSize);
517517 }
518518
......@@ -603,7 +603,7 @@ const MsfStream = struct {
603603
604604 var i: u32 = 0;
605605 while (i < block_count) : (i += 1) {
606 stream.blocks[i] = try in.readIntLe(u32);
606 stream.blocks[i] = try in.readIntLittle(u32);
607607 }
608608
609609 return stream;
std/rand/index.zig+6-2
......@@ -5,7 +5,7 @@
55// ```
66// var buf: [8]u8 = undefined;
77// try std.os.getRandomBytes(buf[0..]);
8// const seed = mem.readIntLE(u64, buf[0..8]);
8// const seed = mem.readIntSliceLittle(u64, buf[0..8]);
99//
1010// var r = DefaultPrng.init(seed);
1111//
......@@ -52,7 +52,7 @@ pub const Random = struct {
5252 // use LE instead of native endian for better portability maybe?
5353 // TODO: endian portability is pointless if the underlying prng isn't endian portable.
5454 // TODO: document the endian portability of this library.
55 const byte_aligned_result = mem.readIntLE(ByteAlignedT, rand_bytes);
55 const byte_aligned_result = mem.readIntSliceLittle(ByteAlignedT, rand_bytes);
5656 const unsigned_result = @truncate(UnsignedT, byte_aligned_result);
5757 return @bitCast(T, unsigned_result);
5858 }
......@@ -69,6 +69,7 @@ pub const Random = struct {
6969 return @intCast(T, limitRangeBiased(u64, r.int(u64), less_than));
7070 }
7171 }
72
7273 /// Returns an evenly distributed random unsigned integer `0 <= i < less_than`.
7374 /// This function assumes that the underlying ::fillFn produces evenly distributed values.
7475 /// Within this assumption, the runtime of this function is exponentially distributed.
......@@ -123,6 +124,7 @@ pub const Random = struct {
123124 }
124125 return r.uintLessThanBiased(T, at_most + 1);
125126 }
127
126128 /// Returns an evenly distributed random unsigned integer `0 <= i <= at_most`.
127129 /// See ::uintLessThan, which this function uses in most cases,
128130 /// for commentary on the runtime of this function.
......@@ -151,6 +153,7 @@ pub const Random = struct {
151153 return at_least + r.uintLessThanBiased(T, less_than - at_least);
152154 }
153155 }
156
154157 /// Returns an evenly distributed random integer `at_least <= i < less_than`.
155158 /// See ::uintLessThan, which this function uses in most cases,
156159 /// for commentary on the runtime of this function.
......@@ -185,6 +188,7 @@ pub const Random = struct {
185188 return at_least + r.uintAtMostBiased(T, at_most - at_least);
186189 }
187190 }
191
188192 /// Returns an evenly distributed random integer `at_least <= i <= at_most`.
189193 /// See ::uintLessThan, which this function uses in most cases,
190194 /// for commentary on the runtime of this function.
std/unicode.zig+15-15
......@@ -249,12 +249,12 @@ pub const Utf16LeIterator = struct {
249249 pub fn nextCodepoint(it: *Utf16LeIterator) !?u32 {
250250 assert(it.i <= it.bytes.len);
251251 if (it.i == it.bytes.len) return null;
252 const c0: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]);
252 const c0: u32 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
253253 if (c0 & ~u32(0x03ff) == 0xd800) {
254254 // surrogate pair
255255 it.i += 2;
256256 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
257 const c1: u32 = mem.readIntLE(u16, it.bytes[it.i .. it.i + 2]);
257 const c1: u32 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
258258 if (c1 & ~u32(0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;
259259 it.i += 2;
260260 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));
......@@ -510,46 +510,46 @@ test "utf16leToUtf8" {
510510 const utf16le_as_bytes = @sliceToBytes(utf16le[0..]);
511511
512512 {
513 mem.writeInt(utf16le_as_bytes[0..], u16('A'), builtin.Endian.Little);
514 mem.writeInt(utf16le_as_bytes[2..], u16('a'), builtin.Endian.Little);
513 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A');
514 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a');
515515 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
516516 assert(mem.eql(u8, utf8, "Aa"));
517517 }
518518
519519 {
520 mem.writeInt(utf16le_as_bytes[0..], u16(0x80), builtin.Endian.Little);
521 mem.writeInt(utf16le_as_bytes[2..], u16(0xffff), builtin.Endian.Little);
520 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80);
521 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff);
522522 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
523523 assert(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf"));
524524 }
525525
526526 {
527527 // the values just outside the surrogate half range
528 mem.writeInt(utf16le_as_bytes[0..], u16(0xd7ff), builtin.Endian.Little);
529 mem.writeInt(utf16le_as_bytes[2..], u16(0xe000), builtin.Endian.Little);
528 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff);
529 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000);
530530 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
531531 assert(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80"));
532532 }
533533
534534 {
535535 // smallest surrogate pair
536 mem.writeInt(utf16le_as_bytes[0..], u16(0xd800), builtin.Endian.Little);
537 mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little);
536 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800);
537 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
538538 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
539539 assert(mem.eql(u8, utf8, "\xf0\x90\x80\x80"));
540540 }
541541
542542 {
543543 // largest surrogate pair
544 mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little);
545 mem.writeInt(utf16le_as_bytes[2..], u16(0xdfff), builtin.Endian.Little);
544 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
545 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff);
546546 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
547547 assert(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf"));
548548 }
549549
550550 {
551 mem.writeInt(utf16le_as_bytes[0..], u16(0xdbff), builtin.Endian.Little);
552 mem.writeInt(utf16le_as_bytes[2..], u16(0xdc00), builtin.Endian.Little);
551 mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff);
552 mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00);
553553 const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le);
554554 assert(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80"));
555555 }
......@@ -583,7 +583,7 @@ pub fn utf8ToUtf16Le(utf16le: []u16, utf8: []const u8) !usize {
583583 while (it.nextCodepoint()) |codepoint| {
584584 if (end_index == utf16le_as_bytes.len) return (end_index / 2) + 1;
585585 // TODO surrogate pairs
586 mem.writeInt(utf16le_as_bytes[end_index..], @intCast(u16, codepoint), builtin.Endian.Little);
586 mem.writeIntSliceLittle(u16, utf16le_as_bytes[end_index..], @intCast(u16, codepoint));
587587 end_index += 2;
588588 }
589589 return end_index / 2;
test/behavior.zig+2
......@@ -8,6 +8,7 @@ comptime {
88 _ = @import("cases/atomics.zig");
99 _ = @import("cases/bitcast.zig");
1010 _ = @import("cases/bool.zig");
11 _ = @import("cases/bswap.zig");
1112 _ = @import("cases/bugs/1076.zig");
1213 _ = @import("cases/bugs/1111.zig");
1314 _ = @import("cases/bugs/1277.zig");
......@@ -64,6 +65,7 @@ comptime {
6465 _ = @import("cases/switch_prong_implicit_cast.zig");
6566 _ = @import("cases/syntax.zig");
6667 _ = @import("cases/this.zig");
68 _ = @import("cases/truncate.zig");
6769 _ = @import("cases/try.zig");
6870 _ = @import("cases/type_info.zig");
6971 _ = @import("cases/undefined.zig");
test/cases/bswap.zig created+32
......@@ -0,0 +1,32 @@
1const std = @import("std");
2const assert = std.debug.assert;
3
4test "@bswap" {
5 comptime testByteSwap();
6 testByteSwap();
7}
8
9fn testByteSwap() void {
10 assert(@bswap(u0, 0) == 0);
11 assert(@bswap(u8, 0x12) == 0x12);
12 assert(@bswap(u16, 0x1234) == 0x3412);
13 assert(@bswap(u24, 0x123456) == 0x563412);
14 assert(@bswap(u32, 0x12345678) == 0x78563412);
15 assert(@bswap(u40, 0x123456789a) == 0x9a78563412);
16 assert(@bswap(u48, 0x123456789abc) == 0xbc9a78563412);
17 assert(@bswap(u56, 0x123456789abcde) == 0xdebc9a78563412);
18 assert(@bswap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412);
19 assert(@bswap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412);
20
21 assert(@bswap(i0, 0) == 0);
22 assert(@bswap(i8, -50) == -50);
23 assert(@bswap(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x3412)));
24 assert(@bswap(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x563412)));
25 assert(@bswap(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x78563412)));
26 assert(@bswap(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40(0x9a78563412)));
27 assert(@bswap(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0xbc9a78563412)));
28 assert(@bswap(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0xdebc9a78563412)));
29 assert(@bswap(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0xf1debc9a78563412)));
30 assert(@bswap(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) ==
31 @bitCast(i128, u128(0x8171615141312111f1debc9a78563412)));
32}
test/cases/truncate.zig created+8
......@@ -0,0 +1,8 @@
1const std = @import("std");
2const assert = std.debug.assert;
3
4test "truncate u0 to larger integer allowed and has comptime known result" {
5 var x: u0 = 0;
6 const y = @truncate(u8, x);
7 comptime assert(y == 0);
8}
test/compile_errors.zig+12
......@@ -1,6 +1,18 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "reading past end of pointer casted array",
6 \\comptime {
7 \\ const array = "aoeu";
8 \\ const slice = array[2..];
9 \\ const int_ptr = @ptrCast(*const u24, slice.ptr);
10 \\ const deref = int_ptr.*;
11 \\}
12 ,
13 ".tmp_source.zig:5:26: error: attempt to read 3 bytes from [4]u8 at index 2 which is 2 bytes",
14 );
15
416 cases.add(
517 "error note for function parameter incompatibility",
618 \\fn do_the_thing(func: fn (arg: i32) void) void {}