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 {
63376337 {#header_close#}
63386338
63396339 {#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>
63416341 <p>
6342 This function counts the number of leading zeroes in {#syntax#}x{#endsyntax#} which is an integer
6343 type {#syntax#}T{#endsyntax#}.
6342 This function counts the number of leading zeroes in {#syntax#}integer{#endsyntax#}.
63446343 </p>
63456344 <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#}.
63476347 Otherwise, the return type is an unsigned integer with the minimum number
63486348 of bits that can represent the bit count of the integer type.
63496349 </p>
63506350 <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#}.
63526353 </p>
63536354 {#see_also|@ctz|@popCount#}
63546355 {#header_close#}
......@@ -6478,18 +6479,19 @@ test "main" {
64786479 {#header_close#}
64796480
64806481 {#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>
64826483 <p>
6483 This function counts the number of trailing zeroes in {#syntax#}x{#endsyntax#} which is an integer
6484 type {#syntax#}T{#endsyntax#}.
6484 This function counts the number of trailing zeroes in {#syntax#}integer{#endsyntax#}.
64856485 </p>
64866486 <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#}.
64886489 Otherwise, the return type is an unsigned integer with the minimum number
64896490 of bits that can represent the bit count of the integer type.
64906491 </p>
64916492 <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#}.
64936495 </p>
64946496 {#see_also|@clz|@popCount#}
64956497 {#header_close#}
......@@ -7036,10 +7038,11 @@ test "call foo" {
70367038 {#header_close#}
70377039
70387040 {#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>
70407042 <p>Counts the number of bits set in an integer.</p>
70417043 <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#}.
70437046 Otherwise, the return type is an unsigned integer with the minimum number
70447047 of bits that can represent the bit count of the integer type.
70457048 </p>
src/analyze.cpp+9-23
......@@ -213,6 +213,15 @@ static ZigType *new_container_type_entry(CodeGen *g, ZigTypeId id, AstNode *sour
213213 return entry;
214214}
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
216225AstNode *type_decl_node(ZigType *type_entry) {
217226 switch (type_entry->id) {
218227 case ZigTypeIdInvalid:
......@@ -326,33 +335,10 @@ static bool is_slice(ZigType *type) {
326335 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
327336}
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
346338ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
347339 return get_int_type(g, false, bits_needed_for_unsigned(x));
348340}
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
356342ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
357343 if (result_type != nullptr && result_type->promise_parent != nullptr) {
358344 return result_type->promise_parent;
src/analyze.hpp-1
......@@ -34,7 +34,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
3434ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
3535 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
3636ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
37ZigType *get_smallest_popcount_unsigned_int_type(CodeGen *g, uint64_t x);
3837ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type);
3938ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry);
4039ZigType *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
41264126
41274127 char llvm_name[64];
41284128 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
4129 LLVMTypeRef param_types[3];
4130 switch (n_args) {
4131 case 1:
4132 param_types[0] = get_llvm_type(g, int_type);
4133 break;
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);
4129 LLVMTypeRef param_types[] = {
4130 get_llvm_type(g, int_type),
4131 LLVMInt1Type(),
4132 };
4133 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), param_types, n_args, false);
41424134 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
41434135 assert(LLVMGetIntrinsicID(fn_val));
41444136
src/ir.cpp+82-111
......@@ -10472,6 +10472,20 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
1047210472 return const_val->data.x_type;
1047310473}
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
1047510489static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {
1047610490 if (type_is_invalid(type_value->value.type))
1047710491 return ira->codegen->builtin_types.entry_invalid;
......@@ -17025,123 +17039,93 @@ static IrInstruction *ir_analyze_instruction_optional_unwrap_ptr(IrAnalyze *ira,
1702517039}
1702617040
1702717041static 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);
1702917043 if (type_is_invalid(int_type))
1703017044 return ira->codegen->invalid_instruction;
1703117045
17032 IrInstruction *op = instruction->op->child;
17033
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))
17046 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
17047 if (type_is_invalid(op->value.type))
1704217048 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) {
17047 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
17048 bigint_init_unsigned(&result->value.data.x_bigint, 0);
17049 return result;
17050 }
17051
17052 if (instr_is_comptime(casted_op)) {
17053 size_t result_usize = bigint_ctz(&op->value.data.x_bigint,
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;
17053 if (instr_is_comptime(op)) {
17054 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17055 if (val == nullptr)
17056 return ira->codegen->invalid_instruction;
17057 if (val->special == ConstValSpecialUndef)
17058 return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
17059 size_t result_usize = bigint_ctz(&op->value.data.x_bigint, int_type->data.integral.bit_count);
17060 return ir_const_unsigned(ira, &instruction->base, result_usize);
1705817061 }
1705917062
17063 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
1706017064 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);
1706217066 result->value.type = return_type;
1706317067 return result;
1706417068}
1706517069
1706617070static 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);
1706817072 if (type_is_invalid(int_type))
1706917073 return ira->codegen->invalid_instruction;
1707017074
17071 IrInstruction *op = instruction->op->child;
17072
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))
17075 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
17076 if (type_is_invalid(op->value.type))
1708117077 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) {
17086 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
17087 bigint_init_unsigned(&result->value.data.x_bigint, 0);
17088 return result;
17089 }
17090
17091 if (instr_is_comptime(casted_op)) {
17092 size_t result_usize = bigint_clz(&op->value.data.x_bigint,
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;
17082 if (instr_is_comptime(op)) {
17083 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17084 if (val == nullptr)
17085 return ira->codegen->invalid_instruction;
17086 if (val->special == ConstValSpecialUndef)
17087 return ir_const_undef(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
17088 size_t result_usize = bigint_clz(&op->value.data.x_bigint, int_type->data.integral.bit_count);
17089 return ir_const_unsigned(ira, &instruction->base, result_usize);
1709717090 }
1709817091
17092 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
1709917093 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);
1710117095 result->value.type = return_type;
1710217096 return result;
1710317097}
1710417098
1710517099static 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);
1710717101 if (type_is_invalid(int_type))
1710817102 return ira->codegen->invalid_instruction;
1710917103
17110 IrInstruction *op = instruction->op->child;
17111
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))
17104 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
17105 if (type_is_invalid(op->value.type))
1712017106 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) {
17125 IrInstruction *result = ir_const(ira, &instruction->base, return_type);
17126 bigint_init_unsigned(&result->value.data.x_bigint, 0);
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)
17111 if (instr_is_comptime(op)) {
17112 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
17113 if (val == nullptr)
1713317114 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
1713517118 if (bigint_cmp_zero(&val->data.x_bigint) != CmpLT) {
1713617119 size_t result = bigint_popcount_unsigned(&val->data.x_bigint);
1713717120 return ir_const_unsigned(ira, &instruction->base, result);
1713817121 }
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);
1714017123 return ir_const_unsigned(ira, &instruction->base, result);
1714117124 }
1714217125
17126 ZigType *return_type = get_smallest_unsigned_int_type(ira->codegen, int_type->data.integral.bit_count);
1714317127 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);
1714517129 result->value.type = return_type;
1714617130 return result;
1714717131}
......@@ -23002,28 +22986,13 @@ static IrInstruction *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionS
2300222986}
2300322987
2300422988static 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);
2300622990 if (type_is_invalid(int_type))
2300722991 return ira->codegen->invalid_instruction;
2300822992
23009 IrInstruction *op = instruction->op->child;
23010
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));
22993 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
22994 if (type_is_invalid(op->value.type))
2302522995 return ira->codegen->invalid_instruction;
23026 }
2302722996
2302822997 if (int_type->data.integral.bit_count == 0) {
2302922998 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
......@@ -23031,14 +23000,22 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction
2303123000 return result;
2303223001 }
2303323002
23034 if (int_type->data.integral.bit_count == 8) {
23003 if (int_type->data.integral.bit_count == 8)
2303523004 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;
2303623011 }
2303723012
23038 if (instr_is_comptime(casted_op)) {
23039 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);
23040 if (!val)
23013 if (instr_is_comptime(op)) {
23014 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
23015 if (val == nullptr)
2304123016 return ira->codegen->invalid_instruction;
23017 if (val->special == ConstValSpecialUndef)
23018 return ir_const_undef(ira, &instruction->base, int_type);
2304223019
2304323020 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
2304423021 size_t buf_size = int_type->data.integral.bit_count / 8;
......@@ -23050,26 +23027,18 @@ static IrInstruction *ir_analyze_instruction_bswap(IrAnalyze *ira, IrInstruction
2305023027 }
2305123028
2305223029 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);
2305423031 result->value.type = int_type;
2305523032 return result;
2305623033}
2305723034
2305823035static 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);
2306023037 if (type_is_invalid(int_type))
2306123038 return ira->codegen->invalid_instruction;
2306223039
23063 IrInstruction *op = instruction->op->child;
23064
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))
23040 IrInstruction *op = ir_implicit_cast(ira, instruction->op->child, int_type);
23041 if (type_is_invalid(op->value.type))
2307323042 return ira->codegen->invalid_instruction;
2307423043
2307523044 if (int_type->data.integral.bit_count == 0) {
......@@ -23078,10 +23047,12 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr
2307823047 return result;
2307923048 }
2308023049
23081 if (instr_is_comptime(casted_op)) {
23082 ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad);
23083 if (!val)
23050 if (instr_is_comptime(op)) {
23051 ConstExprValue *val = ir_resolve_const(ira, op, UndefOk);
23052 if (val == nullptr)
2308423053 return ira->codegen->invalid_instruction;
23054 if (val->special == ConstValSpecialUndef)
23055 return ir_const_undef(ira, &instruction->base, int_type);
2308523056
2308623057 IrInstruction *result = ir_const(ira, &instruction->base, int_type);
2308723058 size_t num_bits = int_type->data.integral.bit_count;
......@@ -23111,7 +23082,7 @@ static IrInstruction *ir_analyze_instruction_bit_reverse(IrAnalyze *ira, IrInstr
2311123082 }
2311223083
2311323084 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);
2311523086 result->value.type = int_type;
2311623087 return result;
2311723088}
test/stage1/behavior/bitreverse.zig+11-11
......@@ -9,17 +9,17 @@ test "@bitReverse" {
99
1010fn testBitReverse() void {
1111 // using comptime_ints, unsigned
12 expect(@bitReverse(u0, u0(0)) == 0);
13 expect(@bitReverse(u5, u5(0x12)) == 0x9);
14 expect(@bitReverse(u8, u8(0x12)) == 0x48);
15 expect(@bitReverse(u16, u16(0x1234)) == 0x2c48);
16 expect(@bitReverse(u24, u24(0x123456)) == 0x6a2c48);
17 expect(@bitReverse(u32, u32(0x12345678)) == 0x1e6a2c48);
18 expect(@bitReverse(u40, u40(0x123456789a)) == 0x591e6a2c48);
19 expect(@bitReverse(u48, u48(0x123456789abc)) == 0x3d591e6a2c48);
20 expect(@bitReverse(u56, u56(0x123456789abcde)) == 0x7b3d591e6a2c48);
21 expect(@bitReverse(u64, u64(0x123456789abcdef1)) == 0x8f7b3d591e6a2c48);
22 expect(@bitReverse(u128, u128(0x123456789abcdef11121314151617181)) == 0x818e868a828c84888f7b3d591e6a2c48);
12 expect(@bitReverse(u0, 0) == 0);
13 expect(@bitReverse(u5, 0x12) == 0x9);
14 expect(@bitReverse(u8, 0x12) == 0x48);
15 expect(@bitReverse(u16, 0x1234) == 0x2c48);
16 expect(@bitReverse(u24, 0x123456) == 0x6a2c48);
17 expect(@bitReverse(u32, 0x12345678) == 0x1e6a2c48);
18 expect(@bitReverse(u40, 0x123456789a) == 0x591e6a2c48);
19 expect(@bitReverse(u48, 0x123456789abc) == 0x3d591e6a2c48);
20 expect(@bitReverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48);
21 expect(@bitReverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48);
22 expect(@bitReverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48);
2323
2424 // using runtime uints, unsigned
2525 var num0: u0 = 0;
test/stage1/behavior/bugs/2114.zig+1-1
......@@ -3,7 +3,7 @@ const expect = std.testing.expect;
33const math = std.math;
44
55fn ctz(x: var) usize {
6 return @ctz(u128, x);
6 return @ctz(@typeOf(x), x);
77}
88
99test "fixed" {
test/stage1/behavior/byteswap.zig+10-10
......@@ -7,16 +7,16 @@ test "@byteSwap" {
77}
88
99fn testByteSwap() void {
10 expect(@byteSwap(u0, u0(0)) == 0);
11 expect(@byteSwap(u8, u8(0x12)) == 0x12);
12 expect(@byteSwap(u16, u16(0x1234)) == 0x3412);
13 expect(@byteSwap(u24, u24(0x123456)) == 0x563412);
14 expect(@byteSwap(u32, u32(0x12345678)) == 0x78563412);
15 expect(@byteSwap(u40, u40(0x123456789a)) == 0x9a78563412);
16 expect(@byteSwap(i48, u48(0x123456789abc)) == @bitCast(i48, u48(0xbc9a78563412)));
17 expect(@byteSwap(u56, u56(0x123456789abcde)) == 0xdebc9a78563412);
18 expect(@byteSwap(u64, u64(0x123456789abcdef1)) == 0xf1debc9a78563412);
19 expect(@byteSwap(u128, u128(0x123456789abcdef11121314151617181)) == 0x8171615141312111f1debc9a78563412);
10 expect(@byteSwap(u0, 0) == 0);
11 expect(@byteSwap(u8, 0x12) == 0x12);
12 expect(@byteSwap(u16, 0x1234) == 0x3412);
13 expect(@byteSwap(u24, 0x123456) == 0x563412);
14 expect(@byteSwap(u32, 0x12345678) == 0x78563412);
15 expect(@byteSwap(u40, 0x123456789a) == 0x9a78563412);
16 expect(@byteSwap(i48, 0x123456789abc) == @bitCast(i48, u48(0xbc9a78563412)));
17 expect(@byteSwap(u56, 0x123456789abcde) == 0xdebc9a78563412);
18 expect(@byteSwap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412);
19 expect(@byteSwap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412);
2020
2121 expect(@byteSwap(u0, u0(0)) == 0);
2222 expect(@byteSwap(i8, i8(-50)) == -50);
test/stage1/behavior/math.zig+10-21
......@@ -114,12 +114,12 @@ test "@clz" {
114114}
115115
116116fn testClz() void {
117 expect(clz(u8, u8(0b10001010)) == 0);
118 expect(clz(u8, u8(0b00001010)) == 4);
119 expect(clz(u8, u8(0b00011010)) == 3);
120 expect(clz(u8, u8(0b00000000)) == 8);
121 expect(clz(u128, u128(0xffffffffffffffff)) == 64);
122 expect(clz(u128, u128(0x10000000000000000)) == 63);
117 expect(clz(u8, 0b10001010) == 0);
118 expect(clz(u8, 0b00001010) == 4);
119 expect(clz(u8, 0b00011010) == 3);
120 expect(clz(u8, 0b00000000) == 8);
121 expect(clz(u128, 0xffffffffffffffff) == 64);
122 expect(clz(u128, 0x10000000000000000) == 63);
123123}
124124
125125fn clz(comptime T: type, x: T) usize {
......@@ -132,27 +132,16 @@ test "@ctz" {
132132}
133133
134134fn testCtz() void {
135 expect(ctz(u8, u8(0b10100000)) == 5);
136 expect(ctz(u8, u8(0b10001010)) == 1);
137 expect(ctz(u8, u8(0b00000000)) == 8);
138 expect(ctz(u16, u16(0b00000000)) == 16);
135 expect(ctz(u8, 0b10100000) == 5);
136 expect(ctz(u8, 0b10001010) == 1);
137 expect(ctz(u8, 0b00000000) == 8);
138 expect(ctz(u16, 0b00000000) == 16);
139139}
140140
141141fn ctz(comptime T: type, x: T) usize {
142142 return @ctz(T, x);
143143}
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
156145test "assignment operators" {
157146 var i: u32 = 0;
158147 i += 5;
test/stage1/behavior/popcount.zig+1-1
......@@ -38,7 +38,7 @@ fn testPopCount() void {
3838 expect(@popCount(u8, @bitCast(u8, i8(-120))) == 2);
3939 }
4040 comptime {
41 expect(@popCount(i128, u128(0b11111111000110001100010000100001000011000011100101010001)) == 24);
41 expect(@popCount(i128, 0b11111111000110001100010000100001000011000011100101010001) == 24);
4242 }
4343}
4444