authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-17 17:14:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-17 17:14:35-04:00
log0d117bb0a993b2a0a290f99255c5bf1bf05f187f
tree54abbc90bb6c31746eb0d9b1c8a3269395190900
parent6a98bf3dba6f3ed5b40fe7899899e2a792028be4

fix wrong value for clz, ctz at compile time

closes #418 also make clz, ctz return smaller integer bit widths and use smaller integer bit widths for enum tag types

6 files changed, 30 insertions(+), 20 deletions(-)

src/analyze.cpp+8-9
......@@ -161,18 +161,17 @@ static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *so
161161 return entry;
162162}
163163
164static uint8_t log2_u64(uint64_t x) {
165 return (63 - __builtin_clzll(x));
166}
164167
165// TODO no reason to limit to 8/16/32/64
166168static uint8_t bits_needed_for_unsigned(uint64_t x) {
167 if (x <= UINT8_MAX) {
168 return 8;
169 } else if (x <= UINT16_MAX) {
170 return 16;
171 } else if (x <= UINT32_MAX) {
172 return 32;
173 } else {
174 return 64;
169 if (x == 0) {
170 return 0;
175171 }
172 uint8_t base = log2_u64(x);
173 uint64_t upper = (((uint64_t)1) << base) - 1;
174 return (upper >= x) ? base : (base + 1);
176175}
177176
178177bool type_is_complete(TypeTableEntry *type_entry) {
src/bigint.cpp+3-1
......@@ -95,7 +95,9 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)
9595}
9696
9797static bool bit_at_index(const BigInt *bi, size_t index) {
98 size_t digit_index = bi->digit_count - (index / 64) - 1;
98 size_t digit_index = index / 64;
99 if (digit_index >= bi->digit_count)
100 return false;
99101 size_t digit_bit_index = index % 64;
100102 const uint64_t *digits = bigint_ptr(bi);
101103 uint64_t digit = digits[digit_index];
src/codegen.cpp+8-5
......@@ -2440,25 +2440,27 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, TypeTableEntry *int_type, Bui
24402440}
24412441
24422442static LLVMValueRef ir_render_clz(CodeGen *g, IrExecutable *executable, IrInstructionClz *instruction) {
2443 TypeTableEntry *int_type = instruction->base.value.type;
2443 TypeTableEntry *int_type = instruction->value->value.type;
24442444 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdClz);
24452445 LLVMValueRef operand = ir_llvm_value(g, instruction->value);
24462446 LLVMValueRef params[] {
24472447 operand,
24482448 LLVMConstNull(LLVMInt1Type()),
24492449 };
2450 return LLVMBuildCall(g->builder, fn_val, params, 2, "");
2450 LLVMValueRef wrong_size_int = LLVMBuildCall(g->builder, fn_val, params, 2, "");
2451 return gen_widen_or_shorten(g, false, int_type, instruction->base.value.type, wrong_size_int);
24512452}
24522453
24532454static LLVMValueRef ir_render_ctz(CodeGen *g, IrExecutable *executable, IrInstructionCtz *instruction) {
2454 TypeTableEntry *int_type = instruction->base.value.type;
2455 TypeTableEntry *int_type = instruction->value->value.type;
24552456 LLVMValueRef fn_val = get_int_builtin_fn(g, int_type, BuiltinFnIdCtz);
24562457 LLVMValueRef operand = ir_llvm_value(g, instruction->value);
24572458 LLVMValueRef params[] {
24582459 operand,
24592460 LLVMConstNull(LLVMInt1Type()),
24602461 };
2461 return LLVMBuildCall(g->builder, fn_val, params, 2, "");
2462 LLVMValueRef wrong_size_int = LLVMBuildCall(g->builder, fn_val, params, 2, "");
2463 return gen_widen_or_shorten(g, false, int_type, instruction->base.value.type, wrong_size_int);
24622464}
24632465
24642466static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutable *executable, IrInstructionSwitchBr *instruction) {
......@@ -2545,7 +2547,8 @@ static LLVMValueRef ir_render_enum_tag_name(CodeGen *g, IrExecutable *executable
25452547
25462548 LLVMValueRef indices[] = {
25472549 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
2548 enum_tag_value,
2550 gen_widen_or_shorten(g, false, enum_tag_type->data.enum_tag.int_type,
2551 g->builtin_types.entry_usize, enum_tag_value),
25492552 };
25502553 return LLVMBuildInBoundsGEP(g->builder, enum_tag_type->data.enum_tag.name_table, indices, 2, "");
25512554}
src/ir.cpp+8-4
......@@ -11359,16 +11359,18 @@ static TypeTableEntry *ir_analyze_instruction_ctz(IrAnalyze *ira, IrInstructionC
1135911359 if (type_is_invalid(value->value.type)) {
1136011360 return ira->codegen->builtin_types.entry_invalid;
1136111361 } else if (value->value.type->id == TypeTableEntryIdInt) {
11362 TypeTableEntry *return_type = get_smallest_unsigned_int_type(ira->codegen,
11363 value->value.type->data.integral.bit_count);
1136211364 if (value->value.special != ConstValSpecialRuntime) {
1136311365 size_t result = bigint_ctz(&value->value.data.x_bigint,
1136411366 value->value.type->data.integral.bit_count);
1136511367 ConstExprValue *out_val = ir_build_const_from(ira, &ctz_instruction->base);
1136611368 bigint_init_unsigned(&out_val->data.x_bigint, result);
11367 return value->value.type;
11369 return return_type;
1136811370 }
1136911371
1137011372 ir_build_ctz_from(&ira->new_irb, &ctz_instruction->base, value);
11371 return value->value.type;
11373 return return_type;
1137211374 } else {
1137311375 ir_add_error_node(ira, ctz_instruction->base.source_node,
1137411376 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->value.type->name)));
......@@ -11381,16 +11383,18 @@ static TypeTableEntry *ir_analyze_instruction_clz(IrAnalyze *ira, IrInstructionC
1138111383 if (type_is_invalid(value->value.type)) {
1138211384 return ira->codegen->builtin_types.entry_invalid;
1138311385 } else if (value->value.type->id == TypeTableEntryIdInt) {
11386 TypeTableEntry *return_type = get_smallest_unsigned_int_type(ira->codegen,
11387 value->value.type->data.integral.bit_count);
1138411388 if (value->value.special != ConstValSpecialRuntime) {
1138511389 size_t result = bigint_clz(&value->value.data.x_bigint,
1138611390 value->value.type->data.integral.bit_count);
1138711391 ConstExprValue *out_val = ir_build_const_from(ira, &clz_instruction->base);
1138811392 bigint_init_unsigned(&out_val->data.x_bigint, result);
11389 return value->value.type;
11393 return return_type;
1139011394 }
1139111395
1139211396 ir_build_clz_from(&ira->new_irb, &clz_instruction->base, value);
11393 return value->value.type;
11397 return return_type;
1139411398 } else {
1139511399 ir_add_error_node(ira, clz_instruction->base.source_node,
1139611400 buf_sprintf("expected integer type, found '%s'", buf_ptr(&value->value.type->name)));
std/special/compiler_rt/index.zig+1-1
......@@ -139,7 +139,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
139139 // K X
140140 // ---
141141 // 0 K
142 sr = 1 + n_uword_bits + @clz(su_int(d[low])) - @clz(su_int(n[high]));
142 sr = 1 + n_uword_bits + c_uint(@clz(su_int(d[low]))) - c_uint(@clz(su_int(n[high])));
143143 // 2 <= sr <= n_udword_bits - 1
144144 // q.all = n.all << (n_udword_bits - sr);
145145 // r.all = n.all >> sr;
test/cases/math.zig+2
......@@ -66,6 +66,8 @@ fn testClz() {
6666 assert(clz(u8(0b00001010)) == 4);
6767 assert(clz(u8(0b10001010)) == 0);
6868 assert(clz(u8(0b00000000)) == 8);
69 assert(clz(u128(0xffffffffffffffff)) == 64);
70 assert(clz(u128(0x10000000000000000)) == 63);
6971}
7072
7173fn clz(x: var) -> usize {