authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-16 16:32:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-16 16:37:58-04:00
log80983ca1ca5cdcbd5ce7db017c1987d75cc8184b
tree9a7e19e081e2dc575b9b363c28145f60b18cc32f
parent1fdb24827fb51351d5e31103069619668fae31c4
signaturelock-open Commit is signed but in an unrecognized format.

fixups to the previous commit


10 files changed, 144 insertions(+), 204 deletions(-)

doc/langref.html.in+15-12
...@@ -6337,18 +6337,19 @@ comptime {...@@ -6337,18 +6337,19 @@ comptime {
6337 {#header_close#}6337 {#header_close#}
63386338
6339 {#header_open|@clz#}6339 {#header_open|@clz#}
6340 <pre>{#syntax#}@clz(comptime T: type, integer: T) math.Log2Int(@intType(false, @typeInfo(T).Int.bits + 1)){#endsyntax#}</pre>6340 <pre>{#syntax#}@clz(comptime T: type, integer: T){#endsyntax#}</pre>
6341 <p>6341 <p>
6342 This function counts the number of leading zeroes in {#syntax#}x{#endsyntax#} which is an integer6342 This function counts the number of leading zeroes in {#syntax#}integer{#endsyntax#}.
6343 type {#syntax#}T{#endsyntax#}.
6344 </p>6343 </p>
6345 <p>6344 <p>
6346 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#}, the return type is {#syntax#}comptime_int{#endsyntax#}.6345 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#},
6346 the return type is {#syntax#}comptime_int{#endsyntax#}.
6347 Otherwise, the return type is an unsigned integer with the minimum number6347 Otherwise, the return type is an unsigned integer with the minimum number
6348 of bits that can represent the bit count of the integer type.6348 of bits that can represent the bit count of the integer type.
6349 </p>6349 </p>
6350 <p>6350 <p>
6351 If {#syntax#}x{#endsyntax#} is zero, {#syntax#}@clz{#endsyntax#} returns {#syntax#}T.bit_count{#endsyntax#}.6351 If {#syntax#}integer{#endsyntax#} is zero, {#syntax#}@clz{#endsyntax#} returns the bit width
6352 of integer type {#syntax#}T{#endsyntax#}.
6352 </p>6353 </p>
6353 {#see_also|@ctz|@popCount#}6354 {#see_also|@ctz|@popCount#}
6354 {#header_close#}6355 {#header_close#}
...@@ -6478,18 +6479,19 @@ test "main" {...@@ -6478,18 +6479,19 @@ test "main" {
6478 {#header_close#}6479 {#header_close#}
64796480
6480 {#header_open|@ctz#}6481 {#header_open|@ctz#}
6481 <pre>{#syntax#}@ctz(comptime T: type, integer: T) math.Log2Int(@intType(false, @typeInfo(T).Int.bits + 1)){#endsyntax#}</pre>6482 <pre>{#syntax#}@ctz(comptime T: type, integer: T){#endsyntax#}</pre>
6482 <p>6483 <p>
6483 This function counts the number of trailing zeroes in {#syntax#}x{#endsyntax#} which is an integer6484 This function counts the number of trailing zeroes in {#syntax#}integer{#endsyntax#}.
6484 type {#syntax#}T{#endsyntax#}.
6485 </p>6485 </p>
6486 <p>6486 <p>
6487 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#}, the return type is {#syntax#}comptime_int{#endsyntax#}.6487 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#},
6488 the return type is {#syntax#}comptime_int{#endsyntax#}.
6488 Otherwise, the return type is an unsigned integer with the minimum number6489 Otherwise, the return type is an unsigned integer with the minimum number
6489 of bits that can represent the bit count of the integer type.6490 of bits that can represent the bit count of the integer type.
6490 </p>6491 </p>
6491 <p>6492 <p>
6492 If {#syntax#}x{#endsyntax#} is zero, {#syntax#}@ctz{#endsyntax#} returns {#syntax#}T.bit_count{#endsyntax#}.6493 If {#syntax#}integer{#endsyntax#} is zero, {#syntax#}@ctz{#endsyntax#} returns
6494 the bit width of integer type {#syntax#}T{#endsyntax#}.
6493 </p>6495 </p>
6494 {#see_also|@clz|@popCount#}6496 {#see_also|@clz|@popCount#}
6495 {#header_close#}6497 {#header_close#}
...@@ -7036,10 +7038,11 @@ test "call foo" {...@@ -7036,10 +7038,11 @@ test "call foo" {
7036 {#header_close#}7038 {#header_close#}
70377039
7038 {#header_open|@popCount#}7040 {#header_open|@popCount#}
7039 <pre>{#syntax#}@popCount(comptime T: type, integer: T) math.Log2Int(@intType(false, @typeInfo(T).Int.bits + 1)){#endsyntax#}</pre>7041 <pre>{#syntax#}@popCount(comptime T: type, integer: T){#endsyntax#}</pre>
7040 <p>Counts the number of bits set in an integer.</p>7042 <p>Counts the number of bits set in an integer.</p>
7041 <p>7043 <p>
7042 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#}, the return type is {#syntax#}comptime_int{#endsyntax#}.7044 If {#syntax#}integer{#endsyntax#} is known at {#link|comptime#},
7045 the return type is {#syntax#}comptime_int{#endsyntax#}.
7043 Otherwise, the return type is an unsigned integer with the minimum number7046 Otherwise, the return type is an unsigned integer with the minimum number
7044 of bits that can represent the bit count of the integer type.7047 of bits that can represent the bit count of the integer type.
7045 </p>7048 </p>
src/analyze.cpp+9-23
...@@ -213,6 +213,15 @@ static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *sour...@@ -213,6 +213,15 @@ static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *sour
213 return entry;213 return entry;
214}214}
215215
216static uint8_t bits_needed_for_unsigned(uint64_t x) {
217 if (x == 0) {
218 return 0;
219 }
220 uint8_t base = log2_u64(x);
221 uint64_t upper = (((uint64_t)1) << base) - 1;
222 return (upper >= x) ? base : (base + 1);
223}
224
216AstNode *type_decl_node(ZigType *type_entry) {225AstNode *type_decl_node(ZigType *type_entry) {
217 switch (type_entry->id) {226 switch (type_entry->id) {
218 case ZigTypeIdInvalid:227 case ZigTypeIdInvalid:
...@@ -326,33 +335,10 @@ static bool is_slice(ZigType *type) {...@@ -326,33 +335,10 @@ static bool is_slice(ZigType *type) {
326 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;335 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
327}336}
328337
329static uint8_t bits_needed_for_unsigned(uint64_t x) {
330 if (x == 0) {
331 return 0;
332 }
333 uint8_t base = log2_u64(x);
334 uint64_t upper = (((uint64_t)1) << base) - 1;
335 return (upper >= x) ? base : (base + 1);
336}
337
338static uint8_t bits_needed_for_popcount_unsigned(uint64_t x) {
339 uint8_t count = 0;
340 for (uint64_t s = x;s != 0;s >>= 1)
341 count++;
342
343 return count;
344}
345
346ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {338ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
347 return get_int_type(g, false, bits_needed_for_unsigned(x));339 return get_int_type(g, false, bits_needed_for_unsigned(x));
348}340}
349341
350// This is not the same as above, because while shift by bit width is UB, @clz, @popCount, and @ctz
351// can return bit width
352ZigType *get_smallest_popcount_unsigned_int_type(CodeGen *g, uint64_t x) {
353 return get_int_type(g, false, bits_needed_for_popcount_unsigned(x));
354}
355
356ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {342ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
357 if (result_type != nullptr && result_type->promise_parent != nullptr) {343 if (result_type != nullptr && result_type->promise_parent != nullptr) {
358 return result_type->promise_parent;344 return result_type->promise_parent;
src/analyze.hpp-1
...@@ -34,7 +34,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);...@@ -34,7 +34,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
34ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,34ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
35 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);35 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
36ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);36ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
37ZigType *get_smallest_popcount_unsigned_int_type(CodeGen *g, uint64_t x);
38ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);37ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
39ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);38ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
40ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name);39ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name);
src/codegen.cpp+5-13
...@@ -4126,19 +4126,11 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI...@@ -4126,19 +4126,11 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
41264126
4127 char llvm_name[64];4127 char llvm_name[64];
4128 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);4128 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
4129 LLVMTypeRef param_types[3];4129 LLVMTypeRef param_types[] = {
4130 switch (n_args) {4130 get_llvm_type(g, int_type),
4131 case 1:4131 LLVMInt1Type(),
4132 param_types[0] = get_llvm_type(g, int_type);4132 };
4133 break;4133 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), param_types, n_args, false);
4134 case 2: // clz and ctz
4135 param_types[0] = get_llvm_type(g, int_type);
4136 param_types[1] = LLVMInt1Type();
4137 break;
4138 default:
4139 zig_unreachable();
4140 }
4141 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), &param_types[0], n_args, false);
4142 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);4134 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
4143 assert(LLVMGetIntrinsicID(fn_val));4135 assert(LLVMGetIntrinsicID(fn_val));
41444136
src/ir.cpp+82-111
...@@ -10472,6 +10472,20 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {...@@ -10472,6 +10472,20 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
10472 return const_val->data.x_type;10472 return const_val->data.x_type;
10473}10473}
1047410474
10475static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) {
10476 ZigType *ty = ir_resolve_type(ira, type_value);
10477 if (type_is_invalid(ty))
10478 return ira->codegen->builtin_types.entry_invalid;
10479
10480 if (ty->id != ZigTypeIdInt) {
10481 ir_add_error(ira, type_value,
10482 buf_sprintf("expected integer type, found '%s'", buf_ptr(&ty->name)));
10483 return ira->codegen->builtin_types.entry_invalid;
10484 }
10485
10486 return ty;
10487}
10488
10475static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {10489static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {
10476 if (type_is_invalid(type_value->value.type))10490 if (type_is_invalid(type_value->value.type))
10477 return ira->codegen->builtin_types.entry_invalid;10491 return ira->codegen->builtin_types.entry_invalid;
...@@ -17025,123 +17039,93 @@ static IrInstruction *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira,...@@ -17025,123 +17039,93 @@ static IrInstruction *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira,
17025}17039}
1702617040
17027static IrInstruction *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *instruction) {17041static IrInstruction *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionCtz *instruction) {
17028 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);17042 ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child);
17029 if (type_is_invalid(int_type))17043 if (type_is_invalid(int_type))
17030 return ira->codegen->invalid_instruction;17044 return ira->codegen->invalid_instruction;
1703117045
17032 IrInstruction *op = instruction->op->child;17046 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
1703317047 if (type_is_invalid(op->value.type))
17034 if (int_type->id != ZigTypeIdInt) {
17035 ir_add_error(ira, instruction->type,
17036 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
17037 return ira->codegen->invalid_instruction;
17038 }
17039
17040 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
17041 if (type_is_invalid(casted_op->value.type))
17042 return ira->codegen->invalid_instruction;17048 return ira->codegen->invalid_instruction;
1704317049
17044 ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);17050 if (int_type->data.integral.bit_count == 0)
17051 return ir_const_unsigned(ira, &instruction->base, 0);
1704517052
17046 if (int_type->data.integral.bit_count == 0) {17053 if (instr_is_comptime(op)) {
17047 IrInstruction *result = ir_const(ira, &instruction->base, return_type);17054 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17048 bigint_init_unsigned(&result->value.data.x_bigint, 0);17055 if (val == nullptr)
17049 return result;17056 return ira->codegen->invalid_instruction;
17050 }17057 if (val->special == ConstValSpecialUndef)
1705117058 return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
17052 if (instr_is_comptime(casted_op)) {17059 size_t result_usize = bigint_ctz(&op->value.data.x_bigint, int_type->data.integral.bit_count);
17053 size_t result_usize = bigint_ctz(&op->value.data.x_bigint,17060 return ir_const_unsigned(ira, &instruction->base, result_usize);
17054 op->value.type->data.integral.bit_count);
17055 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
17056 bigint_init_unsigned(&result->value.data.x_bigint, result_usize);
17057 return result;
17058 }17061 }
1705917062
17063 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
17060 IrInstruction *result = ir_build_ctz(&ira->new_irb, instruction->base.scope,17064 IrInstruction *result = ir_build_ctz(&ira->new_irb, instruction->base.scope,
17061 instruction->base.source_node, nullptr, casted_op);17065 instruction->base.source_node, nullptr, op);
17062 result->value.type = return_type;17066 result->value.type = return_type;
17063 return result;17067 return result;
17064}17068}
1706517069
17066static IrInstruction *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionClz *instruction) {17070static IrInstruction *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionClz *instruction) {
17067 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);17071 ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child);
17068 if (type_is_invalid(int_type))17072 if (type_is_invalid(int_type))
17069 return ira->codegen->invalid_instruction;17073 return ira->codegen->invalid_instruction;
1707017074
17071 IrInstruction *op = instruction->op->child;17075 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
1707217076 if (type_is_invalid(op->value.type))
17073 if (int_type->id != ZigTypeIdInt) {
17074 ir_add_error(ira, instruction->type,
17075 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
17076 return ira->codegen->invalid_instruction;
17077 }
17078
17079 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
17080 if (type_is_invalid(casted_op->value.type))
17081 return ira->codegen->invalid_instruction;17077 return ira->codegen->invalid_instruction;
1708217078
17083 ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);17079 if (int_type->data.integral.bit_count == 0)
17080 return ir_const_unsigned(ira, &instruction->base, 0);
1708417081
17085 if (int_type->data.integral.bit_count == 0) {17082 if (instr_is_comptime(op)) {
17086 IrInstruction *result = ir_const(ira, &instruction->base, return_type);17083 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17087 bigint_init_unsigned(&result->value.data.x_bigint, 0);17084 if (val == nullptr)
17088 return result;17085 return ira->codegen->invalid_instruction;
17089 }17086 if (val->special == ConstValSpecialUndef)
1709017087 return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
17091 if (instr_is_comptime(casted_op)) {17088 size_t result_usize = bigint_clz(&op->value.data.x_bigint, int_type->data.integral.bit_count);
17092 size_t result_usize = bigint_clz(&op->value.data.x_bigint,17089 return ir_const_unsigned(ira, &instruction->base, result_usize);
17093 op->value.type->data.integral.bit_count);
17094 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
17095 bigint_init_unsigned(&result->value.data.x_bigint, result_usize);
17096 return result;
17097 }17090 }
1709817091
17092 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
17099 IrInstruction *result = ir_build_clz(&ira->new_irb, instruction->base.scope,17093 IrInstruction *result = ir_build_clz(&ira->new_irb, instruction->base.scope,
17100 instruction->base.source_node, nullptr, casted_op);17094 instruction->base.source_node, nullptr, op);
17101 result->value.type = return_type;17095 result->value.type = return_type;
17102 return result;17096 return result;
17103}17097}
1710417098
17105static IrInstruction *ir_analyze_instruction_pop_count(IrAnalyze *ira, IrInstructionPopCount *instruction) {17099static IrInstruction *ir_analyze_instruction_pop_count(IrAnalyze *ira, IrInstructionPopCount *instruction) {
17106 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);17100 ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child);
17107 if (type_is_invalid(int_type))17101 if (type_is_invalid(int_type))
17108 return ira->codegen->invalid_instruction;17102 return ira->codegen->invalid_instruction;
1710917103
17110 IrInstruction *op = instruction->op->child;17104 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
1711117105 if (type_is_invalid(op->value.type))
17112 if (int_type->id != ZigTypeIdInt) {
17113 ir_add_error(ira, instruction->type,
17114 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
17115 return ira->codegen->invalid_instruction;
17116 }
17117
17118 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
17119 if (type_is_invalid(casted_op->value.type))
17120 return ira->codegen->invalid_instruction;17106 return ira->codegen->invalid_instruction;
1712117107
17122 ZigType *return_type = get_smallest_popcount_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);17108 if (int_type->data.integral.bit_count == 0)
17109 return ir_const_unsigned(ira, &instruction->base, 0);
1712317110
17124 if (int_type->data.integral.bit_count == 0) {17111 if (instr_is_comptime(op)) {
17125 IrInstruction *result = ir_const(ira, &instruction->base, return_type);17112 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17126 bigint_init_unsigned(&result->value.data.x_bigint, 0);17113 if (val == nullptr)
17127 return result;
17128 }
17129
17130 if (instr_is_comptime(casted_op)) {
17131 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);
17132 if (!val)
17133 return ira->codegen->invalid_instruction;17114 return ira->codegen->invalid_instruction;
17115 if (val->special == ConstValSpecialUndef)
17116 return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
1713417117
17135 if (bigint_cmp_zero(&val->data.x_bigint) != CmpLT) {17118 if (bigint_cmp_zero(&val->data.x_bigint) != CmpLT) {
17136 size_t result = bigint_popcount_unsigned(&val->data.x_bigint);17119 size_t result = bigint_popcount_unsigned(&val->data.x_bigint);
17137 return ir_const_unsigned(ira, &instruction->base, result);17120 return ir_const_unsigned(ira, &instruction->base, result);
17138 }17121 }
17139 size_t result = bigint_popcount_signed(&val->data.x_bigint, op->value.type->data.integral.bit_count);17122 size_t result = bigint_popcount_signed(&val->data.x_bigint, int_type->data.integral.bit_count);
17140 return ir_const_unsigned(ira, &instruction->base, result);17123 return ir_const_unsigned(ira, &instruction->base, result);
17141 }17124 }
1714217125
17126 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
17143 IrInstruction *result = ir_build_pop_count(&ira->new_irb, instruction->base.scope,17127 IrInstruction *result = ir_build_pop_count(&ira->new_irb, instruction->base.scope,
17144 instruction->base.source_node, nullptr, casted_op);17128 instruction->base.source_node, nullptr, op);
17145 result->value.type = return_type;17129 result->value.type = return_type;
17146 return result;17130 return result;
17147}17131}
...@@ -23002,28 +22986,13 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS...@@ -23002,28 +22986,13 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS
23002}22986}
2300322987
23004static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstructionBswap *instruction) {22988static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstructionBswap *instruction) {
23005 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);22989 ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child);
23006 if (type_is_invalid(int_type))22990 if (type_is_invalid(int_type))
23007 return ira->codegen->invalid_instruction;22991 return ira->codegen->invalid_instruction;
2300822992
23009 IrInstruction *op = instruction->op->child;22993 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
2301022994 if (type_is_invalid(op->value.type))
23011 if (int_type->id != ZigTypeIdInt) {
23012 ir_add_error(ira, instruction->type,
23013 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
23014 return ira->codegen->invalid_instruction;
23015 }
23016
23017 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
23018 if (type_is_invalid(casted_op->value.type))
23019 return ira->codegen->invalid_instruction;
23020
23021 if (int_type->data.integral.bit_count % 8 != 0) {
23022 ir_add_error(ira, instruction->op,
23023 buf_sprintf("@byteSwap integer type '%s' has %" PRIu32 " bits which is not evenly divisible by 8",
23024 buf_ptr(&int_type->name), int_type->data.integral.bit_count));
23025 return ira->codegen->invalid_instruction;22995 return ira->codegen->invalid_instruction;
23026 }
2302722996
23028 if (int_type->data.integral.bit_count == 0) {22997 if (int_type->data.integral.bit_count == 0) {
23029 IrInstruction *result = ir_const(ira, &instruction->base, int_type);22998 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
...@@ -23031,14 +23000,22 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction...@@ -23031,14 +23000,22 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction
23031 return result;23000 return result;
23032 }23001 }
2303323002
23034 if (int_type->data.integral.bit_count == 8) {23003 if (int_type->data.integral.bit_count == 8)
23035 return op;23004 return op;
23005
23006 if (int_type->data.integral.bit_count % 8 != 0) {
23007 ir_add_error(ira, instruction->op,
23008 buf_sprintf("@byteSwap integer type '%s' has %" PRIu32 " bits which is not evenly divisible by 8",
23009 buf_ptr(&int_type->name), int_type->data.integral.bit_count));
23010 return ira->codegen->invalid_instruction;
23036 }23011 }
2303723012
23038 if (instr_is_comptime(casted_op)) {23013 if (instr_is_comptime(op)) {
23039 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);23014 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
23040 if (!val)23015 if (val == nullptr)
23041 return ira->codegen->invalid_instruction;23016 return ira->codegen->invalid_instruction;
23017 if (val->special == ConstValSpecialUndef)
23018 return ir_const_undef(ira, &instruction->base, int_type);
2304223019
23043 IrInstruction *result = ir_const(ira, &instruction->base, int_type);23020 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
23044 size_t buf_size = int_type->data.integral.bit_count / 8;23021 size_t buf_size = int_type->data.integral.bit_count / 8;
...@@ -23050,26 +23027,18 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction...@@ -23050,26 +23027,18 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction
23050 }23027 }
2305123028
23052 IrInstruction *result = ir_build_bswap(&ira->new_irb, instruction->base.scope,23029 IrInstruction *result = ir_build_bswap(&ira->new_irb, instruction->base.scope,
23053 instruction->base.source_node, nullptr, casted_op);23030 instruction->base.source_node, nullptr, op);
23054 result->value.type = int_type;23031 result->value.type = int_type;
23055 return result;23032 return result;
23056}23033}
2305723034
23058static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstructionBitReverse *instruction) {23035static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstructionBitReverse *instruction) {
23059 ZigType *int_type = ir_resolve_type(ira, instruction->type->child);23036 ZigType *int_type = ir_resolve_int_type(ira, instruction->type->child);
23060 if (type_is_invalid(int_type))23037 if (type_is_invalid(int_type))
23061 return ira->codegen->invalid_instruction;23038 return ira->codegen->invalid_instruction;
2306223039
23063 IrInstruction *op = instruction->op->child;23040 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
2306423041 if (type_is_invalid(op->value.type))
23065 if (int_type->id != ZigTypeIdInt) {
23066 ir_add_error(ira, instruction->type,
23067 buf_sprintf("expected integer type, found '%s'", buf_ptr(&int_type->name)));
23068 return ira->codegen->invalid_instruction;
23069 }
23070
23071 IrInstruction *casted_op = ir_implicit_cast(ira, op, int_type);
23072 if (type_is_invalid(casted_op->value.type))
23073 return ira->codegen->invalid_instruction;23042 return ira->codegen->invalid_instruction;
2307423043
23075 if (int_type->data.integral.bit_count == 0) {23044 if (int_type->data.integral.bit_count == 0) {
...@@ -23078,10 +23047,12 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr...@@ -23078,10 +23047,12 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr
23078 return result;23047 return result;
23079 }23048 }
2308023049
23081 if (instr_is_comptime(casted_op)) {23050 if (instr_is_comptime(op)) {
23082 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);23051 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
23083 if (!val)23052 if (val == nullptr)
23084 return ira->codegen->invalid_instruction;23053 return ira->codegen->invalid_instruction;
23054 if (val->special == ConstValSpecialUndef)
23055 return ir_const_undef(ira, &instruction->base, int_type);
2308523056
23086 IrInstruction *result = ir_const(ira, &instruction->base, int_type);23057 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
23087 size_t num_bits = int_type->data.integral.bit_count;23058 size_t num_bits = int_type->data.integral.bit_count;
...@@ -23111,7 +23082,7 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr...@@ -23111,7 +23082,7 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr
23111 }23082 }
2311223083
23113 IrInstruction *result = ir_build_bit_reverse(&ira->new_irb, instruction->base.scope,23084 IrInstruction *result = ir_build_bit_reverse(&ira->new_irb, instruction->base.scope,
23114 instruction->base.source_node, nullptr, casted_op);23085 instruction->base.source_node, nullptr, op);
23115 result->value.type = int_type;23086 result->value.type = int_type;
23116 return result;23087 return result;
23117}23088}
test/stage1/behavior/bitreverse.zig+11-11
...@@ -9,17 +9,17 @@ test "@bitReverse" {...@@ -9,17 +9,17 @@ test "@bitReverse" {
99
10fn testBitReverse() void {10fn testBitReverse() void {
11 // using comptime_ints, unsigned11 // using comptime_ints, unsigned
12 expect(@bitReverse(u0, u0(0)) == 0);12 expect(@bitReverse(u0, 0) == 0);
13 expect(@bitReverse(u5, u5(0x12)) == 0x9);13 expect(@bitReverse(u5, 0x12) == 0x9);
14 expect(@bitReverse(u8, u8(0x12)) == 0x48);14 expect(@bitReverse(u8, 0x12) == 0x48);
15 expect(@bitReverse(u16, u16(0x1234)) == 0x2c48);15 expect(@bitReverse(u16, 0x1234) == 0x2c48);
16 expect(@bitReverse(u24, u24(0x123456)) == 0x6a2c48);16 expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
17 expect(@bitReverse(u32, u32(0x12345678)) == 0x1e6a2c48);17 expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
18 expect(@bitReverse(u40, u40(0x123456789a)) == 0x591e6a2c48);18 expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
19 expect(@bitReverse(u48, u48(0x123456789abc)) == 0x3d591e6a2c48);19 expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 expect(@bitReverse(u56, u56(0x123456789abcde)) == 0x7b3d591e6a2c48);20 expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 expect(@bitReverse(u64, u64(0x123456789abcdef1)) == 0x8f7b3d591e6a2c48);21 expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 expect(@bitReverse(u128, u128(0x123456789abcdef11121314151617181)) == 0x818e868a828c84888f7b3d591e6a2c48);22 expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
2323
24 // using runtime uints, unsigned24 // using runtime uints, unsigned
25 var num0: u0 = 0;25 var num0: u0 = 0;
test/stage1/behavior/bugs/2114.zig+1-1
...@@ -3,7 +3,7 @@ const expect = std.testing.expect;...@@ -3,7 +3,7 @@ const expect = std.testing.expect;
3const math = std.math;3const math = std.math;
44
5fn ctz(x: var) usize {5fn ctz(x: var) usize {
6 return @ctz(u128, x);6 return @ctz(@typeOf(x), x);
7}7}
88
9test "fixed" {9test "fixed" {
test/stage1/behavior/byteswap.zig+10-10
...@@ -7,16 +7,16 @@ test "@byteSwap" {...@@ -7,16 +7,16 @@ test "@byteSwap" {
7}7}
88
9fn testByteSwap() void {9fn testByteSwap() void {
10 expect(@byteSwap(u0, u0(0)) == 0);10 expect(@byteSwap(u0, 0) == 0);
11 expect(@byteSwap(u8, u8(0x12)) == 0x12);11 expect(@byteSwap(u8, 0x12) == 0x12);
12 expect(@byteSwap(u16, u16(0x1234)) == 0x3412);12 expect(@byteSwap(u16, 0x1234) == 0x3412);
13 expect(@byteSwap(u24, u24(0x123456)) == 0x563412);13 expect(@byteSwap(u24, 0x123456) == 0x563412);
14 expect(@byteSwap(u32, u32(0x12345678)) == 0x78563412);14 expect(@byteSwap(u32, 0x12345678) == 0x78563412);
15 expect(@byteSwap(u40, u40(0x123456789a)) == 0x9a78563412);15 expect(@byteSwap(u40, 0x123456789a) == 0x9a78563412);
16 expect(@byteSwap(i48, u48(0x123456789abc)) == @bitCast(i48, u48(0xbc9a78563412)));16 expect(@byteSwap(i48, 0x123456789abc) == @bitCast(i48, u48(0xbc9a78563412)));
17 expect(@byteSwap(u56, u56(0x123456789abcde)) == 0xdebc9a78563412);17 expect(@byteSwap(u56, 0x123456789abcde) == 0xdebc9a78563412);
18 expect(@byteSwap(u64, u64(0x123456789abcdef1)) == 0xf1debc9a78563412);18 expect(@byteSwap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412);
19 expect(@byteSwap(u128, u128(0x123456789abcdef11121314151617181)) == 0x8171615141312111f1debc9a78563412);19 expect(@byteSwap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412);
2020
21 expect(@byteSwap(u0, u0(0)) == 0);21 expect(@byteSwap(u0, u0(0)) == 0);
22 expect(@byteSwap(i8, i8(-50)) == -50);22 expect(@byteSwap(i8, i8(-50)) == -50);
test/stage1/behavior/math.zig+10-21
...@@ -114,12 +114,12 @@ test "@clz" {...@@ -114,12 +114,12 @@ test "@clz" {
114}114}
115115
116fn testClz() void {116fn testClz() void {
117 expect(clz(u8, u8(0b10001010)) == 0);117 expect(clz(u8, 0b10001010) == 0);
118 expect(clz(u8, u8(0b00001010)) == 4);118 expect(clz(u8, 0b00001010) == 4);
119 expect(clz(u8, u8(0b00011010)) == 3);119 expect(clz(u8, 0b00011010) == 3);
120 expect(clz(u8, u8(0b00000000)) == 8);120 expect(clz(u8, 0b00000000) == 8);
121 expect(clz(u128, u128(0xffffffffffffffff)) == 64);121 expect(clz(u128, 0xffffffffffffffff) == 64);
122 expect(clz(u128, u128(0x10000000000000000)) == 63);122 expect(clz(u128, 0x10000000000000000) == 63);
123}123}
124124
125fn clz(comptime T: type, x: T) usize {125fn clz(comptime T: type, x: T) usize {
...@@ -132,27 +132,16 @@ test "@ctz" {...@@ -132,27 +132,16 @@ test "@ctz" {
132}132}
133133
134fn testCtz() void {134fn testCtz() void {
135 expect(ctz(u8, u8(0b10100000)) == 5);135 expect(ctz(u8, 0b10100000) == 5);
136 expect(ctz(u8, u8(0b10001010)) == 1);136 expect(ctz(u8, 0b10001010) == 1);
137 expect(ctz(u8, u8(0b00000000)) == 8);137 expect(ctz(u8, 0b00000000) == 8);
138 expect(ctz(u16, u16(0b00000000)) == 16);138 expect(ctz(u16, 0b00000000) == 16);
139}139}
140140
141fn ctz(comptime T: type, x: T) usize {141fn ctz(comptime T: type, x: T) usize {
142 return @ctz(T, x);142 return @ctz(T, x);
143}143}
144144
145pub fn Log2Int(comptime T: type) type {
146 // comptime ceil log2
147 comptime var count = 0;
148 comptime var s = T.bit_count - 1;
149 inline while (s != 0) : (s >>= 1) {
150 count += 1;
151 }
152
153 return @IntType(false, count);
154}
155
156test "assignment operators" {145test "assignment operators" {
157 var i: u32 = 0;146 var i: u32 = 0;
158 i += 5;147 i += 5;
test/stage1/behavior/popcount.zig+1-1
...@@ -38,7 +38,7 @@ fn testPopCount() void {...@@ -38,7 +38,7 @@ fn testPopCount() void {
38 expect(@popCount(u8, @bitCast(u8, i8(-120))) == 2);38 expect(@popCount(u8, @bitCast(u8, i8(-120))) == 2);
39 }39 }
40 comptime {40 comptime {
41 expect(@popCount(i128, u128(0b11111111000110001100010000100001000011000011100101010001)) == 24);41 expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
42 }42 }
43}43}
4444