authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-17 14:40:07-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-06-17 14:40:07-04:00
log431fda414189e012252614c7cf3702b46b305e35
tree929ff496c8b03785eb425329318f5b38170d6962
parente5956f23ca702b79a3a4b0f0440a2fe88e0231e5
parent74ccf56a4b1da78b6cd6b0ac34dd6ded1e15b155
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1123 from ziglang/remove-number-casting-syntax

Remove number casting syntax

90 files changed, 820 insertions(+), 435 deletions(-)

doc/langref.html.in+6-6
......@@ -1355,7 +1355,7 @@ var some_integers: [100]i32 = undefined;
13551355
13561356test "modify an array" {
13571357 for (some_integers) |*item, i| {
1358 item.* = i32(i);
1358 item.* = @intCast(i32, i);
13591359 }
13601360 assert(some_integers[10] == 10);
13611361 assert(some_integers[99] == 99);
......@@ -1397,8 +1397,8 @@ var fancy_array = init: {
13971397 var initial_value: [10]Point = undefined;
13981398 for (initial_value) |*pt, i| {
13991399 pt.* = Point{
1400 .x = i32(i),
1401 .y = i32(i) * 2,
1400 .x = @intCast(i32, i),
1401 .y = @intCast(i32, i) * 2,
14021402 };
14031403 }
14041404 break :init initial_value;
......@@ -2410,7 +2410,7 @@ test "for basics" {
24102410 var sum2: i32 = 0;
24112411 for (items) |value, i| {
24122412 assert(@typeOf(i) == usize);
2413 sum2 += i32(i);
2413 sum2 += @intCast(i32, i);
24142414 }
24152415 assert(sum2 == 10);
24162416}
......@@ -5730,7 +5730,7 @@ comptime {
57305730 {#code_begin|test_err|attempt to cast negative value to unsigned integer#}
57315731comptime {
57325732 const value: i32 = -1;
5733 const unsigned = u32(value);
5733 const unsigned = @intCast(u32, value);
57345734}
57355735 {#code_end#}
57365736 <p>At runtime crashes with the message <code>attempt to cast negative value to unsigned integer</code> and a stack trace.</p>
......@@ -5744,7 +5744,7 @@ comptime {
57445744 {#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#}
57455745comptime {
57465746 const spartan_count: u16 = 300;
5747 const byte = u8(spartan_count);
5747 const byte = @intCast(u8, spartan_count);
57485748}
57495749 {#code_end#}
57505750 <p>At runtime crashes with the message <code>integer cast truncated bits</code> and a stack trace.</p>
example/hello_world/hello_libc.zig+1-1
......@@ -8,7 +8,7 @@ const c = @cImport({
88const msg = c"Hello, world!\n";
99
1010export fn main(argc: c_int, argv: **u8) c_int {
11 if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
11 if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;
1212
1313 return 0;
1414}
src/all_types.hpp+36
......@@ -1357,6 +1357,10 @@ enum BuiltinFnId {
13571357 BuiltinFnIdMod,
13581358 BuiltinFnIdSqrt,
13591359 BuiltinFnIdTruncate,
1360 BuiltinFnIdIntCast,
1361 BuiltinFnIdFloatCast,
1362 BuiltinFnIdIntToFloat,
1363 BuiltinFnIdFloatToInt,
13601364 BuiltinFnIdIntType,
13611365 BuiltinFnIdSetCold,
13621366 BuiltinFnIdSetRuntimeSafety,
......@@ -2040,6 +2044,10 @@ enum IrInstructionId {
20402044 IrInstructionIdCmpxchg,
20412045 IrInstructionIdFence,
20422046 IrInstructionIdTruncate,
2047 IrInstructionIdIntCast,
2048 IrInstructionIdFloatCast,
2049 IrInstructionIdIntToFloat,
2050 IrInstructionIdFloatToInt,
20432051 IrInstructionIdIntType,
20442052 IrInstructionIdBoolNot,
20452053 IrInstructionIdMemset,
......@@ -2632,6 +2640,34 @@ struct IrInstructionTruncate {
26322640 IrInstruction *target;
26332641};
26342642
2643struct IrInstructionIntCast {
2644 IrInstruction base;
2645
2646 IrInstruction *dest_type;
2647 IrInstruction *target;
2648};
2649
2650struct IrInstructionFloatCast {
2651 IrInstruction base;
2652
2653 IrInstruction *dest_type;
2654 IrInstruction *target;
2655};
2656
2657struct IrInstructionIntToFloat {
2658 IrInstruction base;
2659
2660 IrInstruction *dest_type;
2661 IrInstruction *target;
2662};
2663
2664struct IrInstructionFloatToInt {
2665 IrInstruction base;
2666
2667 IrInstruction *dest_type;
2668 IrInstruction *target;
2669};
2670
26352671struct IrInstructionIntType {
26362672 IrInstruction base;
26372673
src/codegen.cpp+8
......@@ -4722,6 +4722,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
47224722 case IrInstructionIdPromiseResultType:
47234723 case IrInstructionIdAwaitBookkeeping:
47244724 case IrInstructionIdAddImplicitReturnType:
4725 case IrInstructionIdIntCast:
4726 case IrInstructionIdFloatCast:
4727 case IrInstructionIdIntToFloat:
4728 case IrInstructionIdFloatToInt:
47254729 zig_unreachable();
47264730
47274731 case IrInstructionIdReturn:
......@@ -6310,6 +6314,10 @@ static void define_builtin_fns(CodeGen *g) {
63106314 create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6);
63116315 create_builtin_fn(g, BuiltinFnIdFence, "fence", 1);
63126316 create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2);
6317 create_builtin_fn(g, BuiltinFnIdIntCast, "intCast", 2);
6318 create_builtin_fn(g, BuiltinFnIdFloatCast, "floatCast", 2);
6319 create_builtin_fn(g, BuiltinFnIdIntToFloat, "intToFloat", 2);
6320 create_builtin_fn(g, BuiltinFnIdFloatToInt, "floatToInt", 2);
63136321 create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1);
63146322 create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
63156323 create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int
src/ir.cpp+269-21
......@@ -460,6 +460,22 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTruncate *) {
460460 return IrInstructionIdTruncate;
461461}
462462
463static constexpr IrInstructionId ir_instruction_id(IrInstructionIntCast *) {
464 return IrInstructionIdIntCast;
465}
466
467static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatCast *) {
468 return IrInstructionIdFloatCast;
469}
470
471static constexpr IrInstructionId ir_instruction_id(IrInstructionIntToFloat *) {
472 return IrInstructionIdIntToFloat;
473}
474
475static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatToInt *) {
476 return IrInstructionIdFloatToInt;
477}
478
463479static constexpr IrInstructionId ir_instruction_id(IrInstructionIntType *) {
464480 return IrInstructionIdIntType;
465481}
......@@ -1899,10 +1915,48 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s
18991915 return &instruction->base;
19001916}
19011917
1902static IrInstruction *ir_build_truncate_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *dest_type, IrInstruction *target) {
1903 IrInstruction *new_instruction = ir_build_truncate(irb, old_instruction->scope, old_instruction->source_node, dest_type, target);
1904 ir_link_new_instruction(new_instruction, old_instruction);
1905 return new_instruction;
1918static IrInstruction *ir_build_int_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
1919 IrInstructionIntCast *instruction = ir_build_instruction<IrInstructionIntCast>(irb, scope, source_node);
1920 instruction->dest_type = dest_type;
1921 instruction->target = target;
1922
1923 ir_ref_instruction(dest_type, irb->current_basic_block);
1924 ir_ref_instruction(target, irb->current_basic_block);
1925
1926 return &instruction->base;
1927}
1928
1929static IrInstruction *ir_build_float_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
1930 IrInstructionFloatCast *instruction = ir_build_instruction<IrInstructionFloatCast>(irb, scope, source_node);
1931 instruction->dest_type = dest_type;
1932 instruction->target = target;
1933
1934 ir_ref_instruction(dest_type, irb->current_basic_block);
1935 ir_ref_instruction(target, irb->current_basic_block);
1936
1937 return &instruction->base;
1938}
1939
1940static IrInstruction *ir_build_int_to_float(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
1941 IrInstructionIntToFloat *instruction = ir_build_instruction<IrInstructionIntToFloat>(irb, scope, source_node);
1942 instruction->dest_type = dest_type;
1943 instruction->target = target;
1944
1945 ir_ref_instruction(dest_type, irb->current_basic_block);
1946 ir_ref_instruction(target, irb->current_basic_block);
1947
1948 return &instruction->base;
1949}
1950
1951static IrInstruction *ir_build_float_to_int(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
1952 IrInstructionFloatToInt *instruction = ir_build_instruction<IrInstructionFloatToInt>(irb, scope, source_node);
1953 instruction->dest_type = dest_type;
1954 instruction->target = target;
1955
1956 ir_ref_instruction(dest_type, irb->current_basic_block);
1957 ir_ref_instruction(target, irb->current_basic_block);
1958
1959 return &instruction->base;
19061960}
19071961
19081962static IrInstruction *ir_build_int_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *is_signed, IrInstruction *bit_count) {
......@@ -3957,6 +4011,66 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
39574011 IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value);
39584012 return ir_lval_wrap(irb, scope, truncate, lval);
39594013 }
4014 case BuiltinFnIdIntCast:
4015 {
4016 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4017 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4018 if (arg0_value == irb->codegen->invalid_instruction)
4019 return arg0_value;
4020
4021 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4022 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4023 if (arg1_value == irb->codegen->invalid_instruction)
4024 return arg1_value;
4025
4026 IrInstruction *result = ir_build_int_cast(irb, scope, node, arg0_value, arg1_value);
4027 return ir_lval_wrap(irb, scope, result, lval);
4028 }
4029 case BuiltinFnIdFloatCast:
4030 {
4031 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4032 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4033 if (arg0_value == irb->codegen->invalid_instruction)
4034 return arg0_value;
4035
4036 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4037 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4038 if (arg1_value == irb->codegen->invalid_instruction)
4039 return arg1_value;
4040
4041 IrInstruction *result = ir_build_float_cast(irb, scope, node, arg0_value, arg1_value);
4042 return ir_lval_wrap(irb, scope, result, lval);
4043 }
4044 case BuiltinFnIdIntToFloat:
4045 {
4046 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4047 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4048 if (arg0_value == irb->codegen->invalid_instruction)
4049 return arg0_value;
4050
4051 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4052 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4053 if (arg1_value == irb->codegen->invalid_instruction)
4054 return arg1_value;
4055
4056 IrInstruction *result = ir_build_int_to_float(irb, scope, node, arg0_value, arg1_value);
4057 return ir_lval_wrap(irb, scope, result, lval);
4058 }
4059 case BuiltinFnIdFloatToInt:
4060 {
4061 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4062 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4063 if (arg0_value == irb->codegen->invalid_instruction)
4064 return arg0_value;
4065
4066 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4067 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4068 if (arg1_value == irb->codegen->invalid_instruction)
4069 return arg1_value;
4070
4071 IrInstruction *result = ir_build_float_to_int(irb, scope, node, arg0_value, arg1_value);
4072 return ir_lval_wrap(irb, scope, result, lval);
4073 }
39604074 case BuiltinFnIdIntType:
39614075 {
39624076 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -9948,34 +10062,37 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
994810062 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);
994910063 }
995010064
9951 // explicit widening or shortening cast
9952 if ((wanted_type->id == TypeTableEntryIdInt &&
9953 actual_type->id == TypeTableEntryIdInt) ||
9954 (wanted_type->id == TypeTableEntryIdFloat &&
9955 actual_type->id == TypeTableEntryIdFloat))
10065 // explicit widening conversion
10066 if (wanted_type->id == TypeTableEntryIdInt &&
10067 actual_type->id == TypeTableEntryIdInt &&
10068 wanted_type->data.integral.is_signed == actual_type->data.integral.is_signed &&
10069 wanted_type->data.integral.bit_count >= actual_type->data.integral.bit_count)
995610070 {
995710071 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
995810072 }
995910073
9960 // explicit error set cast
9961 if (wanted_type->id == TypeTableEntryIdErrorSet &&
9962 actual_type->id == TypeTableEntryIdErrorSet)
10074 // small enough unsigned ints can get casted to large enough signed ints
10075 if (wanted_type->id == TypeTableEntryIdInt && wanted_type->data.integral.is_signed &&
10076 actual_type->id == TypeTableEntryIdInt && !actual_type->data.integral.is_signed &&
10077 wanted_type->data.integral.bit_count > actual_type->data.integral.bit_count)
996310078 {
9964 return ir_analyze_err_set_cast(ira, source_instr, value, wanted_type);
10079 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
996510080 }
996610081
9967 // explicit cast from int to float
10082 // explicit float widening conversion
996810083 if (wanted_type->id == TypeTableEntryIdFloat &&
9969 actual_type->id == TypeTableEntryIdInt)
10084 actual_type->id == TypeTableEntryIdFloat &&
10085 wanted_type->data.floating.bit_count >= actual_type->data.floating.bit_count)
997010086 {
9971 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpIntToFloat, false);
10087 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
997210088 }
997310089
9974 // explicit cast from float to int
9975 if (wanted_type->id == TypeTableEntryIdInt &&
9976 actual_type->id == TypeTableEntryIdFloat)
10090
10091 // explicit error set cast
10092 if (wanted_type->id == TypeTableEntryIdErrorSet &&
10093 actual_type->id == TypeTableEntryIdErrorSet)
997710094 {
9978 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpFloatToInt, false);
10095 return ir_analyze_err_set_cast(ira, source_instr, value, wanted_type);
997910096 }
998010097
998110098 // explicit cast from [N]T to []const T
......@@ -17365,7 +17482,126 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
1736517482 return dest_type;
1736617483 }
1736717484
17368 ir_build_truncate_from(&ira->new_irb, &instruction->base, dest_type_value, target);
17485 IrInstruction *new_instruction = ir_build_truncate(&ira->new_irb, instruction->base.scope,
17486 instruction->base.source_node, dest_type_value, target);
17487 ir_link_new_instruction(new_instruction, &instruction->base);
17488 return dest_type;
17489}
17490
17491static TypeTableEntry *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstructionIntCast *instruction) {
17492 TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
17493 if (type_is_invalid(dest_type))
17494 return ira->codegen->builtin_types.entry_invalid;
17495
17496 if (dest_type->id != TypeTableEntryIdInt) {
17497 ir_add_error(ira, instruction->dest_type, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
17498 return ira->codegen->builtin_types.entry_invalid;
17499 }
17500
17501 IrInstruction *target = instruction->target->other;
17502 if (type_is_invalid(target->value.type))
17503 return ira->codegen->builtin_types.entry_invalid;
17504
17505 if (target->value.type->id == TypeTableEntryIdComptimeInt) {
17506 if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
17507 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type,
17508 CastOpNumLitToConcrete, false);
17509 if (type_is_invalid(result->value.type))
17510 return ira->codegen->builtin_types.entry_invalid;
17511 ir_link_new_instruction(result, &instruction->base);
17512 return dest_type;
17513 } else {
17514 return ira->codegen->builtin_types.entry_invalid;
17515 }
17516 }
17517
17518 if (target->value.type->id != TypeTableEntryIdInt) {
17519 ir_add_error(ira, instruction->target, buf_sprintf("expected integer type, found '%s'",
17520 buf_ptr(&target->value.type->name)));
17521 return ira->codegen->builtin_types.entry_invalid;
17522 }
17523
17524 IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
17525 if (type_is_invalid(result->value.type))
17526 return ira->codegen->builtin_types.entry_invalid;
17527
17528 ir_link_new_instruction(result, &instruction->base);
17529 return dest_type;
17530}
17531
17532static TypeTableEntry *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstructionFloatCast *instruction) {
17533 TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
17534 if (type_is_invalid(dest_type))
17535 return ira->codegen->builtin_types.entry_invalid;
17536
17537 if (dest_type->id != TypeTableEntryIdFloat) {
17538 ir_add_error(ira, instruction->dest_type,
17539 buf_sprintf("expected float type, found '%s'", buf_ptr(&dest_type->name)));
17540 return ira->codegen->builtin_types.entry_invalid;
17541 }
17542
17543 IrInstruction *target = instruction->target->other;
17544 if (type_is_invalid(target->value.type))
17545 return ira->codegen->builtin_types.entry_invalid;
17546
17547 if (target->value.type->id == TypeTableEntryIdComptimeInt ||
17548 target->value.type->id == TypeTableEntryIdComptimeFloat)
17549 {
17550 if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
17551 CastOp op;
17552 if (target->value.type->id == TypeTableEntryIdComptimeInt) {
17553 op = CastOpIntToFloat;
17554 } else {
17555 op = CastOpNumLitToConcrete;
17556 }
17557 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, op, false);
17558 if (type_is_invalid(result->value.type))
17559 return ira->codegen->builtin_types.entry_invalid;
17560 ir_link_new_instruction(result, &instruction->base);
17561 return dest_type;
17562 } else {
17563 return ira->codegen->builtin_types.entry_invalid;
17564 }
17565 }
17566
17567 if (target->value.type->id != TypeTableEntryIdFloat) {
17568 ir_add_error(ira, instruction->target, buf_sprintf("expected float type, found '%s'",
17569 buf_ptr(&target->value.type->name)));
17570 return ira->codegen->builtin_types.entry_invalid;
17571 }
17572
17573 IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
17574 if (type_is_invalid(result->value.type))
17575 return ira->codegen->builtin_types.entry_invalid;
17576 ir_link_new_instruction(result, &instruction->base);
17577 return dest_type;
17578}
17579
17580static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInstructionIntToFloat *instruction) {
17581 TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
17582 if (type_is_invalid(dest_type))
17583 return ira->codegen->builtin_types.entry_invalid;
17584
17585 IrInstruction *target = instruction->target->other;
17586 if (type_is_invalid(target->value.type))
17587 return ira->codegen->builtin_types.entry_invalid;
17588
17589 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
17590 ir_link_new_instruction(result, &instruction->base);
17591 return dest_type;
17592}
17593
17594static TypeTableEntry *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstructionFloatToInt *instruction) {
17595 TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
17596 if (type_is_invalid(dest_type))
17597 return ira->codegen->builtin_types.entry_invalid;
17598
17599 IrInstruction *target = instruction->target->other;
17600 if (type_is_invalid(target->value.type))
17601 return ira->codegen->builtin_types.entry_invalid;
17602
17603 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpFloatToInt, false);
17604 ir_link_new_instruction(result, &instruction->base);
1736917605 return dest_type;
1737017606}
1737117607
......@@ -19899,6 +20135,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1989920135 return ir_analyze_instruction_fence(ira, (IrInstructionFence *)instruction);
1990020136 case IrInstructionIdTruncate:
1990120137 return ir_analyze_instruction_truncate(ira, (IrInstructionTruncate *)instruction);
20138 case IrInstructionIdIntCast:
20139 return ir_analyze_instruction_int_cast(ira, (IrInstructionIntCast *)instruction);
20140 case IrInstructionIdFloatCast:
20141 return ir_analyze_instruction_float_cast(ira, (IrInstructionFloatCast *)instruction);
20142 case IrInstructionIdIntToFloat:
20143 return ir_analyze_instruction_int_to_float(ira, (IrInstructionIntToFloat *)instruction);
20144 case IrInstructionIdFloatToInt:
20145 return ir_analyze_instruction_float_to_int(ira, (IrInstructionFloatToInt *)instruction);
1990220146 case IrInstructionIdIntType:
1990320147 return ir_analyze_instruction_int_type(ira, (IrInstructionIntType *)instruction);
1990420148 case IrInstructionIdBoolNot:
......@@ -20242,6 +20486,10 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2024220486 case IrInstructionIdPromiseResultType:
2024320487 case IrInstructionIdSqrt:
2024420488 case IrInstructionIdAtomicLoad:
20489 case IrInstructionIdIntCast:
20490 case IrInstructionIdFloatCast:
20491 case IrInstructionIdIntToFloat:
20492 case IrInstructionIdFloatToInt:
2024520493 return false;
2024620494
2024720495 case IrInstructionIdAsm:
src/ir_print.cpp+44
......@@ -648,6 +648,38 @@ static void ir_print_truncate(IrPrint *irp, IrInstructionTruncate *instruction)
648648 fprintf(irp->f, ")");
649649}
650650
651static void ir_print_int_cast(IrPrint *irp, IrInstructionIntCast *instruction) {
652 fprintf(irp->f, "@intCast(");
653 ir_print_other_instruction(irp, instruction->dest_type);
654 fprintf(irp->f, ", ");
655 ir_print_other_instruction(irp, instruction->target);
656 fprintf(irp->f, ")");
657}
658
659static void ir_print_float_cast(IrPrint *irp, IrInstructionFloatCast *instruction) {
660 fprintf(irp->f, "@floatCast(");
661 ir_print_other_instruction(irp, instruction->dest_type);
662 fprintf(irp->f, ", ");
663 ir_print_other_instruction(irp, instruction->target);
664 fprintf(irp->f, ")");
665}
666
667static void ir_print_int_to_float(IrPrint *irp, IrInstructionIntToFloat *instruction) {
668 fprintf(irp->f, "@intToFloat(");
669 ir_print_other_instruction(irp, instruction->dest_type);
670 fprintf(irp->f, ", ");
671 ir_print_other_instruction(irp, instruction->target);
672 fprintf(irp->f, ")");
673}
674
675static void ir_print_float_to_int(IrPrint *irp, IrInstructionFloatToInt *instruction) {
676 fprintf(irp->f, "@floatToInt(");
677 ir_print_other_instruction(irp, instruction->dest_type);
678 fprintf(irp->f, ", ");
679 ir_print_other_instruction(irp, instruction->target);
680 fprintf(irp->f, ")");
681}
682
651683static void ir_print_int_type(IrPrint *irp, IrInstructionIntType *instruction) {
652684 fprintf(irp->f, "@IntType(");
653685 ir_print_other_instruction(irp, instruction->is_signed);
......@@ -1417,6 +1449,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
14171449 case IrInstructionIdTruncate:
14181450 ir_print_truncate(irp, (IrInstructionTruncate *)instruction);
14191451 break;
1452 case IrInstructionIdIntCast:
1453 ir_print_int_cast(irp, (IrInstructionIntCast *)instruction);
1454 break;
1455 case IrInstructionIdFloatCast:
1456 ir_print_float_cast(irp, (IrInstructionFloatCast *)instruction);
1457 break;
1458 case IrInstructionIdIntToFloat:
1459 ir_print_int_to_float(irp, (IrInstructionIntToFloat *)instruction);
1460 break;
1461 case IrInstructionIdFloatToInt:
1462 ir_print_float_to_int(irp, (IrInstructionFloatToInt *)instruction);
1463 break;
14201464 case IrInstructionIdIntType:
14211465 ir_print_int_type(irp, (IrInstructionIntType *)instruction);
14221466 break;
src/main.cpp+1-1
......@@ -34,7 +34,7 @@ static int usage(const char *arg0) {
3434 " --assembly [source] add assembly file to build\n"
3535 " --cache-dir [path] override the cache directory\n"
3636 " --color [auto|off|on] enable or disable colored error messages\n"
37 " --emit [filetype] emit a specific file format as compilation output\n"
37 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
3838 " --enable-timing-info print timing diagnostics\n"
3939 " --libc-include-dir [path] directory where libc stdlib.h resides\n"
4040 " --name [name] override output name\n"
std/array_list.zig+4-4
......@@ -185,23 +185,23 @@ test "basic ArrayList test" {
185185 {
186186 var i: usize = 0;
187187 while (i < 10) : (i += 1) {
188 list.append(i32(i + 1)) catch unreachable;
188 list.append(@intCast(i32, i + 1)) catch unreachable;
189189 }
190190 }
191191
192192 {
193193 var i: usize = 0;
194194 while (i < 10) : (i += 1) {
195 assert(list.items[i] == i32(i + 1));
195 assert(list.items[i] == @intCast(i32, i + 1));
196196 }
197197 }
198198
199199 for (list.toSlice()) |v, i| {
200 assert(v == i32(i + 1));
200 assert(v == @intCast(i32, i + 1));
201201 }
202202
203203 for (list.toSliceConst()) |v, i| {
204 assert(v == i32(i + 1));
204 assert(v == @intCast(i32, i + 1));
205205 }
206206
207207 assert(list.pop() == 10);
std/base64.zig+2-2
......@@ -99,7 +99,7 @@ pub const Base64Decoder = struct {
9999 assert(!result.char_in_alphabet[c]);
100100 assert(c != pad_char);
101101
102 result.char_to_index[c] = u8(i);
102 result.char_to_index[c] = @intCast(u8, i);
103103 result.char_in_alphabet[c] = true;
104104 }
105105
......@@ -284,7 +284,7 @@ pub const Base64DecoderUnsafe = struct {
284284 };
285285 for (alphabet_chars) |c, i| {
286286 assert(c != pad_char);
287 result.char_to_index[c] = u8(i);
287 result.char_to_index[c] = @intCast(u8, i);
288288 }
289289 return result;
290290 }
std/crypto/blake2.zig+5-5
......@@ -79,7 +79,7 @@ fn Blake2s(comptime out_len: usize) type {
7979 mem.copy(u32, d.h[0..], iv[0..]);
8080
8181 // No key plus default parameters
82 d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
82 d.h[0] ^= 0x01010000 ^ @intCast(u32, out_len >> 3);
8383 d.t = 0;
8484 d.buf_len = 0;
8585 }
......@@ -110,7 +110,7 @@ fn Blake2s(comptime out_len: usize) type {
110110
111111 // Copy any remainder for next pass.
112112 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
113 d.buf_len += u8(b[off..].len);
113 d.buf_len += @intCast(u8, b[off..].len);
114114 }
115115
116116 pub fn final(d: *Self, out: []u8) void {
......@@ -144,7 +144,7 @@ fn Blake2s(comptime out_len: usize) type {
144144 }
145145
146146 v[12] ^= @truncate(u32, d.t);
147 v[13] ^= u32(d.t >> 32);
147 v[13] ^= @intCast(u32, d.t >> 32);
148148 if (last) v[14] = ~v[14];
149149
150150 const rounds = comptime []RoundParam{
......@@ -345,7 +345,7 @@ fn Blake2b(comptime out_len: usize) type {
345345
346346 // Copy any remainder for next pass.
347347 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
348 d.buf_len += u8(b[off..].len);
348 d.buf_len += @intCast(u8, b[off..].len);
349349 }
350350
351351 pub fn final(d: *Self, out: []u8) void {
......@@ -377,7 +377,7 @@ fn Blake2b(comptime out_len: usize) type {
377377 }
378378
379379 v[12] ^= @truncate(u64, d.t);
380 v[13] ^= u64(d.t >> 64);
380 v[13] ^= @intCast(u64, d.t >> 64);
381381 if (last) v[14] = ~v[14];
382382
383383 const rounds = comptime []RoundParam{
std/crypto/md5.zig+3-3
......@@ -78,7 +78,7 @@ pub const Md5 = struct {
7878
7979 // Copy any remainder for next pass.
8080 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
81 d.buf_len += u8(b[off..].len);
81 d.buf_len += @intCast(u8, b[off..].len);
8282
8383 // Md5 uses the bottom 64-bits for length padding
8484 d.total_len +%= b.len;
......@@ -103,9 +103,9 @@ pub const Md5 = struct {
103103 // Append message length.
104104 var i: usize = 1;
105105 var len = d.total_len >> 5;
106 d.buf[56] = u8(d.total_len & 0x1f) << 3;
106 d.buf[56] = @intCast(u8, d.total_len & 0x1f) << 3;
107107 while (i < 8) : (i += 1) {
108 d.buf[56 + i] = u8(len & 0xff);
108 d.buf[56 + i] = @intCast(u8, len & 0xff);
109109 len >>= 8;
110110 }
111111
std/crypto/sha1.zig+3-3
......@@ -78,7 +78,7 @@ pub const Sha1 = struct {
7878
7979 // Copy any remainder for next pass.
8080 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
81 d.buf_len += u8(b[off..].len);
81 d.buf_len += @intCast(u8, b[off..].len);
8282
8383 d.total_len += b.len;
8484 }
......@@ -102,9 +102,9 @@ pub const Sha1 = struct {
102102 // Append message length.
103103 var i: usize = 1;
104104 var len = d.total_len >> 5;
105 d.buf[63] = u8(d.total_len & 0x1f) << 3;
105 d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
106106 while (i < 8) : (i += 1) {
107 d.buf[63 - i] = u8(len & 0xff);
107 d.buf[63 - i] = @intCast(u8, len & 0xff);
108108 len >>= 8;
109109 }
110110
std/crypto/sha2.zig+6-6
......@@ -131,7 +131,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
131131
132132 // Copy any remainder for next pass.
133133 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
134 d.buf_len += u8(b[off..].len);
134 d.buf_len += @intCast(u8, b[off..].len);
135135
136136 d.total_len += b.len;
137137 }
......@@ -155,9 +155,9 @@ fn Sha2_32(comptime params: Sha2Params32) type {
155155 // Append message length.
156156 var i: usize = 1;
157157 var len = d.total_len >> 5;
158 d.buf[63] = u8(d.total_len & 0x1f) << 3;
158 d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
159159 while (i < 8) : (i += 1) {
160 d.buf[63 - i] = u8(len & 0xff);
160 d.buf[63 - i] = @intCast(u8, len & 0xff);
161161 len >>= 8;
162162 }
163163
......@@ -472,7 +472,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
472472
473473 // Copy any remainder for next pass.
474474 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
475 d.buf_len += u8(b[off..].len);
475 d.buf_len += @intCast(u8, b[off..].len);
476476
477477 d.total_len += b.len;
478478 }
......@@ -496,9 +496,9 @@ fn Sha2_64(comptime params: Sha2Params64) type {
496496 // Append message length.
497497 var i: usize = 1;
498498 var len = d.total_len >> 5;
499 d.buf[127] = u8(d.total_len & 0x1f) << 3;
499 d.buf[127] = @intCast(u8, d.total_len & 0x1f) << 3;
500500 while (i < 16) : (i += 1) {
501 d.buf[127 - i] = u8(len & 0xff);
501 d.buf[127 - i] = @intCast(u8, len & 0xff);
502502 len >>= 8;
503503 }
504504
std/debug/index.zig+4-4
......@@ -554,7 +554,7 @@ const LineNumberProgram = struct {
554554 const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
555555 errdefer self.file_entries.allocator.free(file_name);
556556 return LineInfo{
557 .line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
557 .line = if (self.prev_line >= 0) @intCast(usize, self.prev_line) else 0,
558558 .column = self.prev_column,
559559 .file_name = file_name,
560560 .allocator = self.file_entries.allocator,
......@@ -1070,7 +1070,7 @@ fn readULeb128(in_stream: var) !u64 {
10701070
10711071 var operand: u64 = undefined;
10721072
1073 if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
1073 if (@shlWithOverflow(u64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
10741074
10751075 result |= operand;
10761076
......@@ -1089,13 +1089,13 @@ fn readILeb128(in_stream: var) !i64 {
10891089
10901090 var operand: i64 = undefined;
10911091
1092 if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
1092 if (@shlWithOverflow(i64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
10931093
10941094 result |= operand;
10951095 shift += 7;
10961096
10971097 if ((byte & 0b10000000) == 0) {
1098 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << u6(shift));
1098 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << @intCast(u6, shift));
10991099 return result;
11001100 }
11011101 }
std/fmt/errol/index.zig+39-39
......@@ -29,11 +29,11 @@ pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: Ro
2929 switch (mode) {
3030 RoundMode.Decimal => {
3131 if (float_decimal.exp >= 0) {
32 round_digit = precision + usize(float_decimal.exp);
32 round_digit = precision + @intCast(usize, float_decimal.exp);
3333 } else {
3434 // if a small negative exp, then adjust we need to offset by the number
3535 // of leading zeros that will occur.
36 const min_exp_required = usize(-float_decimal.exp);
36 const min_exp_required = @intCast(usize, -float_decimal.exp);
3737 if (precision > min_exp_required) {
3838 round_digit = precision - min_exp_required;
3939 }
......@@ -107,16 +107,16 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
107107 // normalize the midpoint
108108
109109 const e = math.frexp(val).exponent;
110 var exp = i16(math.floor(307 + f64(e) * 0.30103));
110 var exp = @floatToInt(i16, math.floor(307 + @intToFloat(f64, e) * 0.30103));
111111 if (exp < 20) {
112112 exp = 20;
113 } else if (usize(exp) >= lookup_table.len) {
114 exp = i16(lookup_table.len - 1);
113 } else if (@intCast(usize, exp) >= lookup_table.len) {
114 exp = @intCast(i16, lookup_table.len - 1);
115115 }
116116
117 var mid = lookup_table[usize(exp)];
117 var mid = lookup_table[@intCast(usize, exp)];
118118 mid = hpProd(mid, val);
119 const lten = lookup_table[usize(exp)].val;
119 const lten = lookup_table[@intCast(usize, exp)].val;
120120
121121 exp -= 307;
122122
......@@ -168,25 +168,25 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
168168 // the 0-index for this extra digit.
169169 var buf_index: usize = 1;
170170 while (true) {
171 var hdig = u8(math.floor(high.val));
172 if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
171 var hdig = @floatToInt(u8, math.floor(high.val));
172 if ((high.val == @intToFloat(f64, hdig)) and (high.off < 0)) hdig -= 1;
173173
174 var ldig = u8(math.floor(low.val));
175 if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
174 var ldig = @floatToInt(u8, math.floor(low.val));
175 if ((low.val == @intToFloat(f64, ldig)) and (low.off < 0)) ldig -= 1;
176176
177177 if (ldig != hdig) break;
178178
179179 buffer[buf_index] = hdig + '0';
180180 buf_index += 1;
181 high.val -= f64(hdig);
182 low.val -= f64(ldig);
181 high.val -= @intToFloat(f64, hdig);
182 low.val -= @intToFloat(f64, ldig);
183183 hpMul10(&high);
184184 hpMul10(&low);
185185 }
186186
187187 const tmp = (high.val + low.val) / 2.0;
188 var mdig = u8(math.floor(tmp + 0.5));
189 if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
188 var mdig = @floatToInt(u8, math.floor(tmp + 0.5));
189 if ((@intToFloat(f64, mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
190190
191191 buffer[buf_index] = mdig + '0';
192192 buf_index += 1;
......@@ -304,7 +304,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
304304
305305 assert((val > 9.007199254740992e15) and val < (3.40282366920938e38));
306306
307 var mid = u128(val);
307 var mid = @floatToInt(u128, val);
308308 var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);
309309 var high: u128 = mid + fpeint((val - fpprev(val)) / 2.0);
310310
......@@ -314,11 +314,11 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
314314 low -= 1;
315315 }
316316
317 var l64 = u64(low % pow19);
318 const lf = u64((low / pow19) % pow19);
317 var l64 = @intCast(u64, low % pow19);
318 const lf = @intCast(u64, (low / pow19) % pow19);
319319
320 var h64 = u64(high % pow19);
321 const hf = u64((high / pow19) % pow19);
320 var h64 = @intCast(u64, high % pow19);
321 const hf = @intCast(u64, (high / pow19) % pow19);
322322
323323 if (lf != hf) {
324324 l64 = lf;
......@@ -348,7 +348,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
348348
349349 return FloatDecimal{
350350 .digits = buffer[0..buf_index],
351 .exp = i32(buf_index) + mi,
351 .exp = @intCast(i32, buf_index) + mi,
352352 };
353353}
354354
......@@ -359,33 +359,33 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
359359fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
360360 assert((val >= 16.0) and (val < 9.007199254740992e15));
361361
362 const u = u64(val);
363 const n = f64(u);
362 const u = @floatToInt(u64, val);
363 const n = @intToFloat(f64, u);
364364
365365 var mid = val - n;
366366 var lo = ((fpprev(val) - n) + mid) / 2.0;
367367 var hi = ((fpnext(val) - n) + mid) / 2.0;
368368
369369 var buf_index = u64toa(u, buffer);
370 var exp = i32(buf_index);
370 var exp = @intCast(i32, buf_index);
371371 var j = buf_index;
372372 buffer[j] = 0;
373373
374374 if (mid != 0.0) {
375375 while (mid != 0.0) {
376376 lo *= 10.0;
377 const ldig = i32(lo);
378 lo -= f64(ldig);
377 const ldig = @floatToInt(i32, lo);
378 lo -= @intToFloat(f64, ldig);
379379
380380 mid *= 10.0;
381 const mdig = i32(mid);
382 mid -= f64(mdig);
381 const mdig = @floatToInt(i32, mid);
382 mid -= @intToFloat(f64, mdig);
383383
384384 hi *= 10.0;
385 const hdig = i32(hi);
386 hi -= f64(hdig);
385 const hdig = @floatToInt(i32, hi);
386 hi -= @intToFloat(f64, hdig);
387387
388 buffer[j] = u8(mdig + '0');
388 buffer[j] = @intCast(u8, mdig + '0');
389389 j += 1;
390390
391391 if (hdig != ldig or j > 50) break;
......@@ -452,7 +452,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
452452 var buf_index: usize = 0;
453453
454454 if (value < kTen8) {
455 const v = u32(value);
455 const v = @intCast(u32, value);
456456 if (v < 10000) {
457457 const d1: u32 = (v / 100) << 1;
458458 const d2: u32 = (v % 100) << 1;
......@@ -507,8 +507,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
507507 buf_index += 1;
508508 }
509509 } else if (value < kTen16) {
510 const v0: u32 = u32(value / kTen8);
511 const v1: u32 = u32(value % kTen8);
510 const v0: u32 = @intCast(u32, value / kTen8);
511 const v1: u32 = @intCast(u32, value % kTen8);
512512
513513 const b0: u32 = v0 / 10000;
514514 const c0: u32 = v0 % 10000;
......@@ -578,11 +578,11 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
578578 buffer[buf_index] = c_digits_lut[d8 + 1];
579579 buf_index += 1;
580580 } else {
581 const a = u32(value / kTen16); // 1 to 1844
581 const a = @intCast(u32, value / kTen16); // 1 to 1844
582582 value %= kTen16;
583583
584584 if (a < 10) {
585 buffer[buf_index] = '0' + u8(a);
585 buffer[buf_index] = '0' + @intCast(u8, a);
586586 buf_index += 1;
587587 } else if (a < 100) {
588588 const i: u32 = a << 1;
......@@ -591,7 +591,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
591591 buffer[buf_index] = c_digits_lut[i + 1];
592592 buf_index += 1;
593593 } else if (a < 1000) {
594 buffer[buf_index] = '0' + u8(a / 100);
594 buffer[buf_index] = '0' + @intCast(u8, a / 100);
595595 buf_index += 1;
596596
597597 const i: u32 = (a % 100) << 1;
......@@ -612,8 +612,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
612612 buf_index += 1;
613613 }
614614
615 const v0 = u32(value / kTen8);
616 const v1 = u32(value % kTen8);
615 const v0 = @intCast(u32, value / kTen8);
616 const v1 = @intCast(u32, value % kTen8);
617617
618618 const b0: u32 = v0 / 10000;
619619 const c0: u32 = v0 % 10000;
std/fmt/index.zig+10-9
......@@ -5,6 +5,7 @@ const assert = debug.assert;
55const mem = std.mem;
66const builtin = @import("builtin");
77const errol = @import("errol/index.zig");
8const lossyCast = std.math.lossyCast;
89
910const max_int_digits = 65;
1011
......@@ -463,7 +464,7 @@ pub fn formatFloatDecimal(
463464 errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);
464465
465466 // exp < 0 means the leading is always 0 as errol result is normalized.
466 var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
467 var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
467468
468469 // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
469470 var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
......@@ -492,7 +493,7 @@ pub fn formatFloatDecimal(
492493
493494 // Zero-fill until we reach significant digits or run out of precision.
494495 if (float_decimal.exp <= 0) {
495 const zero_digit_count = usize(-float_decimal.exp);
496 const zero_digit_count = @intCast(usize, -float_decimal.exp);
496497 const zeros_to_print = math.min(zero_digit_count, precision);
497498
498499 var i: usize = 0;
......@@ -521,7 +522,7 @@ pub fn formatFloatDecimal(
521522 }
522523 } else {
523524 // exp < 0 means the leading is always 0 as errol result is normalized.
524 var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
525 var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
525526
526527 // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
527528 var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
......@@ -547,7 +548,7 @@ pub fn formatFloatDecimal(
547548
548549 // Zero-fill until we reach significant digits or run out of precision.
549550 if (float_decimal.exp < 0) {
550 const zero_digit_count = usize(-float_decimal.exp);
551 const zero_digit_count = @intCast(usize, -float_decimal.exp);
551552
552553 var i: usize = 0;
553554 while (i < zero_digit_count) : (i += 1) {
......@@ -578,7 +579,7 @@ pub fn formatBytes(
578579 1024 => math.min(math.log2(value) / 10, mags_iec.len - 1),
579580 else => unreachable,
580581 };
581 const new_value = f64(value) / math.pow(f64, f64(radix), f64(magnitude));
582 const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));
582583 const suffix = switch (radix) {
583584 1000 => mags_si[magnitude],
584585 1024 => mags_iec[magnitude],
......@@ -628,15 +629,15 @@ fn formatIntSigned(
628629 if (value < 0) {
629630 const minus_sign: u8 = '-';
630631 try output(context, (*[1]u8)(&minus_sign)[0..]);
631 const new_value = uint(-(value + 1)) + 1;
632 const new_value = @intCast(uint, -(value + 1)) + 1;
632633 const new_width = if (width == 0) 0 else (width - 1);
633634 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
634635 } else if (width == 0) {
635 return formatIntUnsigned(uint(value), base, uppercase, width, context, Errors, output);
636 return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
636637 } else {
637638 const plus_sign: u8 = '+';
638639 try output(context, (*[1]u8)(&plus_sign)[0..]);
639 const new_value = uint(value);
640 const new_value = @intCast(uint, value);
640641 const new_width = if (width == 0) 0 else (width - 1);
641642 return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
642643 }
......@@ -660,7 +661,7 @@ fn formatIntUnsigned(
660661 while (true) {
661662 const digit = a % base;
662663 index -= 1;
663 buf[index] = digitToChar(u8(digit), uppercase);
664 buf[index] = digitToChar(@intCast(u8, digit), uppercase);
664665 a /= base;
665666 if (a == 0) break;
666667 }
std/hash/crc.zig+2-2
......@@ -26,7 +26,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
2626 var tables: [8][256]u32 = undefined;
2727
2828 for (tables[0]) |*e, i| {
29 var crc = u32(i);
29 var crc = @intCast(u32, i);
3030 var j: usize = 0;
3131 while (j < 8) : (j += 1) {
3232 if (crc & 1 == 1) {
......@@ -122,7 +122,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
122122 var table: [16]u32 = undefined;
123123
124124 for (table) |*e, i| {
125 var crc = u32(i * 16);
125 var crc = @intCast(u32, i * 16);
126126 var j: usize = 0;
127127 while (j < 8) : (j += 1) {
128128 if (crc & 1 == 1) {
std/hash/siphash.zig+3-3
......@@ -81,7 +81,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
8181
8282 // Remainder for next pass.
8383 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
84 d.buf_len += u8(b[off..].len);
84 d.buf_len += @intCast(u8, b[off..].len);
8585 d.msg_len +%= @truncate(u8, b.len);
8686 }
8787
......@@ -233,7 +233,7 @@ test "siphash64-2-4 sanity" {
233233
234234 var buffer: [64]u8 = undefined;
235235 for (vectors) |vector, i| {
236 buffer[i] = u8(i);
236 buffer[i] = @intCast(u8, i);
237237
238238 const expected = mem.readInt(vector, u64, Endian.Little);
239239 debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
......@@ -312,7 +312,7 @@ test "siphash128-2-4 sanity" {
312312
313313 var buffer: [64]u8 = undefined;
314314 for (vectors) |vector, i| {
315 buffer[i] = u8(i);
315 buffer[i] = @intCast(u8, i);
316316
317317 const expected = mem.readInt(vector, u128, Endian.Little);
318318 debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
std/heap.zig+1-1
......@@ -408,7 +408,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
408408
409409 for (slice) |*item, i| {
410410 item.* = try allocator.create(i32);
411 item.*.* = i32(i);
411 item.*.* = @intCast(i32, i);
412412 }
413413
414414 for (slice) |item, i| {
std/json.zig+1-1
......@@ -180,7 +180,7 @@ pub const StreamingParser = struct {
180180 pub fn fromInt(x: var) State {
181181 debug.assert(x == 0 or x == 1);
182182 const T = @TagType(State);
183 return State(T(x));
183 return State(@intCast(T, x));
184184 }
185185 };
186186
std/math/acos.zig+2-2
......@@ -95,12 +95,12 @@ fn acos64(x: f64) f64 {
9595 const pio2_lo: f64 = 6.12323399573676603587e-17;
9696
9797 const ux = @bitCast(u64, x);
98 const hx = u32(ux >> 32);
98 const hx = @intCast(u32, ux >> 32);
9999 const ix = hx & 0x7FFFFFFF;
100100
101101 // |x| >= 1 or nan
102102 if (ix >= 0x3FF00000) {
103 const lx = u32(ux & 0xFFFFFFFF);
103 const lx = @intCast(u32, ux & 0xFFFFFFFF);
104104
105105 // acos(1) = 0, acos(-1) = pi
106106 if ((ix - 0x3FF00000) | lx == 0) {
std/math/asin.zig+2-2
......@@ -87,12 +87,12 @@ fn asin64(x: f64) f64 {
8787 const pio2_lo: f64 = 6.12323399573676603587e-17;
8888
8989 const ux = @bitCast(u64, x);
90 const hx = u32(ux >> 32);
90 const hx = @intCast(u32, ux >> 32);
9191 const ix = hx & 0x7FFFFFFF;
9292
9393 // |x| >= 1 or nan
9494 if (ix >= 0x3FF00000) {
95 const lx = u32(ux & 0xFFFFFFFF);
95 const lx = @intCast(u32, ux & 0xFFFFFFFF);
9696
9797 // asin(1) = +-pi/2 with inexact
9898 if ((ix - 0x3FF00000) | lx == 0) {
std/math/atan.zig+2-2
......@@ -138,7 +138,7 @@ fn atan64(x_: f64) f64 {
138138
139139 var x = x_;
140140 var ux = @bitCast(u64, x);
141 var ix = u32(ux >> 32);
141 var ix = @intCast(u32, ux >> 32);
142142 const sign = ix >> 31;
143143 ix &= 0x7FFFFFFF;
144144
......@@ -159,7 +159,7 @@ fn atan64(x_: f64) f64 {
159159 // |x| < 2^(-27)
160160 if (ix < 0x3E400000) {
161161 if (ix < 0x00100000) {
162 math.forceEval(f32(x));
162 math.forceEval(@floatCast(f32, x));
163163 }
164164 return x;
165165 }
std/math/atan2.zig+4-4
......@@ -124,12 +124,12 @@ fn atan2_64(y: f64, x: f64) f64 {
124124 }
125125
126126 var ux = @bitCast(u64, x);
127 var ix = u32(ux >> 32);
128 var lx = u32(ux & 0xFFFFFFFF);
127 var ix = @intCast(u32, ux >> 32);
128 var lx = @intCast(u32, ux & 0xFFFFFFFF);
129129
130130 var uy = @bitCast(u64, y);
131 var iy = u32(uy >> 32);
132 var ly = u32(uy & 0xFFFFFFFF);
131 var iy = @intCast(u32, uy >> 32);
132 var ly = @intCast(u32, uy & 0xFFFFFFFF);
133133
134134 // x = 1.0
135135 if ((ix -% 0x3FF00000) | lx == 0) {
std/math/atanh.zig+1-1
......@@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 {
6262 if (e < 0x3FF - 32) {
6363 // underflow
6464 if (e == 0) {
65 math.forceEval(f32(y));
65 math.forceEval(@floatCast(f32, y));
6666 }
6767 }
6868 // |x| < 0.5
std/math/big/int.zig+11-11
......@@ -135,7 +135,7 @@ pub const Int = struct {
135135 self.positive = value >= 0;
136136 self.len = 0;
137137
138 var w_value: UT = if (value < 0) UT(-value) else UT(value);
138 var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value);
139139
140140 if (info.bits <= Limb.bit_count) {
141141 self.limbs[0] = Limb(w_value);
......@@ -198,7 +198,7 @@ pub const Int = struct {
198198 var r: UT = 0;
199199
200200 if (@sizeOf(UT) <= @sizeOf(Limb)) {
201 r = UT(self.limbs[0]);
201 r = @intCast(UT, self.limbs[0]);
202202 } else {
203203 for (self.limbs[0..self.len]) |_, ri| {
204204 const limb = self.limbs[self.len - ri - 1];
......@@ -210,7 +210,7 @@ pub const Int = struct {
210210 if (!T.is_signed) {
211211 return if (self.positive) r else error.NegativeIntoUnsigned;
212212 } else {
213 return if (self.positive) T(r) else -T(r);
213 return if (self.positive) @intCast(T, r) else -@intCast(T, r);
214214 }
215215 },
216216 else => {
......@@ -295,7 +295,7 @@ pub const Int = struct {
295295 for (self.limbs[0..self.len]) |limb| {
296296 var shift: usize = 0;
297297 while (shift < Limb.bit_count) : (shift += base_shift) {
298 const r = u8((limb >> Log2Limb(shift)) & Limb(base - 1));
298 const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & Limb(base - 1));
299299 const ch = try digitToChar(r, base);
300300 try digits.append(ch);
301301 }
......@@ -329,7 +329,7 @@ pub const Int = struct {
329329 var r_word = r.limbs[0];
330330 var i: usize = 0;
331331 while (i < digits_per_limb) : (i += 1) {
332 const ch = try digitToChar(u8(r_word % base), base);
332 const ch = try digitToChar(@intCast(u8, r_word % base), base);
333333 r_word /= base;
334334 try digits.append(ch);
335335 }
......@@ -340,7 +340,7 @@ pub const Int = struct {
340340
341341 var r_word = q.limbs[0];
342342 while (r_word != 0) {
343 const ch = try digitToChar(u8(r_word % base), base);
343 const ch = try digitToChar(@intCast(u8, r_word % base), base);
344344 r_word /= base;
345345 try digits.append(ch);
346346 }
......@@ -801,7 +801,7 @@ pub const Int = struct {
801801 q.limbs[i - t - 1] = @maxValue(Limb);
802802 } else {
803803 const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]);
804 const z = Limb(num / DoubleLimb(y.limbs[t]));
804 const z = @intCast(Limb, num / DoubleLimb(y.limbs[t]));
805805 q.limbs[i - t - 1] = if (z > @maxValue(Limb)) @maxValue(Limb) else Limb(z);
806806 }
807807
......@@ -860,7 +860,7 @@ pub const Int = struct {
860860 debug.assert(r.len >= a.len + (shift / Limb.bit_count) + 1);
861861
862862 const limb_shift = shift / Limb.bit_count + 1;
863 const interior_limb_shift = Log2Limb(shift % Limb.bit_count);
863 const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
864864
865865 var carry: Limb = 0;
866866 var i: usize = 0;
......@@ -869,7 +869,7 @@ pub const Int = struct {
869869 const dst_i = src_i + limb_shift;
870870
871871 const src_digit = a[src_i];
872 r[dst_i] = carry | @inlineCall(math.shr, Limb, src_digit, Limb.bit_count - Limb(interior_limb_shift));
872 r[dst_i] = carry | @inlineCall(math.shr, Limb, src_digit, Limb.bit_count - @intCast(Limb, interior_limb_shift));
873873 carry = (src_digit << interior_limb_shift);
874874 }
875875
......@@ -898,7 +898,7 @@ pub const Int = struct {
898898 debug.assert(r.len >= a.len - (shift / Limb.bit_count));
899899
900900 const limb_shift = shift / Limb.bit_count;
901 const interior_limb_shift = Log2Limb(shift % Limb.bit_count);
901 const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
902902
903903 var carry: Limb = 0;
904904 var i: usize = 0;
......@@ -908,7 +908,7 @@ pub const Int = struct {
908908
909909 const src_digit = a[src_i];
910910 r[dst_i] = carry | (src_digit >> interior_limb_shift);
911 carry = @inlineCall(math.shl, Limb, src_digit, Limb.bit_count - Limb(interior_limb_shift));
911 carry = @inlineCall(math.shl, Limb, src_digit, Limb.bit_count - @intCast(Limb, interior_limb_shift));
912912 }
913913 }
914914
std/math/cbrt.zig+3-3
......@@ -54,7 +54,7 @@ fn cbrt32(x: f32) f32 {
5454 r = t * t * t;
5555 t = t * (f64(x) + x + r) / (x + r + r);
5656
57 return f32(t);
57 return @floatCast(f32, t);
5858}
5959
6060fn cbrt64(x: f64) f64 {
......@@ -69,7 +69,7 @@ fn cbrt64(x: f64) f64 {
6969 const P4: f64 = 0.145996192886612446982;
7070
7171 var u = @bitCast(u64, x);
72 var hx = u32(u >> 32) & 0x7FFFFFFF;
72 var hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
7373
7474 // cbrt(nan, inf) = itself
7575 if (hx >= 0x7FF00000) {
......@@ -79,7 +79,7 @@ fn cbrt64(x: f64) f64 {
7979 // cbrt to ~5bits
8080 if (hx < 0x00100000) {
8181 u = @bitCast(u64, x * 0x1.0p54);
82 hx = u32(u >> 32) & 0x7FFFFFFF;
82 hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
8383
8484 // cbrt(0) is itself
8585 if (hx == 0) {
std/math/ceil.zig+2-2
......@@ -20,7 +20,7 @@ pub fn ceil(x: var) @typeOf(x) {
2020
2121fn ceil32(x: f32) f32 {
2222 var u = @bitCast(u32, x);
23 var e = i32((u >> 23) & 0xFF) - 0x7F;
23 var e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
2424 var m: u32 = undefined;
2525
2626 // TODO: Shouldn't need this explicit check.
......@@ -31,7 +31,7 @@ fn ceil32(x: f32) f32 {
3131 if (e >= 23) {
3232 return x;
3333 } else if (e >= 0) {
34 m = u32(0x007FFFFF) >> u5(e);
34 m = u32(0x007FFFFF) >> @intCast(u5, e);
3535 if (u & m == 0) {
3636 return x;
3737 }
std/math/complex/atan.zig+5-5
......@@ -4,7 +4,7 @@ const math = std.math;
44const cmath = math.complex;
55const Complex = cmath.Complex;
66
7pub fn atan(z: var) Complex(@typeOf(z.re)) {
7pub fn atan(z: var) @typeOf(z) {
88 const T = @typeOf(z.re);
99 return switch (T) {
1010 f32 => atan32(z),
......@@ -25,11 +25,11 @@ fn redupif32(x: f32) f32 {
2525 t -= 0.5;
2626 }
2727
28 const u = f32(i32(t));
28 const u = @intToFloat(f32, @floatToInt(i32, t));
2929 return ((x - u * DP1) - u * DP2) - t * DP3;
3030}
3131
32fn atan32(z: *const Complex(f32)) Complex(f32) {
32fn atan32(z: Complex(f32)) Complex(f32) {
3333 const maxnum = 1.0e38;
3434
3535 const x = z.re;
......@@ -74,11 +74,11 @@ fn redupif64(x: f64) f64 {
7474 t -= 0.5;
7575 }
7676
77 const u = f64(i64(t));
77 const u = @intToFloat(f64, @floatToInt(i64, t));
7878 return ((x - u * DP1) - u * DP2) - t * DP3;
7979}
8080
81fn atan64(z: *const Complex(f64)) Complex(f64) {
81fn atan64(z: Complex(f64)) Complex(f64) {
8282 const maxnum = 1.0e308;
8383
8484 const x = z.re;
std/math/complex/cosh.zig+2-2
......@@ -83,12 +83,12 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) {
8383 const y = z.im;
8484
8585 const fx = @bitCast(u64, x);
86 const hx = u32(fx >> 32);
86 const hx = @intCast(u32, fx >> 32);
8787 const lx = @truncate(u32, fx);
8888 const ix = hx & 0x7fffffff;
8989
9090 const fy = @bitCast(u64, y);
91 const hy = u32(fy >> 32);
91 const hy = @intCast(u32, fy >> 32);
9292 const ly = @truncate(u32, fy);
9393 const iy = hy & 0x7fffffff;
9494
std/math/complex/exp.zig+3-3
......@@ -6,7 +6,7 @@ const Complex = cmath.Complex;
66
77const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
88
9pub fn exp(z: var) Complex(@typeOf(z.re)) {
9pub fn exp(z: var) @typeOf(z) {
1010 const T = @typeOf(z.re);
1111
1212 return switch (T) {
......@@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
1616 };
1717}
1818
19fn exp32(z: *const Complex(f32)) Complex(f32) {
19fn exp32(z: Complex(f32)) Complex(f32) {
2020 @setFloatMode(this, @import("builtin").FloatMode.Strict);
2121
2222 const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
......@@ -63,7 +63,7 @@ fn exp32(z: *const Complex(f32)) Complex(f32) {
6363 }
6464}
6565
66fn exp64(z: *const Complex(f64)) Complex(f64) {
66fn exp64(z: Complex(f64)) Complex(f64) {
6767 const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
6868 const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2
6969
std/math/complex/index.zig+7-7
......@@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
3737 };
3838 }
3939
40 pub fn add(self: *const Self, other: *const Self) Self {
40 pub fn add(self: Self, other: Self) Self {
4141 return Self{
4242 .re = self.re + other.re,
4343 .im = self.im + other.im,
4444 };
4545 }
4646
47 pub fn sub(self: *const Self, other: *const Self) Self {
47 pub fn sub(self: Self, other: Self) Self {
4848 return Self{
4949 .re = self.re - other.re,
5050 .im = self.im - other.im,
5151 };
5252 }
5353
54 pub fn mul(self: *const Self, other: *const Self) Self {
54 pub fn mul(self: Self, other: Self) Self {
5555 return Self{
5656 .re = self.re * other.re - self.im * other.im,
5757 .im = self.im * other.re + self.re * other.im,
5858 };
5959 }
6060
61 pub fn div(self: *const Self, other: *const Self) Self {
61 pub fn div(self: Self, other: Self) Self {
6262 const re_num = self.re * other.re + self.im * other.im;
6363 const im_num = self.im * other.re - self.re * other.im;
6464 const den = other.re * other.re + other.im * other.im;
......@@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
6969 };
7070 }
7171
72 pub fn conjugate(self: *const Self) Self {
72 pub fn conjugate(self: Self) Self {
7373 return Self{
7474 .re = self.re,
7575 .im = -self.im,
7676 };
7777 }
7878
79 pub fn reciprocal(self: *const Self) Self {
79 pub fn reciprocal(self: Self) Self {
8080 const m = self.re * self.re + self.im * self.im;
8181 return Self{
8282 .re = self.re / m,
......@@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
8484 };
8585 }
8686
87 pub fn magnitude(self: *const Self) T {
87 pub fn magnitude(self: Self) T {
8888 return math.sqrt(self.re * self.re + self.im * self.im);
8989 }
9090 };
std/math/complex/ldexp.zig+7-6
......@@ -4,7 +4,7 @@ const math = std.math;
44const cmath = math.complex;
55const Complex = cmath.Complex;
66
7pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
7pub fn ldexp_cexp(z: var, expt: i32) @typeOf(z) {
88 const T = @typeOf(z.re);
99
1010 return switch (T) {
......@@ -20,11 +20,12 @@ fn frexp_exp32(x: f32, expt: *i32) f32 {
2020
2121 const exp_x = math.exp(x - kln2);
2222 const hx = @bitCast(u32, exp_x);
23 expt.* = i32(hx >> 23) - (0x7f + 127) + k;
23 // TODO zig should allow this cast implicitly because it should know the value is in range
24 expt.* = @intCast(i32, hx >> 23) - (0x7f + 127) + k;
2425 return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
2526}
2627
27fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) {
28fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
2829 var ex_expt: i32 = undefined;
2930 const exp_x = frexp_exp32(z.re, &ex_expt);
3031 const exptf = expt + ex_expt;
......@@ -45,16 +46,16 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
4546 const exp_x = math.exp(x - kln2);
4647
4748 const fx = @bitCast(u64, x);
48 const hx = u32(fx >> 32);
49 const hx = @intCast(u32, fx >> 32);
4950 const lx = @truncate(u32, fx);
5051
51 expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
52 expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k;
5253
5354 const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
5455 return @bitCast(f64, (u64(high_word) << 32) | lx);
5556}
5657
57fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) {
58fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
5859 var ex_expt: i32 = undefined;
5960 const exp_x = frexp_exp64(z.re, &ex_expt);
6061 const exptf = i64(expt + ex_expt);
std/math/complex/sinh.zig+5-5
......@@ -6,7 +6,7 @@ const Complex = cmath.Complex;
66
77const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
88
9pub fn sinh(z: var) Complex(@typeOf(z.re)) {
9pub fn sinh(z: var) @typeOf(z) {
1010 const T = @typeOf(z.re);
1111 return switch (T) {
1212 f32 => sinh32(z),
......@@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) {
1515 };
1616}
1717
18fn sinh32(z: *const Complex(f32)) Complex(f32) {
18fn sinh32(z: Complex(f32)) Complex(f32) {
1919 const x = z.re;
2020 const y = z.im;
2121
......@@ -78,17 +78,17 @@ fn sinh32(z: *const Complex(f32)) Complex(f32) {
7878 return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
7979}
8080
81fn sinh64(z: *const Complex(f64)) Complex(f64) {
81fn sinh64(z: Complex(f64)) Complex(f64) {
8282 const x = z.re;
8383 const y = z.im;
8484
8585 const fx = @bitCast(u64, x);
86 const hx = u32(fx >> 32);
86 const hx = @intCast(u32, fx >> 32);
8787 const lx = @truncate(u32, fx);
8888 const ix = hx & 0x7fffffff;
8989
9090 const fy = @bitCast(u64, y);
91 const hy = u32(fy >> 32);
91 const hy = @intCast(u32, fy >> 32);
9292 const ly = @truncate(u32, fy);
9393 const iy = hy & 0x7fffffff;
9494
std/math/complex/sqrt.zig+12-7
......@@ -4,18 +4,17 @@ const math = std.math;
44const cmath = math.complex;
55const Complex = cmath.Complex;
66
7// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
8pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
7pub fn sqrt(z: var) @typeOf(z) {
98 const T = @typeOf(z.re);
109
1110 return switch (T) {
1211 f32 => sqrt32(z),
1312 f64 => sqrt64(z),
14 else => @compileError("sqrt not implemented for " ++ @typeName(z)),
13 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
1514 };
1615}
1716
18fn sqrt32(z: *const Complex(f32)) Complex(f32) {
17fn sqrt32(z: Complex(f32)) Complex(f32) {
1918 const x = z.re;
2019 const y = z.im;
2120
......@@ -50,14 +49,20 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
5049
5150 if (dx >= 0) {
5251 const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
53 return Complex(f32).new(f32(t), f32(dy / (2.0 * t)));
52 return Complex(f32).new(
53 @floatCast(f32, t),
54 @floatCast(f32, dy / (2.0 * t)),
55 );
5456 } else {
5557 const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
56 return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y)));
58 return Complex(f32).new(
59 @floatCast(f32, math.fabs(y) / (2.0 * t)),
60 @floatCast(f32, math.copysign(f64, t, y)),
61 );
5762 }
5863}
5964
60fn sqrt64(z: *const Complex(f64)) Complex(f64) {
65fn sqrt64(z: Complex(f64)) Complex(f64) {
6166 // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
6267 const threshold = 0x1.a827999fcef32p+1022;
6368
std/math/complex/tanh.zig+6-4
......@@ -4,7 +4,7 @@ const math = std.math;
44const cmath = math.complex;
55const Complex = cmath.Complex;
66
7pub fn tanh(z: var) Complex(@typeOf(z.re)) {
7pub fn tanh(z: var) @typeOf(z) {
88 const T = @typeOf(z.re);
99 return switch (T) {
1010 f32 => tanh32(z),
......@@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) {
1313 };
1414}
1515
16fn tanh32(z: *const Complex(f32)) Complex(f32) {
16fn tanh32(z: Complex(f32)) Complex(f32) {
1717 const x = z.re;
1818 const y = z.im;
1919
......@@ -51,12 +51,14 @@ fn tanh32(z: *const Complex(f32)) Complex(f32) {
5151 return Complex(f32).new((beta * rho * s) / den, t / den);
5252}
5353
54fn tanh64(z: *const Complex(f64)) Complex(f64) {
54fn tanh64(z: Complex(f64)) Complex(f64) {
5555 const x = z.re;
5656 const y = z.im;
5757
5858 const fx = @bitCast(u64, x);
59 const hx = u32(fx >> 32);
59 // TODO: zig should allow this conversion implicitly because it can notice that the value necessarily
60 // fits in range.
61 const hx = @intCast(u32, fx >> 32);
6062 const lx = @truncate(u32, fx);
6163 const ix = hx & 0x7fffffff;
6264
std/math/cos.zig+2-2
......@@ -55,7 +55,7 @@ fn cos32(x_: f32) f32 {
5555 }
5656
5757 var y = math.floor(x * m4pi);
58 var j = i64(y);
58 var j = @floatToInt(i64, y);
5959
6060 if (j & 1 == 1) {
6161 j += 1;
......@@ -106,7 +106,7 @@ fn cos64(x_: f64) f64 {
106106 }
107107
108108 var y = math.floor(x * m4pi);
109 var j = i64(y);
109 var j = @floatToInt(i64, y);
110110
111111 if (j & 1 == 1) {
112112 j += 1;
std/math/cosh.zig+1-1
......@@ -49,7 +49,7 @@ fn cosh32(x: f32) f32 {
4949
5050fn cosh64(x: f64) f64 {
5151 const u = @bitCast(u64, x);
52 const w = u32(u >> 32);
52 const w = @intCast(u32, u >> 32);
5353 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
5454
5555 // TODO: Shouldn't need this explicit check.
std/math/exp.zig+6-6
......@@ -29,7 +29,7 @@ fn exp32(x_: f32) f32 {
2929
3030 var x = x_;
3131 var hx = @bitCast(u32, x);
32 const sign = i32(hx >> 31);
32 const sign = @intCast(i32, hx >> 31);
3333 hx &= 0x7FFFFFFF;
3434
3535 if (math.isNan(x)) {
......@@ -63,12 +63,12 @@ fn exp32(x_: f32) f32 {
6363 if (hx > 0x3EB17218) {
6464 // |x| > 1.5 * ln2
6565 if (hx > 0x3F851592) {
66 k = i32(invln2 * x + half[usize(sign)]);
66 k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
6767 } else {
6868 k = 1 - sign - sign;
6969 }
7070
71 const fk = f32(k);
71 const fk = @intToFloat(f32, k);
7272 hi = x - fk * ln2hi;
7373 lo = fk * ln2lo;
7474 x = hi - lo;
......@@ -110,7 +110,7 @@ fn exp64(x_: f64) f64 {
110110 var x = x_;
111111 var ux = @bitCast(u64, x);
112112 var hx = ux >> 32;
113 const sign = i32(hx >> 31);
113 const sign = @intCast(i32, hx >> 31);
114114 hx &= 0x7FFFFFFF;
115115
116116 if (math.isNan(x)) {
......@@ -148,12 +148,12 @@ fn exp64(x_: f64) f64 {
148148 if (hx > 0x3EB17218) {
149149 // |x| >= 1.5 * ln2
150150 if (hx > 0x3FF0A2B2) {
151 k = i32(invln2 * x + half[usize(sign)]);
151 k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
152152 } else {
153153 k = 1 - sign - sign;
154154 }
155155
156 const dk = f64(k);
156 const dk = @intToFloat(f64, k);
157157 hi = x - dk * ln2hi;
158158 lo = dk * ln2lo;
159159 x = hi - lo;
std/math/exp2.zig+7-7
......@@ -38,8 +38,8 @@ const exp2ft = []const f64{
3838fn exp2_32(x: f32) f32 {
3939 @setFloatMode(this, @import("builtin").FloatMode.Strict);
4040
41 const tblsiz = u32(exp2ft.len);
42 const redux: f32 = 0x1.8p23 / f32(tblsiz);
41 const tblsiz = @intCast(u32, exp2ft.len);
42 const redux: f32 = 0x1.8p23 / @intToFloat(f32, tblsiz);
4343 const P1: f32 = 0x1.62e430p-1;
4444 const P2: f32 = 0x1.ebfbe0p-3;
4545 const P3: f32 = 0x1.c6b348p-5;
......@@ -89,7 +89,7 @@ fn exp2_32(x: f32) f32 {
8989 var r: f64 = exp2ft[i0];
9090 const t: f64 = r * z;
9191 r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
92 return f32(r * uk);
92 return @floatCast(f32, r * uk);
9393}
9494
9595const exp2dt = []f64{
......@@ -355,8 +355,8 @@ const exp2dt = []f64{
355355fn exp2_64(x: f64) f64 {
356356 @setFloatMode(this, @import("builtin").FloatMode.Strict);
357357
358 const tblsiz = u32(exp2dt.len / 2);
359 const redux: f64 = 0x1.8p52 / f64(tblsiz);
358 const tblsiz = @intCast(u32, exp2dt.len / 2);
359 const redux: f64 = 0x1.8p52 / @intToFloat(f64, tblsiz);
360360 const P1: f64 = 0x1.62e42fefa39efp-1;
361361 const P2: f64 = 0x1.ebfbdff82c575p-3;
362362 const P3: f64 = 0x1.c6b08d704a0a6p-5;
......@@ -364,7 +364,7 @@ fn exp2_64(x: f64) f64 {
364364 const P5: f64 = 0x1.5d88003875c74p-10;
365365
366366 const ux = @bitCast(u64, x);
367 const ix = u32(ux >> 32) & 0x7FFFFFFF;
367 const ix = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
368368
369369 // TODO: This should be handled beneath.
370370 if (math.isNan(x)) {
......@@ -386,7 +386,7 @@ fn exp2_64(x: f64) f64 {
386386 if (ux >> 63 != 0) {
387387 // underflow
388388 if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
389 math.forceEval(f32(-0x1.0p-149 / x));
389 math.forceEval(@floatCast(f32, -0x1.0p-149 / x));
390390 }
391391 if (x <= -1075) {
392392 return 0;
std/math/expm1.zig+10-10
......@@ -78,8 +78,8 @@ fn expm1_32(x_: f32) f32 {
7878 kf += 0.5;
7979 }
8080
81 k = i32(kf);
82 const t = f32(k);
81 k = @floatToInt(i32, kf);
82 const t = @intToFloat(f32, k);
8383 hi = x - t * ln2_hi;
8484 lo = t * ln2_lo;
8585 }
......@@ -123,7 +123,7 @@ fn expm1_32(x_: f32) f32 {
123123 }
124124 }
125125
126 const twopk = @bitCast(f32, u32((0x7F +% k) << 23));
126 const twopk = @bitCast(f32, @intCast(u32, (0x7F +% k) << 23));
127127
128128 if (k < 0 or k > 56) {
129129 var y = x - e + 1.0;
......@@ -136,7 +136,7 @@ fn expm1_32(x_: f32) f32 {
136136 return y - 1.0;
137137 }
138138
139 const uf = @bitCast(f32, u32(0x7F -% k) << 23);
139 const uf = @bitCast(f32, @intCast(u32, 0x7F -% k) << 23);
140140 if (k < 23) {
141141 return (x - e + (1 - uf)) * twopk;
142142 } else {
......@@ -158,7 +158,7 @@ fn expm1_64(x_: f64) f64 {
158158
159159 var x = x_;
160160 const ux = @bitCast(u64, x);
161 const hx = u32(ux >> 32) & 0x7FFFFFFF;
161 const hx = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
162162 const sign = ux >> 63;
163163
164164 if (math.isNegativeInf(x)) {
......@@ -207,8 +207,8 @@ fn expm1_64(x_: f64) f64 {
207207 kf += 0.5;
208208 }
209209
210 k = i32(kf);
211 const t = f64(k);
210 k = @floatToInt(i32, kf);
211 const t = @intToFloat(f64, k);
212212 hi = x - t * ln2_hi;
213213 lo = t * ln2_lo;
214214 }
......@@ -219,7 +219,7 @@ fn expm1_64(x_: f64) f64 {
219219 // |x| < 2^(-54)
220220 else if (hx < 0x3C900000) {
221221 if (hx < 0x00100000) {
222 math.forceEval(f32(x));
222 math.forceEval(@floatCast(f32, x));
223223 }
224224 return x;
225225 } else {
......@@ -252,7 +252,7 @@ fn expm1_64(x_: f64) f64 {
252252 }
253253 }
254254
255 const twopk = @bitCast(f64, u64(0x3FF +% k) << 52);
255 const twopk = @bitCast(f64, @intCast(u64, 0x3FF +% k) << 52);
256256
257257 if (k < 0 or k > 56) {
258258 var y = x - e + 1.0;
......@@ -265,7 +265,7 @@ fn expm1_64(x_: f64) f64 {
265265 return y - 1.0;
266266 }
267267
268 const uf = @bitCast(f64, u64(0x3FF -% k) << 52);
268 const uf = @bitCast(f64, @intCast(u64, 0x3FF -% k) << 52);
269269 if (k < 20) {
270270 return (x - e + (1 - uf)) * twopk;
271271 } else {
std/math/floor.zig+2-2
......@@ -20,7 +20,7 @@ pub fn floor(x: var) @typeOf(x) {
2020
2121fn floor32(x: f32) f32 {
2222 var u = @bitCast(u32, x);
23 const e = i32((u >> 23) & 0xFF) - 0x7F;
23 const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
2424 var m: u32 = undefined;
2525
2626 // TODO: Shouldn't need this explicit check.
......@@ -33,7 +33,7 @@ fn floor32(x: f32) f32 {
3333 }
3434
3535 if (e >= 0) {
36 m = u32(0x007FFFFF) >> u5(e);
36 m = u32(0x007FFFFF) >> @intCast(u5, e);
3737 if (u & m == 0) {
3838 return x;
3939 }
std/math/fma.zig+3-3
......@@ -17,10 +17,10 @@ fn fma32(x: f32, y: f32, z: f32) f32 {
1717 const e = (u >> 52) & 0x7FF;
1818
1919 if ((u & 0x1FFFFFFF) != 0x10000000 or e == 0x7FF or xy_z - xy == z) {
20 return f32(xy_z);
20 return @floatCast(f32, xy_z);
2121 } else {
2222 // TODO: Handle inexact case with double-rounding
23 return f32(xy_z);
23 return @floatCast(f32, xy_z);
2424 }
2525}
2626
......@@ -124,7 +124,7 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 {
124124 var sum = dd_add(a, b);
125125 if (sum.lo != 0) {
126126 var uhii = @bitCast(u64, sum.hi);
127 const bits_lost = -i32((uhii >> 52) & 0x7FF) - scale + 1;
127 const bits_lost = -@intCast(i32, (uhii >> 52) & 0x7FF) - scale + 1;
128128 if ((bits_lost != 1) == (uhii & 1 != 0)) {
129129 const uloi = @bitCast(u64, sum.lo);
130130 uhii += 1 - (((uhii ^ uloi) >> 62) & 2);
std/math/frexp.zig+2-2
......@@ -30,7 +30,7 @@ fn frexp32(x: f32) frexp32_result {
3030 var result: frexp32_result = undefined;
3131
3232 var y = @bitCast(u32, x);
33 const e = i32(y >> 23) & 0xFF;
33 const e = @intCast(i32, y >> 23) & 0xFF;
3434
3535 if (e == 0) {
3636 if (x != 0) {
......@@ -67,7 +67,7 @@ fn frexp64(x: f64) frexp64_result {
6767 var result: frexp64_result = undefined;
6868
6969 var y = @bitCast(u64, x);
70 const e = i32(y >> 52) & 0x7FF;
70 const e = @intCast(i32, y >> 52) & 0x7FF;
7171
7272 if (e == 0) {
7373 if (x != 0) {
std/math/hypot.zig+1-1
......@@ -49,7 +49,7 @@ fn hypot32(x: f32, y: f32) f32 {
4949 yy *= 0x1.0p-90;
5050 }
5151
52 return z * math.sqrt(f32(f64(x) * x + f64(y) * y));
52 return z * math.sqrt(@floatCast(f32, f64(x) * x + f64(y) * y));
5353}
5454
5555fn sq(hi: *f64, lo: *f64, x: f64) void {
std/math/ilogb.zig+2-2
......@@ -23,7 +23,7 @@ const fp_ilogb0 = fp_ilogbnan;
2323
2424fn ilogb32(x: f32) i32 {
2525 var u = @bitCast(u32, x);
26 var e = i32((u >> 23) & 0xFF);
26 var e = @intCast(i32, (u >> 23) & 0xFF);
2727
2828 // TODO: We should be able to merge this with the lower check.
2929 if (math.isNan(x)) {
......@@ -59,7 +59,7 @@ fn ilogb32(x: f32) i32 {
5959
6060fn ilogb64(x: f64) i32 {
6161 var u = @bitCast(u64, x);
62 var e = i32((u >> 52) & 0x7FF);
62 var e = @intCast(i32, (u >> 52) & 0x7FF);
6363
6464 if (math.isNan(x)) {
6565 return @maxValue(i32);
std/math/index.zig+18-7
......@@ -227,7 +227,7 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
227227/// A negative shift amount results in a right shift.
228228pub fn shl(comptime T: type, a: T, shift_amt: var) T {
229229 const abs_shift_amt = absCast(shift_amt);
230 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
230 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
231231
232232 if (@typeOf(shift_amt).is_signed) {
233233 if (shift_amt >= 0) {
......@@ -251,7 +251,7 @@ test "math.shl" {
251251/// A negative shift amount results in a lefft shift.
252252pub fn shr(comptime T: type, a: T, shift_amt: var) T {
253253 const abs_shift_amt = absCast(shift_amt);
254 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
254 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
255255
256256 if (@typeOf(shift_amt).is_signed) {
257257 if (shift_amt >= 0) {
......@@ -473,9 +473,9 @@ fn testRem() void {
473473/// Result is an unsigned integer.
474474pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
475475 const uint = @IntType(false, @typeOf(x).bit_count);
476 if (x >= 0) return uint(x);
476 if (x >= 0) return @intCast(uint, x);
477477
478 return uint(-(x + 1)) + 1;
478 return @intCast(uint, -(x + 1)) + 1;
479479}
480480
481481test "math.absCast" {
......@@ -499,7 +499,7 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
499499
500500 if (x == -@minValue(int)) return @minValue(int);
501501
502 return -int(x);
502 return -@intCast(int, x);
503503}
504504
505505test "math.negateCast" {
......@@ -522,7 +522,7 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
522522 } else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
523523 return error.Overflow;
524524 } else {
525 return T(x);
525 return @intCast(T, x);
526526 }
527527}
528528
......@@ -565,7 +565,7 @@ test "math.floorPowerOfTwo" {
565565
566566pub fn log2_int(comptime T: type, x: T) Log2Int(T) {
567567 assert(x != 0);
568 return Log2Int(T)(T.bit_count - 1 - @clz(x));
568 return @intCast(Log2Int(T), T.bit_count - 1 - @clz(x));
569569}
570570
571571pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
......@@ -597,3 +597,14 @@ fn testFloorPowerOfTwo() void {
597597 assert(floorPowerOfTwo(u4, 8) == 8);
598598 assert(floorPowerOfTwo(u4, 9) == 8);
599599}
600
601pub fn lossyCast(comptime T: type, value: var) T {
602 switch (@typeInfo(@typeOf(value))) {
603 builtin.TypeId.Int => return @intToFloat(T, value),
604 builtin.TypeId.Float => return @floatCast(T, value),
605 builtin.TypeId.ComptimeInt => return T(value),
606 builtin.TypeId.ComptimeFloat => return T(value),
607 else => @compileError("bad type"),
608 }
609}
610
std/math/ln.zig+6-6
......@@ -71,7 +71,7 @@ pub fn ln_32(x_: f32) f32 {
7171
7272 // x into [sqrt(2) / 2, sqrt(2)]
7373 ix += 0x3F800000 - 0x3F3504F3;
74 k += i32(ix >> 23) - 0x7F;
74 k += @intCast(i32, ix >> 23) - 0x7F;
7575 ix = (ix & 0x007FFFFF) + 0x3F3504F3;
7676 x = @bitCast(f32, ix);
7777
......@@ -83,7 +83,7 @@ pub fn ln_32(x_: f32) f32 {
8383 const t2 = z * (Lg1 + w * Lg3);
8484 const R = t2 + t1;
8585 const hfsq = 0.5 * f * f;
86 const dk = f32(k);
86 const dk = @intToFloat(f32, k);
8787
8888 return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
8989}
......@@ -103,7 +103,7 @@ pub fn ln_64(x_: f64) f64 {
103103
104104 var x = x_;
105105 var ix = @bitCast(u64, x);
106 var hx = u32(ix >> 32);
106 var hx = @intCast(u32, ix >> 32);
107107 var k: i32 = 0;
108108
109109 if (hx < 0x00100000 or hx >> 31 != 0) {
......@@ -119,7 +119,7 @@ pub fn ln_64(x_: f64) f64 {
119119 // subnormal, scale x
120120 k -= 54;
121121 x *= 0x1.0p54;
122 hx = u32(@bitCast(u64, ix) >> 32);
122 hx = @intCast(u32, @bitCast(u64, ix) >> 32);
123123 } else if (hx >= 0x7FF00000) {
124124 return x;
125125 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
......@@ -128,7 +128,7 @@ pub fn ln_64(x_: f64) f64 {
128128
129129 // x into [sqrt(2) / 2, sqrt(2)]
130130 hx += 0x3FF00000 - 0x3FE6A09E;
131 k += i32(hx >> 20) - 0x3FF;
131 k += @intCast(i32, hx >> 20) - 0x3FF;
132132 hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
133133 ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
134134 x = @bitCast(f64, ix);
......@@ -141,7 +141,7 @@ pub fn ln_64(x_: f64) f64 {
141141 const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
142142 const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
143143 const R = t2 + t1;
144 const dk = f64(k);
144 const dk = @intToFloat(f64, k);
145145
146146 return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
147147}
std/math/log.zig+6-5
......@@ -13,22 +13,23 @@ pub fn log(comptime T: type, base: T, x: T) T {
1313 return math.ln(x);
1414 }
1515
16 const float_base = math.lossyCast(f64, base);
1617 switch (@typeId(T)) {
1718 TypeId.ComptimeFloat => {
18 return @typeOf(1.0)(math.ln(f64(x)) / math.ln(f64(base)));
19 return @typeOf(1.0)(math.ln(f64(x)) / math.ln(float_base));
1920 },
2021 TypeId.ComptimeInt => {
21 return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
22 return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(float_base)));
2223 },
2324 builtin.TypeId.Int => {
2425 // TODO implement integer log without using float math
25 return T(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
26 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
2627 },
2728
2829 builtin.TypeId.Float => {
2930 switch (T) {
30 f32 => return f32(math.ln(f64(x)) / math.ln(f64(base))),
31 f64 => return math.ln(x) / math.ln(f64(base)),
31 f32 => return @floatCast(f32, math.ln(f64(x)) / math.ln(float_base)),
32 f64 => return math.ln(x) / math.ln(float_base),
3233 else => @compileError("log not implemented for " ++ @typeName(T)),
3334 }
3435 },
std/math/log10.zig+7-7
......@@ -28,7 +28,7 @@ pub fn log10(x: var) @typeOf(x) {
2828 return @typeOf(1)(math.floor(log10_64(f64(x))));
2929 },
3030 TypeId.Int => {
31 return T(math.floor(log10_64(f64(x))));
31 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
3232 },
3333 else => @compileError("log10 not implemented for " ++ @typeName(T)),
3434 }
......@@ -71,7 +71,7 @@ pub fn log10_32(x_: f32) f32 {
7171
7272 // x into [sqrt(2) / 2, sqrt(2)]
7373 ix += 0x3F800000 - 0x3F3504F3;
74 k += i32(ix >> 23) - 0x7F;
74 k += @intCast(i32, ix >> 23) - 0x7F;
7575 ix = (ix & 0x007FFFFF) + 0x3F3504F3;
7676 x = @bitCast(f32, ix);
7777
......@@ -89,7 +89,7 @@ pub fn log10_32(x_: f32) f32 {
8989 u &= 0xFFFFF000;
9090 hi = @bitCast(f32, u);
9191 const lo = f - hi - hfsq + s * (hfsq + R);
92 const dk = f32(k);
92 const dk = @intToFloat(f32, k);
9393
9494 return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi;
9595}
......@@ -109,7 +109,7 @@ pub fn log10_64(x_: f64) f64 {
109109
110110 var x = x_;
111111 var ix = @bitCast(u64, x);
112 var hx = u32(ix >> 32);
112 var hx = @intCast(u32, ix >> 32);
113113 var k: i32 = 0;
114114
115115 if (hx < 0x00100000 or hx >> 31 != 0) {
......@@ -125,7 +125,7 @@ pub fn log10_64(x_: f64) f64 {
125125 // subnormal, scale x
126126 k -= 54;
127127 x *= 0x1.0p54;
128 hx = u32(@bitCast(u64, x) >> 32);
128 hx = @intCast(u32, @bitCast(u64, x) >> 32);
129129 } else if (hx >= 0x7FF00000) {
130130 return x;
131131 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
......@@ -134,7 +134,7 @@ pub fn log10_64(x_: f64) f64 {
134134
135135 // x into [sqrt(2) / 2, sqrt(2)]
136136 hx += 0x3FF00000 - 0x3FE6A09E;
137 k += i32(hx >> 20) - 0x3FF;
137 k += @intCast(i32, hx >> 20) - 0x3FF;
138138 hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
139139 ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
140140 x = @bitCast(f64, ix);
......@@ -157,7 +157,7 @@ pub fn log10_64(x_: f64) f64 {
157157
158158 // val_hi + val_lo ~ log10(1 + f) + k * log10(2)
159159 var val_hi = hi * ivln10hi;
160 const dk = f64(k);
160 const dk = @intToFloat(f64, k);
161161 const y = dk * log10_2hi;
162162 var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;
163163
std/math/log1p.zig+6-6
......@@ -68,7 +68,7 @@ fn log1p_32(x: f32) f32 {
6868 const uf = 1 + x;
6969 var iu = @bitCast(u32, uf);
7070 iu += 0x3F800000 - 0x3F3504F3;
71 k = i32(iu >> 23) - 0x7F;
71 k = @intCast(i32, iu >> 23) - 0x7F;
7272
7373 // correction to avoid underflow in c / u
7474 if (k < 25) {
......@@ -90,7 +90,7 @@ fn log1p_32(x: f32) f32 {
9090 const t2 = z * (Lg1 + w * Lg3);
9191 const R = t2 + t1;
9292 const hfsq = 0.5 * f * f;
93 const dk = f32(k);
93 const dk = @intToFloat(f32, k);
9494
9595 return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
9696}
......@@ -107,7 +107,7 @@ fn log1p_64(x: f64) f64 {
107107 const Lg7: f64 = 1.479819860511658591e-01;
108108
109109 var ix = @bitCast(u64, x);
110 var hx = u32(ix >> 32);
110 var hx = @intCast(u32, ix >> 32);
111111 var k: i32 = 1;
112112 var c: f64 = undefined;
113113 var f: f64 = undefined;
......@@ -145,9 +145,9 @@ fn log1p_64(x: f64) f64 {
145145 if (k != 0) {
146146 const uf = 1 + x;
147147 const hu = @bitCast(u64, uf);
148 var iu = u32(hu >> 32);
148 var iu = @intCast(u32, hu >> 32);
149149 iu += 0x3FF00000 - 0x3FE6A09E;
150 k = i32(iu >> 20) - 0x3FF;
150 k = @intCast(i32, iu >> 20) - 0x3FF;
151151
152152 // correction to avoid underflow in c / u
153153 if (k < 54) {
......@@ -170,7 +170,7 @@ fn log1p_64(x: f64) f64 {
170170 const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
171171 const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
172172 const R = t2 + t1;
173 const dk = f64(k);
173 const dk = @intToFloat(f64, k);
174174
175175 return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
176176}
std/math/log2.zig+6-6
......@@ -75,7 +75,7 @@ pub fn log2_32(x_: f32) f32 {
7575
7676 // x into [sqrt(2) / 2, sqrt(2)]
7777 ix += 0x3F800000 - 0x3F3504F3;
78 k += i32(ix >> 23) - 0x7F;
78 k += @intCast(i32, ix >> 23) - 0x7F;
7979 ix = (ix & 0x007FFFFF) + 0x3F3504F3;
8080 x = @bitCast(f32, ix);
8181
......@@ -93,7 +93,7 @@ pub fn log2_32(x_: f32) f32 {
9393 u &= 0xFFFFF000;
9494 hi = @bitCast(f32, u);
9595 const lo = f - hi - hfsq + s * (hfsq + R);
96 return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + f32(k);
96 return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + @intToFloat(f32, k);
9797}
9898
9999pub fn log2_64(x_: f64) f64 {
......@@ -109,7 +109,7 @@ pub fn log2_64(x_: f64) f64 {
109109
110110 var x = x_;
111111 var ix = @bitCast(u64, x);
112 var hx = u32(ix >> 32);
112 var hx = @intCast(u32, ix >> 32);
113113 var k: i32 = 0;
114114
115115 if (hx < 0x00100000 or hx >> 31 != 0) {
......@@ -125,7 +125,7 @@ pub fn log2_64(x_: f64) f64 {
125125 // subnormal, scale x
126126 k -= 54;
127127 x *= 0x1.0p54;
128 hx = u32(@bitCast(u64, x) >> 32);
128 hx = @intCast(u32, @bitCast(u64, x) >> 32);
129129 } else if (hx >= 0x7FF00000) {
130130 return x;
131131 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
......@@ -134,7 +134,7 @@ pub fn log2_64(x_: f64) f64 {
134134
135135 // x into [sqrt(2) / 2, sqrt(2)]
136136 hx += 0x3FF00000 - 0x3FE6A09E;
137 k += i32(hx >> 20) - 0x3FF;
137 k += @intCast(i32, hx >> 20) - 0x3FF;
138138 hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
139139 ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
140140 x = @bitCast(f64, ix);
......@@ -159,7 +159,7 @@ pub fn log2_64(x_: f64) f64 {
159159 var val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
160160
161161 // spadd(val_hi, val_lo, y)
162 const y = f64(k);
162 const y = @intToFloat(f64, k);
163163 const ww = y + val_hi;
164164 val_lo += (y - ww) + val_hi;
165165 val_hi = ww;
std/math/modf.zig+4-4
......@@ -29,7 +29,7 @@ fn modf32(x: f32) modf32_result {
2929 var result: modf32_result = undefined;
3030
3131 const u = @bitCast(u32, x);
32 const e = i32((u >> 23) & 0xFF) - 0x7F;
32 const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
3333 const us = u & 0x80000000;
3434
3535 // TODO: Shouldn't need this.
......@@ -57,7 +57,7 @@ fn modf32(x: f32) modf32_result {
5757 return result;
5858 }
5959
60 const mask = u32(0x007FFFFF) >> u5(e);
60 const mask = u32(0x007FFFFF) >> @intCast(u5, e);
6161 if (u & mask == 0) {
6262 result.ipart = x;
6363 result.fpart = @bitCast(f32, us);
......@@ -74,7 +74,7 @@ fn modf64(x: f64) modf64_result {
7474 var result: modf64_result = undefined;
7575
7676 const u = @bitCast(u64, x);
77 const e = i32((u >> 52) & 0x7FF) - 0x3FF;
77 const e = @intCast(i32, (u >> 52) & 0x7FF) - 0x3FF;
7878 const us = u & (1 << 63);
7979
8080 if (math.isInf(x)) {
......@@ -101,7 +101,7 @@ fn modf64(x: f64) modf64_result {
101101 return result;
102102 }
103103
104 const mask = u64(@maxValue(u64) >> 12) >> u6(e);
104 const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e);
105105 if (u & mask == 0) {
106106 result.ipart = x;
107107 result.fpart = @bitCast(f64, us);
std/math/pow.zig+2-2
......@@ -146,7 +146,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
146146 var xe = r2.exponent;
147147 var x1 = r2.significand;
148148
149 var i = i32(yi);
149 var i = @floatToInt(i32, yi);
150150 while (i != 0) : (i >>= 1) {
151151 if (i & 1 == 1) {
152152 a1 *= x1;
......@@ -171,7 +171,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
171171
172172fn isOddInteger(x: f64) bool {
173173 const r = math.modf(x);
174 return r.fpart == 0.0 and i64(r.ipart) & 1 == 1;
174 return r.fpart == 0.0 and @floatToInt(i64, r.ipart) & 1 == 1;
175175}
176176
177177test "math.pow" {
std/math/scalbn.zig+2-2
......@@ -37,7 +37,7 @@ fn scalbn32(x: f32, n_: i32) f32 {
3737 }
3838 }
3939
40 const u = u32(n +% 0x7F) << 23;
40 const u = @intCast(u32, n +% 0x7F) << 23;
4141 return y * @bitCast(f32, u);
4242}
4343
......@@ -67,7 +67,7 @@ fn scalbn64(x: f64, n_: i32) f64 {
6767 }
6868 }
6969
70 const u = u64(n +% 0x3FF) << 52;
70 const u = @intCast(u64, n +% 0x3FF) << 52;
7171 return y * @bitCast(f64, u);
7272}
7373
std/math/sin.zig+2-2
......@@ -60,7 +60,7 @@ fn sin32(x_: f32) f32 {
6060 }
6161
6262 var y = math.floor(x * m4pi);
63 var j = i64(y);
63 var j = @floatToInt(i64, y);
6464
6565 if (j & 1 == 1) {
6666 j += 1;
......@@ -112,7 +112,7 @@ fn sin64(x_: f64) f64 {
112112 }
113113
114114 var y = math.floor(x * m4pi);
115 var j = i64(y);
115 var j = @floatToInt(i64, y);
116116
117117 if (j & 1 == 1) {
118118 j += 1;
std/math/sinh.zig+1-1
......@@ -57,7 +57,7 @@ fn sinh64(x: f64) f64 {
5757 @setFloatMode(this, @import("builtin").FloatMode.Strict);
5858
5959 const u = @bitCast(u64, x);
60 const w = u32(u >> 32);
60 const w = @intCast(u32, u >> 32);
6161 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
6262
6363 if (x == 0.0 or math.isNan(x)) {
std/math/sqrt.zig+1-1
......@@ -99,7 +99,7 @@ fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) {
9999 }
100100
101101 const ResultType = @IntType(false, T.bit_count / 2);
102 return ResultType(res);
102 return @intCast(ResultType, res);
103103}
104104
105105test "math.sqrt_int" {
std/math/tan.zig+2-2
......@@ -53,7 +53,7 @@ fn tan32(x_: f32) f32 {
5353 }
5454
5555 var y = math.floor(x * m4pi);
56 var j = i64(y);
56 var j = @floatToInt(i64, y);
5757
5858 if (j & 1 == 1) {
5959 j += 1;
......@@ -102,7 +102,7 @@ fn tan64(x_: f64) f64 {
102102 }
103103
104104 var y = math.floor(x * m4pi);
105 var j = i64(y);
105 var j = @floatToInt(i64, y);
106106
107107 if (j & 1 == 1) {
108108 j += 1;
std/math/tanh.zig+2-2
......@@ -68,7 +68,7 @@ fn tanh32(x: f32) f32 {
6868
6969fn tanh64(x: f64) f64 {
7070 const u = @bitCast(u64, x);
71 const w = u32(u >> 32);
71 const w = @intCast(u32, u >> 32);
7272 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
7373
7474 var t: f64 = undefined;
......@@ -100,7 +100,7 @@ fn tanh64(x: f64) f64 {
100100 }
101101 // |x| is subnormal
102102 else {
103 math.forceEval(f32(x));
103 math.forceEval(@floatCast(f32, x));
104104 t = x;
105105 }
106106
std/math/trunc.zig+4-4
......@@ -19,7 +19,7 @@ pub fn trunc(x: var) @typeOf(x) {
1919
2020fn trunc32(x: f32) f32 {
2121 const u = @bitCast(u32, x);
22 var e = i32(((u >> 23) & 0xFF)) - 0x7F + 9;
22 var e = @intCast(i32, ((u >> 23) & 0xFF)) - 0x7F + 9;
2323 var m: u32 = undefined;
2424
2525 if (e >= 23 + 9) {
......@@ -29,7 +29,7 @@ fn trunc32(x: f32) f32 {
2929 e = 1;
3030 }
3131
32 m = u32(@maxValue(u32)) >> u5(e);
32 m = u32(@maxValue(u32)) >> @intCast(u5, e);
3333 if (u & m == 0) {
3434 return x;
3535 } else {
......@@ -40,7 +40,7 @@ fn trunc32(x: f32) f32 {
4040
4141fn trunc64(x: f64) f64 {
4242 const u = @bitCast(u64, x);
43 var e = i32(((u >> 52) & 0x7FF)) - 0x3FF + 12;
43 var e = @intCast(i32, ((u >> 52) & 0x7FF)) - 0x3FF + 12;
4444 var m: u64 = undefined;
4545
4646 if (e >= 52 + 12) {
......@@ -50,7 +50,7 @@ fn trunc64(x: f64) f64 {
5050 e = 1;
5151 }
5252
53 m = u64(@maxValue(u64)) >> u6(e);
53 m = u64(@maxValue(u64)) >> @intCast(u6, e);
5454 if (u & m == 0) {
5555 return x;
5656 } else {
std/mem.zig+1-1
......@@ -334,7 +334,7 @@ pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) T {
334334 builtin.Endian.Little => {
335335 const ShiftType = math.Log2Int(T);
336336 for (bytes) |b, index| {
337 result = result | (T(b) << ShiftType(index * 8));
337 result = result | (T(b) << @intCast(ShiftType, index * 8));
338338 }
339339 },
340340 }
std/os/child_process.zig+1-1
......@@ -413,7 +413,7 @@ pub const ChildProcess = struct {
413413 }
414414
415415 // we are the parent
416 const pid = i32(pid_result);
416 const pid = @intCast(i32, pid_result);
417417 if (self.stdin_behavior == StdIo.Pipe) {
418418 self.stdin = os.File.openHandle(stdin_pipe[1]);
419419 } else {
std/os/darwin.zig+9-2
......@@ -290,7 +290,7 @@ pub fn WIFSIGNALED(x: i32) bool {
290290/// Get the errno from a syscall return value, or 0 for no error.
291291pub fn getErrno(r: usize) usize {
292292 const signed_r = @bitCast(isize, r);
293 return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
293 return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
294294}
295295
296296pub fn close(fd: i32) usize {
......@@ -339,7 +339,14 @@ pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
339339}
340340
341341pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
342 const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
342 const ptr_result = c.mmap(
343 @ptrCast(*c_void, address),
344 length,
345 @bitCast(c_int, @intCast(c_uint, prot)),
346 @bitCast(c_int, c_uint(flags)),
347 fd,
348 offset,
349 );
343350 const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
344351 return errnoWrap(isize_result);
345352}
std/os/file.zig+3-3
......@@ -266,7 +266,7 @@ pub const File = struct {
266266 pub fn getEndPos(self: *File) !usize {
267267 if (is_posix) {
268268 const stat = try os.posixFStat(self.handle);
269 return usize(stat.size);
269 return @intCast(usize, stat.size);
270270 } else if (is_windows) {
271271 var file_size: windows.LARGE_INTEGER = undefined;
272272 if (windows.GetFileSizeEx(self.handle, &file_size) == 0) {
......@@ -277,7 +277,7 @@ pub const File = struct {
277277 }
278278 if (file_size < 0)
279279 return error.Overflow;
280 return math.cast(usize, u64(file_size));
280 return math.cast(usize, @intCast(u64, file_size));
281281 } else {
282282 @compileError("TODO support getEndPos on this OS");
283283 }
......@@ -343,7 +343,7 @@ pub const File = struct {
343343 } else if (is_windows) {
344344 var index: usize = 0;
345345 while (index < buffer.len) {
346 const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
346 const want_read_count = @intCast(windows.DWORD, math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
347347 var amt_read: windows.DWORD = undefined;
348348 if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
349349 const err = windows.GetLastError();
std/os/index.zig+8-8
......@@ -126,7 +126,7 @@ pub fn getRandomBytes(buf: []u8) !void {
126126 }
127127 defer _ = windows.CryptReleaseContext(hCryptProv, 0);
128128
129 if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) {
129 if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) {
130130 const err = windows.GetLastError();
131131 return switch (err) {
132132 else => unexpectedErrorWindows(err),
......@@ -343,7 +343,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 {
343343 else => return unexpectedErrorPosix(err),
344344 }
345345 }
346 return i32(result);
346 return @intCast(i32, result);
347347 }
348348}
349349
......@@ -586,7 +586,7 @@ pub fn getCwd(allocator: *Allocator) ![]u8 {
586586 errdefer allocator.free(buf);
587587
588588 while (true) {
589 const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
589 const result = windows.GetCurrentDirectoryA(@intCast(windows.WORD, buf.len), buf.ptr);
590590
591591 if (result == 0) {
592592 const err = windows.GetLastError();
......@@ -2019,7 +2019,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
20192019 const rc = posix.socket(domain, socket_type, protocol);
20202020 const err = posix.getErrno(rc);
20212021 switch (err) {
2022 0 => return i32(rc),
2022 0 => return @intCast(i32, rc),
20232023 posix.EACCES => return PosixSocketError.PermissionDenied,
20242024 posix.EAFNOSUPPORT => return PosixSocketError.AddressFamilyNotSupported,
20252025 posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable,
......@@ -2183,7 +2183,7 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!
21832183 const rc = posix.accept4(fd, addr, &sockaddr_size, flags);
21842184 const err = posix.getErrno(rc);
21852185 switch (err) {
2186 0 => return i32(rc),
2186 0 => return @intCast(i32, rc),
21872187 posix.EINTR => continue,
21882188 else => return unexpectedErrorPosix(err),
21892189
......@@ -2226,7 +2226,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
22262226 const rc = posix.epoll_create1(flags);
22272227 const err = posix.getErrno(rc);
22282228 switch (err) {
2229 0 => return i32(rc),
2229 0 => return @intCast(i32, rc),
22302230 else => return unexpectedErrorPosix(err),
22312231
22322232 posix.EINVAL => return LinuxEpollCreateError.InvalidSyscall,
......@@ -2296,7 +2296,7 @@ pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) Lin
22962296
22972297pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
22982298 while (true) {
2299 const rc = posix.epoll_wait(epfd, events.ptr, u32(events.len), timeout);
2299 const rc = posix.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout);
23002300 const err = posix.getErrno(rc);
23012301 switch (err) {
23022302 0 => return rc,
......@@ -2661,7 +2661,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
26612661 posix.EAGAIN => return SpawnThreadError.SystemResources,
26622662 posix.EPERM => unreachable,
26632663 posix.EINVAL => unreachable,
2664 else => return unexpectedErrorPosix(usize(err)),
2664 else => return unexpectedErrorPosix(@intCast(usize, err)),
26652665 }
26662666 } else if (builtin.os == builtin.Os.linux) {
26672667 // use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly
std/os/linux/index.zig+40-40
......@@ -642,7 +642,7 @@ pub fn WIFEXITED(s: i32) bool {
642642 return WTERMSIG(s) == 0;
643643}
644644pub fn WIFSTOPPED(s: i32) bool {
645 return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
645 return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
646646}
647647pub fn WIFSIGNALED(s: i32) bool {
648648 return (unsigned(s) & 0xffff) -% 1 < 0xff;
......@@ -658,11 +658,11 @@ pub const winsize = extern struct {
658658/// Get the errno from a syscall return value, or 0 for no error.
659659pub fn getErrno(r: usize) usize {
660660 const signed_r = @bitCast(isize, r);
661 return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
661 return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
662662}
663663
664664pub fn dup2(old: i32, new: i32) usize {
665 return syscall2(SYS_dup2, usize(old), usize(new));
665 return syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
666666}
667667
668668// TODO https://github.com/ziglang/zig/issues/265
......@@ -693,12 +693,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
693693}
694694
695695pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
696 return syscall3(SYS_getdents, usize(fd), @ptrToInt(dirp), count);
696 return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
697697}
698698
699699pub fn isatty(fd: i32) bool {
700700 var wsz: winsize = undefined;
701 return syscall3(SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
701 return syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
702702}
703703
704704// TODO https://github.com/ziglang/zig/issues/265
......@@ -727,7 +727,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
727727}
728728
729729pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
730 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), @bitCast(usize, offset));
730 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
731731}
732732
733733pub fn munmap(address: usize, length: usize) usize {
......@@ -735,7 +735,7 @@ pub fn munmap(address: usize, length: usize) usize {
735735}
736736
737737pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
738 return syscall3(SYS_read, usize(fd), @ptrToInt(buf), count);
738 return syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count);
739739}
740740
741741// TODO https://github.com/ziglang/zig/issues/265
......@@ -749,7 +749,7 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
749749}
750750
751751pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
752 return syscall4(SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
752 return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
753753}
754754
755755// TODO https://github.com/ziglang/zig/issues/265
......@@ -766,11 +766,11 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {
766766}
767767
768768pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
769 return syscall3(SYS_write, usize(fd), @ptrToInt(buf), count);
769 return syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count);
770770}
771771
772772pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
773 return syscall4(SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
773 return syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset);
774774}
775775
776776// TODO https://github.com/ziglang/zig/issues/265
......@@ -790,7 +790,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
790790
791791// TODO https://github.com/ziglang/zig/issues/265
792792pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
793 return syscall4(SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
793 return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
794794}
795795
796796/// See also `clone` (from the arch-specific include)
......@@ -804,11 +804,11 @@ pub fn clone2(flags: usize, child_stack_ptr: usize) usize {
804804}
805805
806806pub fn close(fd: i32) usize {
807 return syscall1(SYS_close, usize(fd));
807 return syscall1(SYS_close, @intCast(usize, fd));
808808}
809809
810810pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
811 return syscall3(SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos);
811 return syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos);
812812}
813813
814814pub fn exit(status: i32) noreturn {
......@@ -817,11 +817,11 @@ pub fn exit(status: i32) noreturn {
817817}
818818
819819pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
820 return syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));
820 return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
821821}
822822
823823pub fn kill(pid: i32, sig: i32) usize {
824 return syscall2(SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
824 return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @intCast(usize, sig));
825825}
826826
827827// TODO https://github.com/ziglang/zig/issues/265
......@@ -999,8 +999,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len;
999999pub fn raise(sig: i32) usize {
10001000 var set: sigset_t = undefined;
10011001 blockAppSignals(&set);
1002 const tid = i32(syscall0(SYS_gettid));
1003 const ret = syscall2(SYS_tkill, usize(tid), usize(sig));
1002 const tid = @intCast(i32, syscall0(SYS_gettid));
1003 const ret = syscall2(SYS_tkill, @intCast(usize, tid), @intCast(usize, sig));
10041004 restoreSignals(&set);
10051005 return ret;
10061006}
......@@ -1019,12 +1019,12 @@ fn restoreSignals(set: *sigset_t) void {
10191019
10201020pub fn sigaddset(set: *sigset_t, sig: u6) void {
10211021 const s = sig - 1;
1022 (set.*)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
1022 (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
10231023}
10241024
10251025pub fn sigismember(set: *const sigset_t, sig: u6) bool {
10261026 const s = sig - 1;
1027 return ((set.*)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
1027 return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
10281028}
10291029
10301030pub const in_port_t = u16;
......@@ -1057,11 +1057,11 @@ pub const iovec = extern struct {
10571057};
10581058
10591059pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
1060 return syscall3(SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
1060 return syscall3(SYS_getsockname, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
10611061}
10621062
10631063pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
1064 return syscall3(SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
1064 return syscall3(SYS_getpeername, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
10651065}
10661066
10671067pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
......@@ -1069,47 +1069,47 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
10691069}
10701070
10711071pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
1072 return syscall5(SYS_setsockopt, usize(fd), level, optname, usize(optval), @ptrToInt(optlen));
1072 return syscall5(SYS_setsockopt, @intCast(usize, fd), level, optname, @intCast(usize, optval), @ptrToInt(optlen));
10731073}
10741074
10751075pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
1076 return syscall5(SYS_getsockopt, usize(fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
1076 return syscall5(SYS_getsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
10771077}
10781078
10791079pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize {
1080 return syscall3(SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
1080 return syscall3(SYS_sendmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
10811081}
10821082
10831083pub fn connect(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
1084 return syscall3(SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
1084 return syscall3(SYS_connect, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
10851085}
10861086
10871087pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
1088 return syscall3(SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
1088 return syscall3(SYS_recvmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
10891089}
10901090
10911091pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
1092 return syscall6(SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
1092 return syscall6(SYS_recvfrom, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
10931093}
10941094
10951095pub fn shutdown(fd: i32, how: i32) usize {
1096 return syscall2(SYS_shutdown, usize(fd), usize(how));
1096 return syscall2(SYS_shutdown, @intCast(usize, fd), @intCast(usize, how));
10971097}
10981098
10991099pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
1100 return syscall3(SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
1100 return syscall3(SYS_bind, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
11011101}
11021102
11031103pub fn listen(fd: i32, backlog: u32) usize {
1104 return syscall2(SYS_listen, usize(fd), backlog);
1104 return syscall2(SYS_listen, @intCast(usize, fd), backlog);
11051105}
11061106
11071107pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
1108 return syscall6(SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
1108 return syscall6(SYS_sendto, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
11091109}
11101110
11111111pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
1112 return syscall4(SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(*fd[0]));
1112 return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(*fd[0]));
11131113}
11141114
11151115pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
......@@ -1117,11 +1117,11 @@ pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
11171117}
11181118
11191119pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
1120 return syscall4(SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
1120 return syscall4(SYS_accept4, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len), flags);
11211121}
11221122
11231123pub fn fstat(fd: i32, stat_buf: *Stat) usize {
1124 return syscall2(SYS_fstat, usize(fd), @ptrToInt(stat_buf));
1124 return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
11251125}
11261126
11271127// TODO https://github.com/ziglang/zig/issues/265
......@@ -1214,15 +1214,15 @@ pub fn epoll_create1(flags: usize) usize {
12141214}
12151215
12161216pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
1217 return syscall4(SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
1217 return syscall4(SYS_epoll_ctl, @intCast(usize, epoll_fd), @intCast(usize, op), @intCast(usize, fd), @ptrToInt(ev));
12181218}
12191219
12201220pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
1221 return syscall4(SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
1221 return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
12221222}
12231223
12241224pub fn timerfd_create(clockid: i32, flags: u32) usize {
1225 return syscall2(SYS_timerfd_create, usize(clockid), usize(flags));
1225 return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
12261226}
12271227
12281228pub const itimerspec = extern struct {
......@@ -1231,11 +1231,11 @@ pub const itimerspec = extern struct {
12311231};
12321232
12331233pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
1234 return syscall2(SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
1234 return syscall2(SYS_timerfd_gettime, @intCast(usize, fd), @ptrToInt(curr_value));
12351235}
12361236
12371237pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
1238 return syscall4(SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
1238 return syscall4(SYS_timerfd_settime, @intCast(usize, fd), @intCast(usize, flags), @ptrToInt(new_value), @ptrToInt(old_value));
12391239}
12401240
12411241pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
......@@ -1345,7 +1345,7 @@ pub const cap_user_data_t = extern struct {
13451345};
13461346
13471347pub fn unshare(flags: usize) usize {
1348 return syscall1(SYS_unshare, usize(flags));
1348 return syscall1(SYS_unshare, @intCast(usize, flags));
13491349}
13501350
13511351pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
std/os/linux/test.zig+3-3
......@@ -21,7 +21,7 @@ test "timer" {
2121 .it_value = time_interval,
2222 };
2323
24 err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
24 err = linux.timerfd_settime(@intCast(i32, timer_fd), 0, &new_time, null);
2525 assert(err == 0);
2626
2727 var event = linux.epoll_event{
......@@ -29,12 +29,12 @@ test "timer" {
2929 .data = linux.epoll_data{ .ptr = 0 },
3030 };
3131
32 err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);
32 err = linux.epoll_ctl(@intCast(i32, epoll_fd), linux.EPOLL_CTL_ADD, @intCast(i32, timer_fd), &event);
3333 assert(err == 0);
3434
3535 const events_one: linux.epoll_event = undefined;
3636 var events = []linux.epoll_event{events_one} ** 8;
3737
3838 // TODO implicit cast from *[N]T to [*]T
39 err = linux.epoll_wait(i32(epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
39 err = linux.epoll_wait(@intCast(i32, epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
4040}
std/os/linux/vdso.zig+2-2
......@@ -62,8 +62,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
6262
6363 var i: usize = 0;
6464 while (i < hashtab[1]) : (i += 1) {
65 if (0 == (u32(1) << u5(syms[i].st_info & 0xf) & OK_TYPES)) continue;
66 if (0 == (u32(1) << u5(syms[i].st_info >> 4) & OK_BINDS)) continue;
65 if (0 == (u32(1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue;
66 if (0 == (u32(1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue;
6767 if (0 == syms[i].st_shndx) continue;
6868 if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue;
6969 if (maybe_versym) |versym| {
std/os/time.zig+12-12
......@@ -14,12 +14,12 @@ pub const epoch = @import("epoch.zig");
1414pub fn sleep(seconds: usize, nanoseconds: usize) void {
1515 switch (builtin.os) {
1616 Os.linux, Os.macosx, Os.ios => {
17 posixSleep(u63(seconds), u63(nanoseconds));
17 posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds));
1818 },
1919 Os.windows => {
2020 const ns_per_ms = ns_per_s / ms_per_s;
2121 const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
22 windows.Sleep(windows.DWORD(milliseconds));
22 windows.Sleep(@intCast(windows.DWORD, milliseconds));
2323 },
2424 else => @compileError("Unsupported OS"),
2525 }
......@@ -83,8 +83,8 @@ fn milliTimestampDarwin() u64 {
8383 var tv: darwin.timeval = undefined;
8484 var err = darwin.gettimeofday(&tv, null);
8585 debug.assert(err == 0);
86 const sec_ms = u64(tv.tv_sec) * ms_per_s;
87 const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
86 const sec_ms = @intCast(u64, tv.tv_sec) * ms_per_s;
87 const usec_ms = @divFloor(@intCast(u64, tv.tv_usec), us_per_s / ms_per_s);
8888 return u64(sec_ms) + u64(usec_ms);
8989}
9090
......@@ -95,8 +95,8 @@ fn milliTimestampPosix() u64 {
9595 var ts: posix.timespec = undefined;
9696 const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
9797 debug.assert(err == 0);
98 const sec_ms = u64(ts.tv_sec) * ms_per_s;
99 const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s);
98 const sec_ms = @intCast(u64, ts.tv_sec) * ms_per_s;
99 const nsec_ms = @divFloor(@intCast(u64, ts.tv_nsec), ns_per_s / ms_per_s);
100100 return sec_ms + nsec_ms;
101101}
102102
......@@ -162,13 +162,13 @@ pub const Timer = struct {
162162 var freq: i64 = undefined;
163163 var err = windows.QueryPerformanceFrequency(&freq);
164164 if (err == windows.FALSE) return error.TimerUnsupported;
165 self.frequency = u64(freq);
165 self.frequency = @intCast(u64, freq);
166166 self.resolution = @divFloor(ns_per_s, self.frequency);
167167
168168 var start_time: i64 = undefined;
169169 err = windows.QueryPerformanceCounter(&start_time);
170170 debug.assert(err != windows.FALSE);
171 self.start_time = u64(start_time);
171 self.start_time = @intCast(u64, start_time);
172172 },
173173 Os.linux => {
174174 //On Linux, seccomp can do arbitrary things to our ability to call
......@@ -184,12 +184,12 @@ pub const Timer = struct {
184184 posix.EINVAL => return error.TimerUnsupported,
185185 else => return std.os.unexpectedErrorPosix(errno),
186186 }
187 self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
187 self.resolution = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
188188
189189 result = posix.clock_gettime(monotonic_clock_id, &ts);
190190 errno = posix.getErrno(result);
191191 if (errno != 0) return std.os.unexpectedErrorPosix(errno);
192 self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
192 self.start_time = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
193193 },
194194 Os.macosx, Os.ios => {
195195 darwin.mach_timebase_info(&self.frequency);
......@@ -236,7 +236,7 @@ pub const Timer = struct {
236236 var result: i64 = undefined;
237237 var err = windows.QueryPerformanceCounter(&result);
238238 debug.assert(err != windows.FALSE);
239 return u64(result);
239 return @intCast(u64, result);
240240 }
241241
242242 fn clockDarwin() u64 {
......@@ -247,7 +247,7 @@ pub const Timer = struct {
247247 var ts: posix.timespec = undefined;
248248 var result = posix.clock_gettime(monotonic_clock_id, &ts);
249249 debug.assert(posix.getErrno(result) == 0);
250 return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
250 return @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
251251 }
252252};
253253
std/os/windows/util.zig+7-2
......@@ -42,7 +42,7 @@ pub const WriteError = error{
4242};
4343
4444pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void {
45 if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
45 if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), @intCast(u32, bytes.len), null, null) == 0) {
4646 const err = windows.GetLastError();
4747 return switch (err) {
4848 windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources,
......@@ -68,7 +68,12 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
6868 const size = @sizeOf(windows.FILE_NAME_INFO);
6969 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH);
7070
71 if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
71 if (windows.GetFileInformationByHandleEx(
72 handle,
73 windows.FileNameInfo,
74 @ptrCast(*c_void, &name_info_bytes[0]),
75 @intCast(u32, name_info_bytes.len),
76 ) == 0) {
7277 return true;
7378 }
7479
std/rand/index.zig+8-8
......@@ -55,16 +55,16 @@ pub const Random = struct {
5555 if (T.is_signed) {
5656 const uint = @IntType(false, T.bit_count);
5757 if (start >= 0 and end >= 0) {
58 return T(r.range(uint, uint(start), uint(end)));
58 return @intCast(T, r.range(uint, @intCast(uint, start), @intCast(uint, end)));
5959 } else if (start < 0 and end < 0) {
6060 // Can't overflow because the range is over signed ints
6161 return math.negateCast(r.range(uint, math.absCast(end), math.absCast(start)) + 1) catch unreachable;
6262 } else if (start < 0 and end >= 0) {
63 const end_uint = uint(end);
63 const end_uint = @intCast(uint, end);
6464 const total_range = math.absCast(start) + end_uint;
6565 const value = r.range(uint, 0, total_range);
6666 const result = if (value < end_uint) x: {
67 break :x T(value);
67 break :x @intCast(T, value);
6868 } else if (value == end_uint) x: {
6969 break :x start;
7070 } else x: {
......@@ -213,9 +213,9 @@ pub const Pcg = struct {
213213 self.s = l *% default_multiplier +% (self.i | 1);
214214
215215 const xor_s = @truncate(u32, ((l >> 18) ^ l) >> 27);
216 const rot = u32(l >> 59);
216 const rot = @intCast(u32, l >> 59);
217217
218 return (xor_s >> u5(rot)) | (xor_s << u5((0 -% rot) & 31));
218 return (xor_s >> @intCast(u5, rot)) | (xor_s << @intCast(u5, (0 -% rot) & 31));
219219 }
220220
221221 fn seed(self: *Pcg, init_s: u64) void {
......@@ -322,7 +322,7 @@ pub const Xoroshiro128 = struct {
322322 inline for (table) |entry| {
323323 var b: usize = 0;
324324 while (b < 64) : (b += 1) {
325 if ((entry & (u64(1) << u6(b))) != 0) {
325 if ((entry & (u64(1) << @intCast(u6, b))) != 0) {
326326 s0 ^= self.s[0];
327327 s1 ^= self.s[1];
328328 }
......@@ -667,13 +667,13 @@ test "Random range" {
667667}
668668
669669fn testRange(r: *Random, start: i32, end: i32) void {
670 const count = usize(end - start);
670 const count = @intCast(usize, end - start);
671671 var values_buffer = []bool{false} ** 20;
672672 const values = values_buffer[0..count];
673673 var i: usize = 0;
674674 while (i < count) {
675675 const value = r.range(i32, start, end);
676 const index = usize(value - start);
676 const index = @intCast(usize, value - start);
677677 if (!values[index]) {
678678 i += 1;
679679 values[index] = true;
std/segmented_list.zig+6-6
......@@ -104,7 +104,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
104104 }
105105
106106 pub fn deinit(self: *Self) void {
107 self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
107 self.freeShelves(@intCast(ShelfIndex, self.dynamic_segments.len), 0);
108108 self.allocator.free(self.dynamic_segments);
109109 self.* = undefined;
110110 }
......@@ -158,7 +158,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
158158 /// Only grows capacity, or retains current capacity
159159 pub fn growCapacity(self: *Self, new_capacity: usize) !void {
160160 const new_cap_shelf_count = shelfCount(new_capacity);
161 const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
161 const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
162162 if (new_cap_shelf_count > old_shelf_count) {
163163 self.dynamic_segments = try self.allocator.realloc([*]T, self.dynamic_segments, new_cap_shelf_count);
164164 var i = old_shelf_count;
......@@ -175,7 +175,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
175175 /// Only shrinks capacity or retains current capacity
176176 pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
177177 if (new_capacity <= prealloc_item_count) {
178 const len = ShelfIndex(self.dynamic_segments.len);
178 const len = @intCast(ShelfIndex, self.dynamic_segments.len);
179179 self.freeShelves(len, 0);
180180 self.allocator.free(self.dynamic_segments);
181181 self.dynamic_segments = [][*]T{};
......@@ -183,7 +183,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
183183 }
184184
185185 const new_cap_shelf_count = shelfCount(new_capacity);
186 const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
186 const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
187187 assert(new_cap_shelf_count <= old_shelf_count);
188188 if (new_cap_shelf_count == old_shelf_count) {
189189 return;
......@@ -338,7 +338,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
338338 {
339339 var i: usize = 0;
340340 while (i < 100) : (i += 1) {
341 try list.push(i32(i + 1));
341 try list.push(@intCast(i32, i + 1));
342342 assert(list.len == i + 1);
343343 }
344344 }
......@@ -346,7 +346,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
346346 {
347347 var i: usize = 0;
348348 while (i < 100) : (i += 1) {
349 assert(list.at(i).* == i32(i + 1));
349 assert(list.at(i).* == @intCast(i32, i + 1));
350350 }
351351 }
352352
std/special/bootstrap.zig+1-1
......@@ -80,7 +80,7 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
8080 var env_count: usize = 0;
8181 while (c_envp[env_count] != null) : (env_count += 1) {}
8282 const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
83 return callMainWithArgs(usize(c_argc), c_argv, envp);
83 return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
8484}
8585
8686fn callMain() u8 {
std/special/builtin.zig+15-15
......@@ -135,9 +135,9 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
135135 const mask = if (T == f32) 0xff else 0x7ff;
136136 var ux = @bitCast(uint, x);
137137 var uy = @bitCast(uint, y);
138 var ex = i32((ux >> digits) & mask);
139 var ey = i32((uy >> digits) & mask);
140 const sx = if (T == f32) u32(ux & 0x80000000) else i32(ux >> bits_minus_1);
138 var ex = @intCast(i32, (ux >> digits) & mask);
139 var ey = @intCast(i32, (uy >> digits) & mask);
140 const sx = if (T == f32) @intCast(u32, ux & 0x80000000) else @intCast(i32, ux >> bits_minus_1);
141141 var i: uint = undefined;
142142
143143 if (uy << 1 == 0 or isNan(uint, uy) or ex == mask)
......@@ -156,7 +156,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
156156 ex -= 1;
157157 i <<= 1;
158158 }) {}
159 ux <<= log2uint(@bitCast(u32, -ex + 1));
159 ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1));
160160 } else {
161161 ux &= @maxValue(uint) >> exp_bits;
162162 ux |= 1 << digits;
......@@ -167,7 +167,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
167167 ey -= 1;
168168 i <<= 1;
169169 }) {}
170 uy <<= log2uint(@bitCast(u32, -ey + 1));
170 uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1));
171171 } else {
172172 uy &= @maxValue(uint) >> exp_bits;
173173 uy |= 1 << digits;
......@@ -199,12 +199,12 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
199199 ux -%= 1 << digits;
200200 ux |= uint(@bitCast(u32, ex)) << digits;
201201 } else {
202 ux >>= log2uint(@bitCast(u32, -ex + 1));
202 ux >>= @intCast(log2uint, @bitCast(u32, -ex + 1));
203203 }
204204 if (T == f32) {
205205 ux |= sx;
206206 } else {
207 ux |= uint(sx) << bits_minus_1;
207 ux |= @intCast(uint, sx) << bits_minus_1;
208208 }
209209 return @bitCast(T, ux);
210210}
......@@ -227,8 +227,8 @@ export fn sqrt(x: f64) f64 {
227227 const sign: u32 = 0x80000000;
228228 const u = @bitCast(u64, x);
229229
230 var ix0 = u32(u >> 32);
231 var ix1 = u32(u & 0xFFFFFFFF);
230 var ix0 = @intCast(u32, u >> 32);
231 var ix1 = @intCast(u32, u & 0xFFFFFFFF);
232232
233233 // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan
234234 if (ix0 & 0x7FF00000 == 0x7FF00000) {
......@@ -245,7 +245,7 @@ export fn sqrt(x: f64) f64 {
245245 }
246246
247247 // normalize x
248 var m = i32(ix0 >> 20);
248 var m = @intCast(i32, ix0 >> 20);
249249 if (m == 0) {
250250 // subnormal
251251 while (ix0 == 0) {
......@@ -259,9 +259,9 @@ export fn sqrt(x: f64) f64 {
259259 while (ix0 & 0x00100000 == 0) : (i += 1) {
260260 ix0 <<= 1;
261261 }
262 m -= i32(i) - 1;
263 ix0 |= ix1 >> u5(32 - i);
264 ix1 <<= u5(i);
262 m -= @intCast(i32, i) - 1;
263 ix0 |= ix1 >> @intCast(u5, 32 - i);
264 ix1 <<= @intCast(u5, i);
265265 }
266266
267267 // unbias exponent
......@@ -345,10 +345,10 @@ export fn sqrt(x: f64) f64 {
345345
346346 // NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same
347347 // behaviour at least.
348 var iix0 = i32(ix0);
348 var iix0 = @intCast(i32, ix0);
349349 iix0 = iix0 +% (m << 20);
350350
351 const uz = (u64(iix0) << 32) | ix1;
351 const uz = (@intCast(u64, iix0) << 32) | ix1;
352352 return @bitCast(f64, uz);
353353}
354354
std/special/compiler_rt/divti3.zig+1-1
......@@ -13,7 +13,7 @@ pub extern fn __divti3(a: i128, b: i128) i128 {
1313
1414 const r = udivmod(u128, @bitCast(u128, an), @bitCast(u128, bn), null);
1515 const s = s_a ^ s_b;
16 return (i128(r) ^ s) -% s;
16 return (@bitCast(i128, r) ^ s) -% s;
1717}
1818
1919pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {
std/special/compiler_rt/fixuint.zig+4-4
......@@ -32,14 +32,14 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
3232 const aAbs: rep_t = aRep & absMask;
3333
3434 const sign = if ((aRep & signBit) != 0) i32(-1) else i32(1);
35 const exponent = i32(aAbs >> significandBits) - exponentBias;
35 const exponent = @intCast(i32, aAbs >> significandBits) - exponentBias;
3636 const significand: rep_t = (aAbs & significandMask) | implicitBit;
3737
3838 // If either the value or the exponent is negative, the result is zero.
3939 if (sign == -1 or exponent < 0) return 0;
4040
4141 // If the value is too large for the integer type, saturate.
42 if (c_uint(exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
42 if (@intCast(c_uint, exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
4343
4444 // If 0 <= exponent < significandBits, right shift to get the result.
4545 // Otherwise, shift left.
......@@ -47,11 +47,11 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
4747 // TODO this is a workaround for the mysterious "integer cast truncated bits"
4848 // happening on the next line
4949 @setRuntimeSafety(false);
50 return fixuint_t(significand >> Log2Int(rep_t)(significandBits - exponent));
50 return @intCast(fixuint_t, significand >> @intCast(Log2Int(rep_t), significandBits - exponent));
5151 } else {
5252 // TODO this is a workaround for the mysterious "integer cast truncated bits"
5353 // happening on the next line
5454 @setRuntimeSafety(false);
55 return fixuint_t(significand) << Log2Int(fixuint_t)(exponent - significandBits);
55 return @intCast(fixuint_t, significand) << @intCast(Log2Int(fixuint_t), exponent - significandBits);
5656 }
5757}
std/special/compiler_rt/index.zig+6-6
......@@ -292,7 +292,7 @@ extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 {
292292 @setRuntimeSafety(is_test);
293293
294294 const d = __udivsi3(a, b);
295 rem.* = u32(i32(a) -% (i32(d) * i32(b)));
295 rem.* = @bitCast(u32, @bitCast(i32, a) -% (@bitCast(i32, d) * @bitCast(i32, b)));
296296 return d;
297297}
298298
......@@ -316,12 +316,12 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
316316 sr += 1;
317317 // 1 <= sr <= n_uword_bits - 1
318318 // Not a special case
319 var q: u32 = n << u5(n_uword_bits - sr);
320 var r: u32 = n >> u5(sr);
319 var q: u32 = n << @intCast(u5, n_uword_bits - sr);
320 var r: u32 = n >> @intCast(u5, sr);
321321 var carry: u32 = 0;
322322 while (sr > 0) : (sr -= 1) {
323323 // r:q = ((r:q) << 1) | carry
324 r = (r << 1) | (q >> u5(n_uword_bits - 1));
324 r = (r << 1) | (q >> @intCast(u5, n_uword_bits - 1));
325325 q = (q << 1) | carry;
326326 // carry = 0;
327327 // if (r.all >= d.all)
......@@ -329,8 +329,8 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
329329 // r.all -= d.all;
330330 // carry = 1;
331331 // }
332 const s = i32(d -% r -% 1) >> u5(n_uword_bits - 1);
333 carry = u32(s & 1);
332 const s = @intCast(i32, d -% r -% 1) >> @intCast(u5, n_uword_bits - 1);
333 carry = @intCast(u32, s & 1);
334334 r -= d & @bitCast(u32, s);
335335 }
336336 q = (q << 1) | carry;
std/special/compiler_rt/udivmod.zig+17-17
......@@ -71,7 +71,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
7171 r[high] = n[high] & (d[high] - 1);
7272 rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
7373 }
74 return n[high] >> Log2SingleInt(@ctz(d[high]));
74 return n[high] >> @intCast(Log2SingleInt, @ctz(d[high]));
7575 }
7676 // K K
7777 // ---
......@@ -88,10 +88,10 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
8888 // 1 <= sr <= SingleInt.bit_count - 1
8989 // q.all = a << (DoubleInt.bit_count - sr);
9090 q[low] = 0;
91 q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
91 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
9292 // r.all = a >> sr;
93 r[high] = n[high] >> Log2SingleInt(sr);
94 r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
93 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
94 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
9595 } else {
9696 // d[low] != 0
9797 if (d[high] == 0) {
......@@ -107,8 +107,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
107107 return a;
108108 }
109109 sr = @ctz(d[low]);
110 q[high] = n[high] >> Log2SingleInt(sr);
111 q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
110 q[high] = n[high] >> @intCast(Log2SingleInt, sr);
111 q[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
112112 return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
113113 }
114114 // K X
......@@ -126,15 +126,15 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
126126 } else if (sr < SingleInt.bit_count) {
127127 // 2 <= sr <= SingleInt.bit_count - 1
128128 q[low] = 0;
129 q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
130 r[high] = n[high] >> Log2SingleInt(sr);
131 r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
129 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
130 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
131 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
132132 } else {
133133 // SingleInt.bit_count + 1 <= sr <= DoubleInt.bit_count - 1
134 q[low] = n[low] << Log2SingleInt(DoubleInt.bit_count - sr);
135 q[high] = (n[high] << Log2SingleInt(DoubleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr - SingleInt.bit_count));
134 q[low] = n[low] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr);
135 q[high] = (n[high] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count));
136136 r[high] = 0;
137 r[low] = n[high] >> Log2SingleInt(sr - SingleInt.bit_count);
137 r[low] = n[high] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count);
138138 }
139139 } else {
140140 // K X
......@@ -158,9 +158,9 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
158158 r[high] = 0;
159159 r[low] = n[high];
160160 } else {
161 r[high] = n[high] >> Log2SingleInt(sr);
162 r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
163 q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
161 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
162 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
163 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
164164 }
165165 }
166166 }
......@@ -184,8 +184,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
184184 // carry = 1;
185185 // }
186186 r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
187 const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
188 carry = u32(s & 1);
187 const s: SignedDoubleInt = @intCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
188 carry = @intCast(u32, s & 1);
189189 r_all -= b & @bitCast(DoubleInt, s);
190190 r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
191191 }
std/unicode.zig+10-10
......@@ -35,22 +35,22 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
3535 // - Increasing the initial shift by 6 each time
3636 // - Each time after the first shorten the shifted
3737 // value to a max of 0b111111 (63)
38 1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range
38 1 => out[0] = @intCast(u8, c), // Can just do 0 + codepoint for initial range
3939 2 => {
40 out[0] = u8(0b11000000 | (c >> 6));
41 out[1] = u8(0b10000000 | (c & 0b111111));
40 out[0] = @intCast(u8, 0b11000000 | (c >> 6));
41 out[1] = @intCast(u8, 0b10000000 | (c & 0b111111));
4242 },
4343 3 => {
4444 if (0xd800 <= c and c <= 0xdfff) return error.Utf8CannotEncodeSurrogateHalf;
45 out[0] = u8(0b11100000 | (c >> 12));
46 out[1] = u8(0b10000000 | ((c >> 6) & 0b111111));
47 out[2] = u8(0b10000000 | (c & 0b111111));
45 out[0] = @intCast(u8, 0b11100000 | (c >> 12));
46 out[1] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
47 out[2] = @intCast(u8, 0b10000000 | (c & 0b111111));
4848 },
4949 4 => {
50 out[0] = u8(0b11110000 | (c >> 18));
51 out[1] = u8(0b10000000 | ((c >> 12) & 0b111111));
52 out[2] = u8(0b10000000 | ((c >> 6) & 0b111111));
53 out[3] = u8(0b10000000 | (c & 0b111111));
50 out[0] = @intCast(u8, 0b11110000 | (c >> 18));
51 out[1] = @intCast(u8, 0b10000000 | ((c >> 12) & 0b111111));
52 out[2] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
53 out[3] = @intCast(u8, 0b10000000 | (c & 0b111111));
5454 },
5555 else => unreachable,
5656 }
std/zig/tokenizer.zig+1-1
......@@ -1128,7 +1128,7 @@ pub const Tokenizer = struct {
11281128 // check utf8-encoded character.
11291129 const length = std.unicode.utf8ByteSequenceLength(c0) catch return 1;
11301130 if (self.index + length > self.buffer.len) {
1131 return u3(self.buffer.len - self.index);
1131 return @intCast(u3, self.buffer.len - self.index);
11321132 }
11331133 const bytes = self.buffer[self.index .. self.index + length];
11341134 switch (length) {
test/cases/cast.zig+17-1
......@@ -343,7 +343,7 @@ fn testPeerErrorAndArray2(x: u8) error![]const u8 {
343343test "explicit cast float number literal to integer if no fraction component" {
344344 const x = i32(1e4);
345345 assert(x == 10000);
346 const y = i32(f32(1e4));
346 const y = @floatToInt(i32, f32(1e4));
347347 assert(y == 10000);
348348}
349349
......@@ -398,3 +398,19 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
398398 const x: [*]const ?[*]const u8 = &window_name;
399399 assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name"));
400400}
401
402test "@intCast comptime_int" {
403 const result = @intCast(i32, 1234);
404 assert(@typeOf(result) == i32);
405 assert(result == 1234);
406}
407
408test "@floatCast comptime_int and comptime_float" {
409 const result = @floatCast(f32, 1234);
410 assert(@typeOf(result) == f32);
411 assert(result == 1234.0);
412
413 const result2 = @floatCast(f32, 1234.0);
414 assert(@typeOf(result) == f32);
415 assert(result == 1234.0);
416}
test/cases/enum.zig+1-1
......@@ -99,7 +99,7 @@ test "int to enum" {
9999 testIntToEnumEval(3);
100100}
101101fn testIntToEnumEval(x: i32) void {
102 assert(IntToEnumNumber(u3(x)) == IntToEnumNumber.Three);
102 assert(IntToEnumNumber(@intCast(u3, x)) == IntToEnumNumber.Three);
103103}
104104const IntToEnumNumber = enum {
105105 Zero,
test/cases/eval.zig+4-4
......@@ -5,7 +5,7 @@ const builtin = @import("builtin");
55test "compile time recursion" {
66 assert(some_data.len == 21);
77}
8var some_data: [usize(fibonacci(7))]u8 = undefined;
8var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
99fn fibonacci(x: i32) i32 {
1010 if (x <= 1) return 1;
1111 return fibonacci(x - 1) + fibonacci(x - 2);
......@@ -356,7 +356,7 @@ const global_array = x: {
356356test "compile-time downcast when the bits fit" {
357357 comptime {
358358 const spartan_count: u16 = 255;
359 const byte = u8(spartan_count);
359 const byte = @intCast(u8, spartan_count);
360360 assert(byte == 255);
361361 }
362362}
......@@ -440,7 +440,7 @@ test "binary math operator in partially inlined function" {
440440 var b: [16]u8 = undefined;
441441
442442 for (b) |*r, i|
443 r.* = u8(i + 1);
443 r.* = @intCast(u8, i + 1);
444444
445445 copyWithPartialInline(s[0..], b[0..]);
446446 assert(s[0] == 0x1020304);
......@@ -480,7 +480,7 @@ fn generateTable(comptime T: type) [1010]T {
480480 var res: [1010]T = undefined;
481481 var i: usize = 0;
482482 while (i < 1010) : (i += 1) {
483 res[i] = T(i);
483 res[i] = @intCast(T, i);
484484 }
485485 return res;
486486}
test/cases/fn.zig+1-1
......@@ -80,7 +80,7 @@ test "function pointers" {
8080 fn4,
8181 };
8282 for (fns) |f, i| {
83 assert(f() == u32(i) + 5);
83 assert(f() == @intCast(u32, i) + 5);
8484 }
8585}
8686fn fn1() u32 {
test/cases/for.zig+2-2
......@@ -46,7 +46,7 @@ test "basic for loop" {
4646 buf_index += 1;
4747 }
4848 for (array) |item, index| {
49 buffer[buf_index] = u8(index);
49 buffer[buf_index] = @intCast(u8, index);
5050 buf_index += 1;
5151 }
5252 const unknown_size: []const u8 = array;
......@@ -55,7 +55,7 @@ test "basic for loop" {
5555 buf_index += 1;
5656 }
5757 for (unknown_size) |item, index| {
58 buffer[buf_index] = u8(index);
58 buffer[buf_index] = @intCast(u8, index);
5959 buf_index += 1;
6060 }
6161
test/cases/struct.zig+4-4
......@@ -365,14 +365,14 @@ test "runtime struct initialization of bitfield" {
365365 .y = x1,
366366 };
367367 const s2 = Nibbles{
368 .x = u4(x2),
369 .y = u4(x2),
368 .x = @intCast(u4, x2),
369 .y = @intCast(u4, x2),
370370 };
371371
372372 assert(s1.x == x1);
373373 assert(s1.y == x1);
374 assert(s2.x == u4(x2));
375 assert(s2.y == u4(x2));
374 assert(s2.x == @intCast(u4, x2));
375 assert(s2.y == @intCast(u4, x2));
376376}
377377
378378var x1 = u4(1);
test/compare_output.zig+3-3
......@@ -299,7 +299,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
299299 \\export fn main() c_int {
300300 \\ var array = []u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
301301 \\
302 \\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), c_ulong(array.len), @sizeOf(i32), compare_fn);
302 \\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), @intCast(c_ulong, array.len), @sizeOf(i32), compare_fn);
303303 \\
304304 \\ for (array) |item, i| {
305305 \\ if (item != i) {
......@@ -331,8 +331,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
331331 \\ }
332332 \\ const small: f32 = 3.25;
333333 \\ const x: f64 = small;
334 \\ const y = i32(x);
335 \\ const z = f64(y);
334 \\ const y = @floatToInt(i32, x);
335 \\ const z = @intToFloat(f64, y);
336336 \\ _ = c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4));
337337 \\ return 0;
338338 \\}
test/compile_errors.zig+4-4
......@@ -2931,10 +2931,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29312931 "cast negative value to unsigned integer",
29322932 \\comptime {
29332933 \\ const value: i32 = -1;
2934 \\ const unsigned = u32(value);
2934 \\ const unsigned = @intCast(u32, value);
29352935 \\}
29362936 ,
2937 ".tmp_source.zig:3:25: error: attempt to cast negative value to unsigned integer",
2937 ".tmp_source.zig:3:22: error: attempt to cast negative value to unsigned integer",
29382938 );
29392939
29402940 cases.add(
......@@ -2963,10 +2963,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
29632963 "compile-time integer cast truncates bits",
29642964 \\comptime {
29652965 \\ const spartan_count: u16 = 300;
2966 \\ const byte = u8(spartan_count);
2966 \\ const byte = @intCast(u8, spartan_count);
29672967 \\}
29682968 ,
2969 ".tmp_source.zig:3:20: error: cast from 'u16' to 'u8' truncates bits",
2969 ".tmp_source.zig:3:18: error: cast from 'u16' to 'u8' truncates bits",
29702970 );
29712971
29722972 cases.add(
test/runtime_safety.zig+2-2
......@@ -188,7 +188,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
188188 \\ if (x == 0) return error.Whatever;
189189 \\}
190190 \\fn shorten_cast(x: i32) i8 {
191 \\ return i8(x);
191 \\ return @intCast(i8, x);
192192 \\}
193193 );
194194
......@@ -201,7 +201,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
201201 \\ if (x == 0) return error.Whatever;
202202 \\}
203203 \\fn unsigned_cast(x: i32) u32 {
204 \\ return u32(x);
204 \\ return @intCast(u32, x);
205205 \\}
206206 );
207207