| author | |
| committer | |
| log | 431fda414189e012252614c7cf3702b46b305e35 |
| tree | 929ff496c8b03785eb425329318f5b38170d6962 |
| parent | e5956f23ca702b79a3a4b0f0440a2fe88e0231e5 |
| parent | 74ccf56a4b1da78b6cd6b0ac34dd6ded1e15b155 |
| signature |
Remove number casting syntax90 files changed, 820 insertions(+), 435 deletions(-)
doc/langref.html.in+6-6| ... | ... | @@ -1355,7 +1355,7 @@ var some_integers: [100]i32 = undefined; |
| 1355 | 1355 | |
| 1356 | 1356 | test "modify an array" { |
| 1357 | 1357 | for (some_integers) |*item, i| { |
| 1358 | item.* = i32(i); | |
| 1358 | item.* = @intCast(i32, i); | |
| 1359 | 1359 | } |
| 1360 | 1360 | assert(some_integers[10] == 10); |
| 1361 | 1361 | assert(some_integers[99] == 99); |
| ... | ... | @@ -1397,8 +1397,8 @@ var fancy_array = init: { |
| 1397 | 1397 | var initial_value: [10]Point = undefined; |
| 1398 | 1398 | for (initial_value) |*pt, i| { |
| 1399 | 1399 | pt.* = Point{ |
| 1400 | .x = i32(i), | |
| 1401 | .y = i32(i) * 2, | |
| 1400 | .x = @intCast(i32, i), | |
| 1401 | .y = @intCast(i32, i) * 2, | |
| 1402 | 1402 | }; |
| 1403 | 1403 | } |
| 1404 | 1404 | break :init initial_value; |
| ... | ... | @@ -2410,7 +2410,7 @@ test "for basics" { |
| 2410 | 2410 | var sum2: i32 = 0; |
| 2411 | 2411 | for (items) |value, i| { |
| 2412 | 2412 | assert(@typeOf(i) == usize); |
| 2413 | sum2 += i32(i); | |
| 2413 | sum2 += @intCast(i32, i); | |
| 2414 | 2414 | } |
| 2415 | 2415 | assert(sum2 == 10); |
| 2416 | 2416 | } |
| ... | ... | @@ -5730,7 +5730,7 @@ comptime { |
| 5730 | 5730 | {#code_begin|test_err|attempt to cast negative value to unsigned integer#} |
| 5731 | 5731 | comptime { |
| 5732 | 5732 | const value: i32 = -1; |
| 5733 | const unsigned = u32(value); | |
| 5733 | const unsigned = @intCast(u32, value); | |
| 5734 | 5734 | } |
| 5735 | 5735 | {#code_end#} |
| 5736 | 5736 | <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 { |
| 5744 | 5744 | {#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#} |
| 5745 | 5745 | comptime { |
| 5746 | 5746 | const spartan_count: u16 = 300; |
| 5747 | const byte = u8(spartan_count); | |
| 5747 | const byte = @intCast(u8, spartan_count); | |
| 5748 | 5748 | } |
| 5749 | 5749 | {#code_end#} |
| 5750 | 5750 | <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({ |
| 8 | 8 | const msg = c"Hello, world!\n"; |
| 9 | 9 | |
| 10 | 10 | export 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; | |
| 12 | 12 | |
| 13 | 13 | return 0; |
| 14 | 14 | } |
src/all_types.hpp+36| ... | ... | @@ -1357,6 +1357,10 @@ enum BuiltinFnId { |
| 1357 | 1357 | BuiltinFnIdMod, |
| 1358 | 1358 | BuiltinFnIdSqrt, |
| 1359 | 1359 | BuiltinFnIdTruncate, |
| 1360 | BuiltinFnIdIntCast, | |
| 1361 | BuiltinFnIdFloatCast, | |
| 1362 | BuiltinFnIdIntToFloat, | |
| 1363 | BuiltinFnIdFloatToInt, | |
| 1360 | 1364 | BuiltinFnIdIntType, |
| 1361 | 1365 | BuiltinFnIdSetCold, |
| 1362 | 1366 | BuiltinFnIdSetRuntimeSafety, |
| ... | ... | @@ -2040,6 +2044,10 @@ enum IrInstructionId { |
| 2040 | 2044 | IrInstructionIdCmpxchg, |
| 2041 | 2045 | IrInstructionIdFence, |
| 2042 | 2046 | IrInstructionIdTruncate, |
| 2047 | IrInstructionIdIntCast, | |
| 2048 | IrInstructionIdFloatCast, | |
| 2049 | IrInstructionIdIntToFloat, | |
| 2050 | IrInstructionIdFloatToInt, | |
| 2043 | 2051 | IrInstructionIdIntType, |
| 2044 | 2052 | IrInstructionIdBoolNot, |
| 2045 | 2053 | IrInstructionIdMemset, |
| ... | ... | @@ -2632,6 +2640,34 @@ struct IrInstructionTruncate { |
| 2632 | 2640 | IrInstruction *target; |
| 2633 | 2641 | }; |
| 2634 | 2642 | |
| 2643 | struct IrInstructionIntCast { | |
| 2644 | IrInstruction base; | |
| 2645 | ||
| 2646 | IrInstruction *dest_type; | |
| 2647 | IrInstruction *target; | |
| 2648 | }; | |
| 2649 | ||
| 2650 | struct IrInstructionFloatCast { | |
| 2651 | IrInstruction base; | |
| 2652 | ||
| 2653 | IrInstruction *dest_type; | |
| 2654 | IrInstruction *target; | |
| 2655 | }; | |
| 2656 | ||
| 2657 | struct IrInstructionIntToFloat { | |
| 2658 | IrInstruction base; | |
| 2659 | ||
| 2660 | IrInstruction *dest_type; | |
| 2661 | IrInstruction *target; | |
| 2662 | }; | |
| 2663 | ||
| 2664 | struct IrInstructionFloatToInt { | |
| 2665 | IrInstruction base; | |
| 2666 | ||
| 2667 | IrInstruction *dest_type; | |
| 2668 | IrInstruction *target; | |
| 2669 | }; | |
| 2670 | ||
| 2635 | 2671 | struct IrInstructionIntType { |
| 2636 | 2672 | IrInstruction base; |
| 2637 | 2673 |
src/codegen.cpp+8| ... | ... | @@ -4722,6 +4722,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, |
| 4722 | 4722 | case IrInstructionIdPromiseResultType: |
| 4723 | 4723 | case IrInstructionIdAwaitBookkeeping: |
| 4724 | 4724 | case IrInstructionIdAddImplicitReturnType: |
| 4725 | case IrInstructionIdIntCast: | |
| 4726 | case IrInstructionIdFloatCast: | |
| 4727 | case IrInstructionIdIntToFloat: | |
| 4728 | case IrInstructionIdFloatToInt: | |
| 4725 | 4729 | zig_unreachable(); |
| 4726 | 4730 | |
| 4727 | 4731 | case IrInstructionIdReturn: |
| ... | ... | @@ -6310,6 +6314,10 @@ static void define_builtin_fns(CodeGen *g) { |
| 6310 | 6314 | create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6); |
| 6311 | 6315 | create_builtin_fn(g, BuiltinFnIdFence, "fence", 1); |
| 6312 | 6316 | 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); | |
| 6313 | 6321 | create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1); |
| 6314 | 6322 | create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX); |
| 6315 | 6323 | 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 *) { |
| 460 | 460 | return IrInstructionIdTruncate; |
| 461 | 461 | } |
| 462 | 462 | |
| 463 | static constexpr IrInstructionId ir_instruction_id(IrInstructionIntCast *) { | |
| 464 | return IrInstructionIdIntCast; | |
| 465 | } | |
| 466 | ||
| 467 | static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatCast *) { | |
| 468 | return IrInstructionIdFloatCast; | |
| 469 | } | |
| 470 | ||
| 471 | static constexpr IrInstructionId ir_instruction_id(IrInstructionIntToFloat *) { | |
| 472 | return IrInstructionIdIntToFloat; | |
| 473 | } | |
| 474 | ||
| 475 | static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatToInt *) { | |
| 476 | return IrInstructionIdFloatToInt; | |
| 477 | } | |
| 478 | ||
| 463 | 479 | static constexpr IrInstructionId ir_instruction_id(IrInstructionIntType *) { |
| 464 | 480 | return IrInstructionIdIntType; |
| 465 | 481 | } |
| ... | ... | @@ -1899,10 +1915,48 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s |
| 1899 | 1915 | return &instruction->base; |
| 1900 | 1916 | } |
| 1901 | 1917 | |
| 1902 | static 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; | |
| 1918 | static 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 | ||
| 1929 | static 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 | ||
| 1940 | static 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 | ||
| 1951 | static 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; | |
| 1906 | 1960 | } |
| 1907 | 1961 | |
| 1908 | 1962 | static 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 |
| 3957 | 4011 | IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value); |
| 3958 | 4012 | return ir_lval_wrap(irb, scope, truncate, lval); |
| 3959 | 4013 | } |
| 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 | } | |
| 3960 | 4074 | case BuiltinFnIdIntType: |
| 3961 | 4075 | { |
| 3962 | 4076 | 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 |
| 9948 | 10062 | return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false); |
| 9949 | 10063 | } |
| 9950 | 10064 | |
| 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) | |
| 9956 | 10070 | { |
| 9957 | 10071 | return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type); |
| 9958 | 10072 | } |
| 9959 | 10073 | |
| 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) | |
| 9963 | 10078 | { |
| 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); | |
| 9965 | 10080 | } |
| 9966 | 10081 | |
| 9967 | // explicit cast from int to float | |
| 10082 | // explicit float widening conversion | |
| 9968 | 10083 | 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) | |
| 9970 | 10086 | { |
| 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); | |
| 9972 | 10088 | } |
| 9973 | 10089 | |
| 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) | |
| 9977 | 10094 | { |
| 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); | |
| 9979 | 10096 | } |
| 9980 | 10097 | |
| 9981 | 10098 | // explicit cast from [N]T to []const T |
| ... | ... | @@ -17365,7 +17482,126 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc |
| 17365 | 17482 | return dest_type; |
| 17366 | 17483 | } |
| 17367 | 17484 | |
| 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 | ||
| 17491 | static 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 | ||
| 17532 | static 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 | ||
| 17580 | static 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 | ||
| 17594 | static 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); | |
| 17369 | 17605 | return dest_type; |
| 17370 | 17606 | } |
| 17371 | 17607 | |
| ... | ... | @@ -19899,6 +20135,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi |
| 19899 | 20135 | return ir_analyze_instruction_fence(ira, (IrInstructionFence *)instruction); |
| 19900 | 20136 | case IrInstructionIdTruncate: |
| 19901 | 20137 | 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); | |
| 19902 | 20146 | case IrInstructionIdIntType: |
| 19903 | 20147 | return ir_analyze_instruction_int_type(ira, (IrInstructionIntType *)instruction); |
| 19904 | 20148 | case IrInstructionIdBoolNot: |
| ... | ... | @@ -20242,6 +20486,10 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 20242 | 20486 | case IrInstructionIdPromiseResultType: |
| 20243 | 20487 | case IrInstructionIdSqrt: |
| 20244 | 20488 | case IrInstructionIdAtomicLoad: |
| 20489 | case IrInstructionIdIntCast: | |
| 20490 | case IrInstructionIdFloatCast: | |
| 20491 | case IrInstructionIdIntToFloat: | |
| 20492 | case IrInstructionIdFloatToInt: | |
| 20245 | 20493 | return false; |
| 20246 | 20494 | |
| 20247 | 20495 | case IrInstructionIdAsm: |
src/ir_print.cpp+44| ... | ... | @@ -648,6 +648,38 @@ static void ir_print_truncate(IrPrint *irp, IrInstructionTruncate *instruction) |
| 648 | 648 | fprintf(irp->f, ")"); |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | static 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 | ||
| 659 | static 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 | ||
| 667 | static 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 | ||
| 675 | static 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 | ||
| 651 | 683 | static void ir_print_int_type(IrPrint *irp, IrInstructionIntType *instruction) { |
| 652 | 684 | fprintf(irp->f, "@IntType("); |
| 653 | 685 | ir_print_other_instruction(irp, instruction->is_signed); |
| ... | ... | @@ -1417,6 +1449,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { |
| 1417 | 1449 | case IrInstructionIdTruncate: |
| 1418 | 1450 | ir_print_truncate(irp, (IrInstructionTruncate *)instruction); |
| 1419 | 1451 | 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; | |
| 1420 | 1464 | case IrInstructionIdIntType: |
| 1421 | 1465 | ir_print_int_type(irp, (IrInstructionIntType *)instruction); |
| 1422 | 1466 | break; |
src/main.cpp+1-1| ... | ... | @@ -34,7 +34,7 @@ static int usage(const char *arg0) { |
| 34 | 34 | " --assembly [source] add assembly file to build\n" |
| 35 | 35 | " --cache-dir [path] override the cache directory\n" |
| 36 | 36 | " --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" | |
| 38 | 38 | " --enable-timing-info print timing diagnostics\n" |
| 39 | 39 | " --libc-include-dir [path] directory where libc stdlib.h resides\n" |
| 40 | 40 | " --name [name] override output name\n" |
std/array_list.zig+4-4| ... | ... | @@ -185,23 +185,23 @@ test "basic ArrayList test" { |
| 185 | 185 | { |
| 186 | 186 | var i: usize = 0; |
| 187 | 187 | while (i < 10) : (i += 1) { |
| 188 | list.append(i32(i + 1)) catch unreachable; | |
| 188 | list.append(@intCast(i32, i + 1)) catch unreachable; | |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | { |
| 193 | 193 | var i: usize = 0; |
| 194 | 194 | while (i < 10) : (i += 1) { |
| 195 | assert(list.items[i] == i32(i + 1)); | |
| 195 | assert(list.items[i] == @intCast(i32, i + 1)); | |
| 196 | 196 | } |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | for (list.toSlice()) |v, i| { |
| 200 | assert(v == i32(i + 1)); | |
| 200 | assert(v == @intCast(i32, i + 1)); | |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | for (list.toSliceConst()) |v, i| { |
| 204 | assert(v == i32(i + 1)); | |
| 204 | assert(v == @intCast(i32, i + 1)); | |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | assert(list.pop() == 10); |
std/base64.zig+2-2| ... | ... | @@ -99,7 +99,7 @@ pub const Base64Decoder = struct { |
| 99 | 99 | assert(!result.char_in_alphabet[c]); |
| 100 | 100 | assert(c != pad_char); |
| 101 | 101 | |
| 102 | result.char_to_index[c] = u8(i); | |
| 102 | result.char_to_index[c] = @intCast(u8, i); | |
| 103 | 103 | result.char_in_alphabet[c] = true; |
| 104 | 104 | } |
| 105 | 105 | |
| ... | ... | @@ -284,7 +284,7 @@ pub const Base64DecoderUnsafe = struct { |
| 284 | 284 | }; |
| 285 | 285 | for (alphabet_chars) |c, i| { |
| 286 | 286 | assert(c != pad_char); |
| 287 | result.char_to_index[c] = u8(i); | |
| 287 | result.char_to_index[c] = @intCast(u8, i); | |
| 288 | 288 | } |
| 289 | 289 | return result; |
| 290 | 290 | } |
std/crypto/blake2.zig+5-5| ... | ... | @@ -79,7 +79,7 @@ fn Blake2s(comptime out_len: usize) type { |
| 79 | 79 | mem.copy(u32, d.h[0..], iv[0..]); |
| 80 | 80 | |
| 81 | 81 | // No key plus default parameters |
| 82 | d.h[0] ^= 0x01010000 ^ u32(out_len >> 3); | |
| 82 | d.h[0] ^= 0x01010000 ^ @intCast(u32, out_len >> 3); | |
| 83 | 83 | d.t = 0; |
| 84 | 84 | d.buf_len = 0; |
| 85 | 85 | } |
| ... | ... | @@ -110,7 +110,7 @@ fn Blake2s(comptime out_len: usize) type { |
| 110 | 110 | |
| 111 | 111 | // Copy any remainder for next pass. |
| 112 | 112 | 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); | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | pub fn final(d: *Self, out: []u8) void { |
| ... | ... | @@ -144,7 +144,7 @@ fn Blake2s(comptime out_len: usize) type { |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | v[12] ^= @truncate(u32, d.t); |
| 147 | v[13] ^= u32(d.t >> 32); | |
| 147 | v[13] ^= @intCast(u32, d.t >> 32); | |
| 148 | 148 | if (last) v[14] = ~v[14]; |
| 149 | 149 | |
| 150 | 150 | const rounds = comptime []RoundParam{ |
| ... | ... | @@ -345,7 +345,7 @@ fn Blake2b(comptime out_len: usize) type { |
| 345 | 345 | |
| 346 | 346 | // Copy any remainder for next pass. |
| 347 | 347 | 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); | |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | pub fn final(d: *Self, out: []u8) void { |
| ... | ... | @@ -377,7 +377,7 @@ fn Blake2b(comptime out_len: usize) type { |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | v[12] ^= @truncate(u64, d.t); |
| 380 | v[13] ^= u64(d.t >> 64); | |
| 380 | v[13] ^= @intCast(u64, d.t >> 64); | |
| 381 | 381 | if (last) v[14] = ~v[14]; |
| 382 | 382 | |
| 383 | 383 | const rounds = comptime []RoundParam{ |
std/crypto/md5.zig+3-3| ... | ... | @@ -78,7 +78,7 @@ pub const Md5 = struct { |
| 78 | 78 | |
| 79 | 79 | // Copy any remainder for next pass. |
| 80 | 80 | 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); | |
| 82 | 82 | |
| 83 | 83 | // Md5 uses the bottom 64-bits for length padding |
| 84 | 84 | d.total_len +%= b.len; |
| ... | ... | @@ -103,9 +103,9 @@ pub const Md5 = struct { |
| 103 | 103 | // Append message length. |
| 104 | 104 | var i: usize = 1; |
| 105 | 105 | 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; | |
| 107 | 107 | while (i < 8) : (i += 1) { |
| 108 | d.buf[56 + i] = u8(len & 0xff); | |
| 108 | d.buf[56 + i] = @intCast(u8, len & 0xff); | |
| 109 | 109 | len >>= 8; |
| 110 | 110 | } |
| 111 | 111 |
std/crypto/sha1.zig+3-3| ... | ... | @@ -78,7 +78,7 @@ pub const Sha1 = struct { |
| 78 | 78 | |
| 79 | 79 | // Copy any remainder for next pass. |
| 80 | 80 | 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); | |
| 82 | 82 | |
| 83 | 83 | d.total_len += b.len; |
| 84 | 84 | } |
| ... | ... | @@ -102,9 +102,9 @@ pub const Sha1 = struct { |
| 102 | 102 | // Append message length. |
| 103 | 103 | var i: usize = 1; |
| 104 | 104 | 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; | |
| 106 | 106 | while (i < 8) : (i += 1) { |
| 107 | d.buf[63 - i] = u8(len & 0xff); | |
| 107 | d.buf[63 - i] = @intCast(u8, len & 0xff); | |
| 108 | 108 | len >>= 8; |
| 109 | 109 | } |
| 110 | 110 |
std/crypto/sha2.zig+6-6| ... | ... | @@ -131,7 +131,7 @@ fn Sha2_32(comptime params: Sha2Params32) type { |
| 131 | 131 | |
| 132 | 132 | // Copy any remainder for next pass. |
| 133 | 133 | 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); | |
| 135 | 135 | |
| 136 | 136 | d.total_len += b.len; |
| 137 | 137 | } |
| ... | ... | @@ -155,9 +155,9 @@ fn Sha2_32(comptime params: Sha2Params32) type { |
| 155 | 155 | // Append message length. |
| 156 | 156 | var i: usize = 1; |
| 157 | 157 | 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; | |
| 159 | 159 | while (i < 8) : (i += 1) { |
| 160 | d.buf[63 - i] = u8(len & 0xff); | |
| 160 | d.buf[63 - i] = @intCast(u8, len & 0xff); | |
| 161 | 161 | len >>= 8; |
| 162 | 162 | } |
| 163 | 163 | |
| ... | ... | @@ -472,7 +472,7 @@ fn Sha2_64(comptime params: Sha2Params64) type { |
| 472 | 472 | |
| 473 | 473 | // Copy any remainder for next pass. |
| 474 | 474 | 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); | |
| 476 | 476 | |
| 477 | 477 | d.total_len += b.len; |
| 478 | 478 | } |
| ... | ... | @@ -496,9 +496,9 @@ fn Sha2_64(comptime params: Sha2Params64) type { |
| 496 | 496 | // Append message length. |
| 497 | 497 | var i: usize = 1; |
| 498 | 498 | 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; | |
| 500 | 500 | while (i < 16) : (i += 1) { |
| 501 | d.buf[127 - i] = u8(len & 0xff); | |
| 501 | d.buf[127 - i] = @intCast(u8, len & 0xff); | |
| 502 | 502 | len >>= 8; |
| 503 | 503 | } |
| 504 | 504 |
std/debug/index.zig+4-4| ... | ... | @@ -554,7 +554,7 @@ const LineNumberProgram = struct { |
| 554 | 554 | const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); |
| 555 | 555 | errdefer self.file_entries.allocator.free(file_name); |
| 556 | 556 | 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, | |
| 558 | 558 | .column = self.prev_column, |
| 559 | 559 | .file_name = file_name, |
| 560 | 560 | .allocator = self.file_entries.allocator, |
| ... | ... | @@ -1070,7 +1070,7 @@ fn readULeb128(in_stream: var) !u64 { |
| 1070 | 1070 | |
| 1071 | 1071 | var operand: u64 = undefined; |
| 1072 | 1072 | |
| 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; | |
| 1074 | 1074 | |
| 1075 | 1075 | result |= operand; |
| 1076 | 1076 | |
| ... | ... | @@ -1089,13 +1089,13 @@ fn readILeb128(in_stream: var) !i64 { |
| 1089 | 1089 | |
| 1090 | 1090 | var operand: i64 = undefined; |
| 1091 | 1091 | |
| 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; | |
| 1093 | 1093 | |
| 1094 | 1094 | result |= operand; |
| 1095 | 1095 | shift += 7; |
| 1096 | 1096 | |
| 1097 | 1097 | 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)); | |
| 1099 | 1099 | return result; |
| 1100 | 1100 | } |
| 1101 | 1101 | } |
std/fmt/errol/index.zig+39-39| ... | ... | @@ -29,11 +29,11 @@ pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: Ro |
| 29 | 29 | switch (mode) { |
| 30 | 30 | RoundMode.Decimal => { |
| 31 | 31 | if (float_decimal.exp >= 0) { |
| 32 | round_digit = precision + usize(float_decimal.exp); | |
| 32 | round_digit = precision + @intCast(usize, float_decimal.exp); | |
| 33 | 33 | } else { |
| 34 | 34 | // if a small negative exp, then adjust we need to offset by the number |
| 35 | 35 | // 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); | |
| 37 | 37 | if (precision > min_exp_required) { |
| 38 | 38 | round_digit = precision - min_exp_required; |
| 39 | 39 | } |
| ... | ... | @@ -107,16 +107,16 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal { |
| 107 | 107 | // normalize the midpoint |
| 108 | 108 | |
| 109 | 109 | 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)); | |
| 111 | 111 | if (exp < 20) { |
| 112 | 112 | 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); | |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | var mid = lookup_table[usize(exp)]; | |
| 117 | var mid = lookup_table[@intCast(usize, exp)]; | |
| 118 | 118 | mid = hpProd(mid, val); |
| 119 | const lten = lookup_table[usize(exp)].val; | |
| 119 | const lten = lookup_table[@intCast(usize, exp)].val; | |
| 120 | 120 | |
| 121 | 121 | exp -= 307; |
| 122 | 122 | |
| ... | ... | @@ -168,25 +168,25 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal { |
| 168 | 168 | // the 0-index for this extra digit. |
| 169 | 169 | var buf_index: usize = 1; |
| 170 | 170 | 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; | |
| 173 | 173 | |
| 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; | |
| 176 | 176 | |
| 177 | 177 | if (ldig != hdig) break; |
| 178 | 178 | |
| 179 | 179 | buffer[buf_index] = hdig + '0'; |
| 180 | 180 | 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); | |
| 183 | 183 | hpMul10(&high); |
| 184 | 184 | hpMul10(&low); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | 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; | |
| 190 | 190 | |
| 191 | 191 | buffer[buf_index] = mdig + '0'; |
| 192 | 192 | buf_index += 1; |
| ... | ... | @@ -304,7 +304,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal { |
| 304 | 304 | |
| 305 | 305 | assert((val > 9.007199254740992e15) and val < (3.40282366920938e38)); |
| 306 | 306 | |
| 307 | var mid = u128(val); | |
| 307 | var mid = @floatToInt(u128, val); | |
| 308 | 308 | var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0); |
| 309 | 309 | var high: u128 = mid + fpeint((val - fpprev(val)) / 2.0); |
| 310 | 310 | |
| ... | ... | @@ -314,11 +314,11 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal { |
| 314 | 314 | low -= 1; |
| 315 | 315 | } |
| 316 | 316 | |
| 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); | |
| 319 | 319 | |
| 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); | |
| 322 | 322 | |
| 323 | 323 | if (lf != hf) { |
| 324 | 324 | l64 = lf; |
| ... | ... | @@ -348,7 +348,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal { |
| 348 | 348 | |
| 349 | 349 | return FloatDecimal{ |
| 350 | 350 | .digits = buffer[0..buf_index], |
| 351 | .exp = i32(buf_index) + mi, | |
| 351 | .exp = @intCast(i32, buf_index) + mi, | |
| 352 | 352 | }; |
| 353 | 353 | } |
| 354 | 354 | |
| ... | ... | @@ -359,33 +359,33 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal { |
| 359 | 359 | fn errolFixed(val: f64, buffer: []u8) FloatDecimal { |
| 360 | 360 | assert((val >= 16.0) and (val < 9.007199254740992e15)); |
| 361 | 361 | |
| 362 | const u = u64(val); | |
| 363 | const n = f64(u); | |
| 362 | const u = @floatToInt(u64, val); | |
| 363 | const n = @intToFloat(f64, u); | |
| 364 | 364 | |
| 365 | 365 | var mid = val - n; |
| 366 | 366 | var lo = ((fpprev(val) - n) + mid) / 2.0; |
| 367 | 367 | var hi = ((fpnext(val) - n) + mid) / 2.0; |
| 368 | 368 | |
| 369 | 369 | var buf_index = u64toa(u, buffer); |
| 370 | var exp = i32(buf_index); | |
| 370 | var exp = @intCast(i32, buf_index); | |
| 371 | 371 | var j = buf_index; |
| 372 | 372 | buffer[j] = 0; |
| 373 | 373 | |
| 374 | 374 | if (mid != 0.0) { |
| 375 | 375 | while (mid != 0.0) { |
| 376 | 376 | lo *= 10.0; |
| 377 | const ldig = i32(lo); | |
| 378 | lo -= f64(ldig); | |
| 377 | const ldig = @floatToInt(i32, lo); | |
| 378 | lo -= @intToFloat(f64, ldig); | |
| 379 | 379 | |
| 380 | 380 | mid *= 10.0; |
| 381 | const mdig = i32(mid); | |
| 382 | mid -= f64(mdig); | |
| 381 | const mdig = @floatToInt(i32, mid); | |
| 382 | mid -= @intToFloat(f64, mdig); | |
| 383 | 383 | |
| 384 | 384 | hi *= 10.0; |
| 385 | const hdig = i32(hi); | |
| 386 | hi -= f64(hdig); | |
| 385 | const hdig = @floatToInt(i32, hi); | |
| 386 | hi -= @intToFloat(f64, hdig); | |
| 387 | 387 | |
| 388 | buffer[j] = u8(mdig + '0'); | |
| 388 | buffer[j] = @intCast(u8, mdig + '0'); | |
| 389 | 389 | j += 1; |
| 390 | 390 | |
| 391 | 391 | if (hdig != ldig or j > 50) break; |
| ... | ... | @@ -452,7 +452,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize { |
| 452 | 452 | var buf_index: usize = 0; |
| 453 | 453 | |
| 454 | 454 | if (value < kTen8) { |
| 455 | const v = u32(value); | |
| 455 | const v = @intCast(u32, value); | |
| 456 | 456 | if (v < 10000) { |
| 457 | 457 | const d1: u32 = (v / 100) << 1; |
| 458 | 458 | const d2: u32 = (v % 100) << 1; |
| ... | ... | @@ -507,8 +507,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize { |
| 507 | 507 | buf_index += 1; |
| 508 | 508 | } |
| 509 | 509 | } 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); | |
| 512 | 512 | |
| 513 | 513 | const b0: u32 = v0 / 10000; |
| 514 | 514 | const c0: u32 = v0 % 10000; |
| ... | ... | @@ -578,11 +578,11 @@ fn u64toa(value_param: u64, buffer: []u8) usize { |
| 578 | 578 | buffer[buf_index] = c_digits_lut[d8 + 1]; |
| 579 | 579 | buf_index += 1; |
| 580 | 580 | } else { |
| 581 | const a = u32(value / kTen16); // 1 to 1844 | |
| 581 | const a = @intCast(u32, value / kTen16); // 1 to 1844 | |
| 582 | 582 | value %= kTen16; |
| 583 | 583 | |
| 584 | 584 | if (a < 10) { |
| 585 | buffer[buf_index] = '0' + u8(a); | |
| 585 | buffer[buf_index] = '0' + @intCast(u8, a); | |
| 586 | 586 | buf_index += 1; |
| 587 | 587 | } else if (a < 100) { |
| 588 | 588 | const i: u32 = a << 1; |
| ... | ... | @@ -591,7 +591,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize { |
| 591 | 591 | buffer[buf_index] = c_digits_lut[i + 1]; |
| 592 | 592 | buf_index += 1; |
| 593 | 593 | } else if (a < 1000) { |
| 594 | buffer[buf_index] = '0' + u8(a / 100); | |
| 594 | buffer[buf_index] = '0' + @intCast(u8, a / 100); | |
| 595 | 595 | buf_index += 1; |
| 596 | 596 | |
| 597 | 597 | const i: u32 = (a % 100) << 1; |
| ... | ... | @@ -612,8 +612,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize { |
| 612 | 612 | buf_index += 1; |
| 613 | 613 | } |
| 614 | 614 | |
| 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); | |
| 617 | 617 | |
| 618 | 618 | const b0: u32 = v0 / 10000; |
| 619 | 619 | const c0: u32 = v0 % 10000; |
std/fmt/index.zig+10-9| ... | ... | @@ -5,6 +5,7 @@ const assert = debug.assert; |
| 5 | 5 | const mem = std.mem; |
| 6 | 6 | const builtin = @import("builtin"); |
| 7 | 7 | const errol = @import("errol/index.zig"); |
| 8 | const lossyCast = std.math.lossyCast; | |
| 8 | 9 | |
| 9 | 10 | const max_int_digits = 65; |
| 10 | 11 | |
| ... | ... | @@ -463,7 +464,7 @@ pub fn formatFloatDecimal( |
| 463 | 464 | errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal); |
| 464 | 465 | |
| 465 | 466 | // 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; | |
| 467 | 468 | |
| 468 | 469 | // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this. |
| 469 | 470 | var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len); |
| ... | ... | @@ -492,7 +493,7 @@ pub fn formatFloatDecimal( |
| 492 | 493 | |
| 493 | 494 | // Zero-fill until we reach significant digits or run out of precision. |
| 494 | 495 | if (float_decimal.exp <= 0) { |
| 495 | const zero_digit_count = usize(-float_decimal.exp); | |
| 496 | const zero_digit_count = @intCast(usize, -float_decimal.exp); | |
| 496 | 497 | const zeros_to_print = math.min(zero_digit_count, precision); |
| 497 | 498 | |
| 498 | 499 | var i: usize = 0; |
| ... | ... | @@ -521,7 +522,7 @@ pub fn formatFloatDecimal( |
| 521 | 522 | } |
| 522 | 523 | } else { |
| 523 | 524 | // 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; | |
| 525 | 526 | |
| 526 | 527 | // the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this. |
| 527 | 528 | var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len); |
| ... | ... | @@ -547,7 +548,7 @@ pub fn formatFloatDecimal( |
| 547 | 548 | |
| 548 | 549 | // Zero-fill until we reach significant digits or run out of precision. |
| 549 | 550 | if (float_decimal.exp < 0) { |
| 550 | const zero_digit_count = usize(-float_decimal.exp); | |
| 551 | const zero_digit_count = @intCast(usize, -float_decimal.exp); | |
| 551 | 552 | |
| 552 | 553 | var i: usize = 0; |
| 553 | 554 | while (i < zero_digit_count) : (i += 1) { |
| ... | ... | @@ -578,7 +579,7 @@ pub fn formatBytes( |
| 578 | 579 | 1024 => math.min(math.log2(value) / 10, mags_iec.len - 1), |
| 579 | 580 | else => unreachable, |
| 580 | 581 | }; |
| 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)); | |
| 582 | 583 | const suffix = switch (radix) { |
| 583 | 584 | 1000 => mags_si[magnitude], |
| 584 | 585 | 1024 => mags_iec[magnitude], |
| ... | ... | @@ -628,15 +629,15 @@ fn formatIntSigned( |
| 628 | 629 | if (value < 0) { |
| 629 | 630 | const minus_sign: u8 = '-'; |
| 630 | 631 | 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; | |
| 632 | 633 | const new_width = if (width == 0) 0 else (width - 1); |
| 633 | 634 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); |
| 634 | 635 | } 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); | |
| 636 | 637 | } else { |
| 637 | 638 | const plus_sign: u8 = '+'; |
| 638 | 639 | try output(context, (*[1]u8)(&plus_sign)[0..]); |
| 639 | const new_value = uint(value); | |
| 640 | const new_value = @intCast(uint, value); | |
| 640 | 641 | const new_width = if (width == 0) 0 else (width - 1); |
| 641 | 642 | return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output); |
| 642 | 643 | } |
| ... | ... | @@ -660,7 +661,7 @@ fn formatIntUnsigned( |
| 660 | 661 | while (true) { |
| 661 | 662 | const digit = a % base; |
| 662 | 663 | index -= 1; |
| 663 | buf[index] = digitToChar(u8(digit), uppercase); | |
| 664 | buf[index] = digitToChar(@intCast(u8, digit), uppercase); | |
| 664 | 665 | a /= base; |
| 665 | 666 | if (a == 0) break; |
| 666 | 667 | } |
std/hash/crc.zig+2-2| ... | ... | @@ -26,7 +26,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { |
| 26 | 26 | var tables: [8][256]u32 = undefined; |
| 27 | 27 | |
| 28 | 28 | for (tables[0]) |*e, i| { |
| 29 | var crc = u32(i); | |
| 29 | var crc = @intCast(u32, i); | |
| 30 | 30 | var j: usize = 0; |
| 31 | 31 | while (j < 8) : (j += 1) { |
| 32 | 32 | if (crc & 1 == 1) { |
| ... | ... | @@ -122,7 +122,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { |
| 122 | 122 | var table: [16]u32 = undefined; |
| 123 | 123 | |
| 124 | 124 | for (table) |*e, i| { |
| 125 | var crc = u32(i * 16); | |
| 125 | var crc = @intCast(u32, i * 16); | |
| 126 | 126 | var j: usize = 0; |
| 127 | 127 | while (j < 8) : (j += 1) { |
| 128 | 128 | 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) |
| 81 | 81 | |
| 82 | 82 | // Remainder for next pass. |
| 83 | 83 | 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); | |
| 85 | 85 | d.msg_len +%= @truncate(u8, b.len); |
| 86 | 86 | } |
| 87 | 87 | |
| ... | ... | @@ -233,7 +233,7 @@ test "siphash64-2-4 sanity" { |
| 233 | 233 | |
| 234 | 234 | var buffer: [64]u8 = undefined; |
| 235 | 235 | for (vectors) |vector, i| { |
| 236 | buffer[i] = u8(i); | |
| 236 | buffer[i] = @intCast(u8, i); | |
| 237 | 237 | |
| 238 | 238 | const expected = mem.readInt(vector, u64, Endian.Little); |
| 239 | 239 | debug.assert(siphash.hash(test_key, buffer[0..i]) == expected); |
| ... | ... | @@ -312,7 +312,7 @@ test "siphash128-2-4 sanity" { |
| 312 | 312 | |
| 313 | 313 | var buffer: [64]u8 = undefined; |
| 314 | 314 | for (vectors) |vector, i| { |
| 315 | buffer[i] = u8(i); | |
| 315 | buffer[i] = @intCast(u8, i); | |
| 316 | 316 | |
| 317 | 317 | const expected = mem.readInt(vector, u128, Endian.Little); |
| 318 | 318 | 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 { |
| 408 | 408 | |
| 409 | 409 | for (slice) |*item, i| { |
| 410 | 410 | item.* = try allocator.create(i32); |
| 411 | item.*.* = i32(i); | |
| 411 | item.*.* = @intCast(i32, i); | |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | for (slice) |item, i| { |
std/json.zig+1-1| ... | ... | @@ -180,7 +180,7 @@ pub const StreamingParser = struct { |
| 180 | 180 | pub fn fromInt(x: var) State { |
| 181 | 181 | debug.assert(x == 0 or x == 1); |
| 182 | 182 | const T = @TagType(State); |
| 183 | return State(T(x)); | |
| 183 | return State(@intCast(T, x)); | |
| 184 | 184 | } |
| 185 | 185 | }; |
| 186 | 186 |
std/math/acos.zig+2-2| ... | ... | @@ -95,12 +95,12 @@ fn acos64(x: f64) f64 { |
| 95 | 95 | const pio2_lo: f64 = 6.12323399573676603587e-17; |
| 96 | 96 | |
| 97 | 97 | const ux = @bitCast(u64, x); |
| 98 | const hx = u32(ux >> 32); | |
| 98 | const hx = @intCast(u32, ux >> 32); | |
| 99 | 99 | const ix = hx & 0x7FFFFFFF; |
| 100 | 100 | |
| 101 | 101 | // |x| >= 1 or nan |
| 102 | 102 | if (ix >= 0x3FF00000) { |
| 103 | const lx = u32(ux & 0xFFFFFFFF); | |
| 103 | const lx = @intCast(u32, ux & 0xFFFFFFFF); | |
| 104 | 104 | |
| 105 | 105 | // acos(1) = 0, acos(-1) = pi |
| 106 | 106 | if ((ix - 0x3FF00000) | lx == 0) { |
std/math/asin.zig+2-2| ... | ... | @@ -87,12 +87,12 @@ fn asin64(x: f64) f64 { |
| 87 | 87 | const pio2_lo: f64 = 6.12323399573676603587e-17; |
| 88 | 88 | |
| 89 | 89 | const ux = @bitCast(u64, x); |
| 90 | const hx = u32(ux >> 32); | |
| 90 | const hx = @intCast(u32, ux >> 32); | |
| 91 | 91 | const ix = hx & 0x7FFFFFFF; |
| 92 | 92 | |
| 93 | 93 | // |x| >= 1 or nan |
| 94 | 94 | if (ix >= 0x3FF00000) { |
| 95 | const lx = u32(ux & 0xFFFFFFFF); | |
| 95 | const lx = @intCast(u32, ux & 0xFFFFFFFF); | |
| 96 | 96 | |
| 97 | 97 | // asin(1) = +-pi/2 with inexact |
| 98 | 98 | if ((ix - 0x3FF00000) | lx == 0) { |
std/math/atan.zig+2-2| ... | ... | @@ -138,7 +138,7 @@ fn atan64(x_: f64) f64 { |
| 138 | 138 | |
| 139 | 139 | var x = x_; |
| 140 | 140 | var ux = @bitCast(u64, x); |
| 141 | var ix = u32(ux >> 32); | |
| 141 | var ix = @intCast(u32, ux >> 32); | |
| 142 | 142 | const sign = ix >> 31; |
| 143 | 143 | ix &= 0x7FFFFFFF; |
| 144 | 144 | |
| ... | ... | @@ -159,7 +159,7 @@ fn atan64(x_: f64) f64 { |
| 159 | 159 | // |x| < 2^(-27) |
| 160 | 160 | if (ix < 0x3E400000) { |
| 161 | 161 | if (ix < 0x00100000) { |
| 162 | math.forceEval(f32(x)); | |
| 162 | math.forceEval(@floatCast(f32, x)); | |
| 163 | 163 | } |
| 164 | 164 | return x; |
| 165 | 165 | } |
std/math/atan2.zig+4-4| ... | ... | @@ -124,12 +124,12 @@ fn atan2_64(y: f64, x: f64) f64 { |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | 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); | |
| 129 | 129 | |
| 130 | 130 | 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); | |
| 133 | 133 | |
| 134 | 134 | // x = 1.0 |
| 135 | 135 | if ((ix -% 0x3FF00000) | lx == 0) { |
std/math/atanh.zig+1-1| ... | ... | @@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 { |
| 62 | 62 | if (e < 0x3FF - 32) { |
| 63 | 63 | // underflow |
| 64 | 64 | if (e == 0) { |
| 65 | math.forceEval(f32(y)); | |
| 65 | math.forceEval(@floatCast(f32, y)); | |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | 68 | // |x| < 0.5 |
std/math/big/int.zig+11-11| ... | ... | @@ -135,7 +135,7 @@ pub const Int = struct { |
| 135 | 135 | self.positive = value >= 0; |
| 136 | 136 | self.len = 0; |
| 137 | 137 | |
| 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); | |
| 139 | 139 | |
| 140 | 140 | if (info.bits <= Limb.bit_count) { |
| 141 | 141 | self.limbs[0] = Limb(w_value); |
| ... | ... | @@ -198,7 +198,7 @@ pub const Int = struct { |
| 198 | 198 | var r: UT = 0; |
| 199 | 199 | |
| 200 | 200 | if (@sizeOf(UT) <= @sizeOf(Limb)) { |
| 201 | r = UT(self.limbs[0]); | |
| 201 | r = @intCast(UT, self.limbs[0]); | |
| 202 | 202 | } else { |
| 203 | 203 | for (self.limbs[0..self.len]) |_, ri| { |
| 204 | 204 | const limb = self.limbs[self.len - ri - 1]; |
| ... | ... | @@ -210,7 +210,7 @@ pub const Int = struct { |
| 210 | 210 | if (!T.is_signed) { |
| 211 | 211 | return if (self.positive) r else error.NegativeIntoUnsigned; |
| 212 | 212 | } else { |
| 213 | return if (self.positive) T(r) else -T(r); | |
| 213 | return if (self.positive) @intCast(T, r) else -@intCast(T, r); | |
| 214 | 214 | } |
| 215 | 215 | }, |
| 216 | 216 | else => { |
| ... | ... | @@ -295,7 +295,7 @@ pub const Int = struct { |
| 295 | 295 | for (self.limbs[0..self.len]) |limb| { |
| 296 | 296 | var shift: usize = 0; |
| 297 | 297 | 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)); | |
| 299 | 299 | const ch = try digitToChar(r, base); |
| 300 | 300 | try digits.append(ch); |
| 301 | 301 | } |
| ... | ... | @@ -329,7 +329,7 @@ pub const Int = struct { |
| 329 | 329 | var r_word = r.limbs[0]; |
| 330 | 330 | var i: usize = 0; |
| 331 | 331 | 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); | |
| 333 | 333 | r_word /= base; |
| 334 | 334 | try digits.append(ch); |
| 335 | 335 | } |
| ... | ... | @@ -340,7 +340,7 @@ pub const Int = struct { |
| 340 | 340 | |
| 341 | 341 | var r_word = q.limbs[0]; |
| 342 | 342 | 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); | |
| 344 | 344 | r_word /= base; |
| 345 | 345 | try digits.append(ch); |
| 346 | 346 | } |
| ... | ... | @@ -801,7 +801,7 @@ pub const Int = struct { |
| 801 | 801 | q.limbs[i - t - 1] = @maxValue(Limb); |
| 802 | 802 | } else { |
| 803 | 803 | 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])); | |
| 805 | 805 | q.limbs[i - t - 1] = if (z > @maxValue(Limb)) @maxValue(Limb) else Limb(z); |
| 806 | 806 | } |
| 807 | 807 | |
| ... | ... | @@ -860,7 +860,7 @@ pub const Int = struct { |
| 860 | 860 | debug.assert(r.len >= a.len + (shift / Limb.bit_count) + 1); |
| 861 | 861 | |
| 862 | 862 | 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); | |
| 864 | 864 | |
| 865 | 865 | var carry: Limb = 0; |
| 866 | 866 | var i: usize = 0; |
| ... | ... | @@ -869,7 +869,7 @@ pub const Int = struct { |
| 869 | 869 | const dst_i = src_i + limb_shift; |
| 870 | 870 | |
| 871 | 871 | 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)); | |
| 873 | 873 | carry = (src_digit << interior_limb_shift); |
| 874 | 874 | } |
| 875 | 875 | |
| ... | ... | @@ -898,7 +898,7 @@ pub const Int = struct { |
| 898 | 898 | debug.assert(r.len >= a.len - (shift / Limb.bit_count)); |
| 899 | 899 | |
| 900 | 900 | 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); | |
| 902 | 902 | |
| 903 | 903 | var carry: Limb = 0; |
| 904 | 904 | var i: usize = 0; |
| ... | ... | @@ -908,7 +908,7 @@ pub const Int = struct { |
| 908 | 908 | |
| 909 | 909 | const src_digit = a[src_i]; |
| 910 | 910 | 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)); | |
| 912 | 912 | } |
| 913 | 913 | } |
| 914 | 914 |
std/math/cbrt.zig+3-3| ... | ... | @@ -54,7 +54,7 @@ fn cbrt32(x: f32) f32 { |
| 54 | 54 | r = t * t * t; |
| 55 | 55 | t = t * (f64(x) + x + r) / (x + r + r); |
| 56 | 56 | |
| 57 | return f32(t); | |
| 57 | return @floatCast(f32, t); | |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | fn cbrt64(x: f64) f64 { |
| ... | ... | @@ -69,7 +69,7 @@ fn cbrt64(x: f64) f64 { |
| 69 | 69 | const P4: f64 = 0.145996192886612446982; |
| 70 | 70 | |
| 71 | 71 | var u = @bitCast(u64, x); |
| 72 | var hx = u32(u >> 32) & 0x7FFFFFFF; | |
| 72 | var hx = @intCast(u32, u >> 32) & 0x7FFFFFFF; | |
| 73 | 73 | |
| 74 | 74 | // cbrt(nan, inf) = itself |
| 75 | 75 | if (hx >= 0x7FF00000) { |
| ... | ... | @@ -79,7 +79,7 @@ fn cbrt64(x: f64) f64 { |
| 79 | 79 | // cbrt to ~5bits |
| 80 | 80 | if (hx < 0x00100000) { |
| 81 | 81 | u = @bitCast(u64, x * 0x1.0p54); |
| 82 | hx = u32(u >> 32) & 0x7FFFFFFF; | |
| 82 | hx = @intCast(u32, u >> 32) & 0x7FFFFFFF; | |
| 83 | 83 | |
| 84 | 84 | // cbrt(0) is itself |
| 85 | 85 | if (hx == 0) { |
std/math/ceil.zig+2-2| ... | ... | @@ -20,7 +20,7 @@ pub fn ceil(x: var) @typeOf(x) { |
| 20 | 20 | |
| 21 | 21 | fn ceil32(x: f32) f32 { |
| 22 | 22 | var u = @bitCast(u32, x); |
| 23 | var e = i32((u >> 23) & 0xFF) - 0x7F; | |
| 23 | var e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F; | |
| 24 | 24 | var m: u32 = undefined; |
| 25 | 25 | |
| 26 | 26 | // TODO: Shouldn't need this explicit check. |
| ... | ... | @@ -31,7 +31,7 @@ fn ceil32(x: f32) f32 { |
| 31 | 31 | if (e >= 23) { |
| 32 | 32 | return x; |
| 33 | 33 | } else if (e >= 0) { |
| 34 | m = u32(0x007FFFFF) >> u5(e); | |
| 34 | m = u32(0x007FFFFF) >> @intCast(u5, e); | |
| 35 | 35 | if (u & m == 0) { |
| 36 | 36 | return x; |
| 37 | 37 | } |
std/math/complex/atan.zig+5-5| ... | ... | @@ -4,7 +4,7 @@ const math = std.math; |
| 4 | 4 | const cmath = math.complex; |
| 5 | 5 | const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | pub fn atan(z: var) Complex(@typeOf(z.re)) { | |
| 7 | pub fn atan(z: var) @typeOf(z) { | |
| 8 | 8 | const T = @typeOf(z.re); |
| 9 | 9 | return switch (T) { |
| 10 | 10 | f32 => atan32(z), |
| ... | ... | @@ -25,11 +25,11 @@ fn redupif32(x: f32) f32 { |
| 25 | 25 | t -= 0.5; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | const u = f32(i32(t)); | |
| 28 | const u = @intToFloat(f32, @floatToInt(i32, t)); | |
| 29 | 29 | return ((x - u * DP1) - u * DP2) - t * DP3; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | fn atan32(z: *const Complex(f32)) Complex(f32) { | |
| 32 | fn atan32(z: Complex(f32)) Complex(f32) { | |
| 33 | 33 | const maxnum = 1.0e38; |
| 34 | 34 | |
| 35 | 35 | const x = z.re; |
| ... | ... | @@ -74,11 +74,11 @@ fn redupif64(x: f64) f64 { |
| 74 | 74 | t -= 0.5; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | const u = f64(i64(t)); | |
| 77 | const u = @intToFloat(f64, @floatToInt(i64, t)); | |
| 78 | 78 | return ((x - u * DP1) - u * DP2) - t * DP3; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | fn atan64(z: *const Complex(f64)) Complex(f64) { | |
| 81 | fn atan64(z: Complex(f64)) Complex(f64) { | |
| 82 | 82 | const maxnum = 1.0e308; |
| 83 | 83 | |
| 84 | 84 | const x = z.re; |
std/math/complex/cosh.zig+2-2| ... | ... | @@ -83,12 +83,12 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) { |
| 83 | 83 | const y = z.im; |
| 84 | 84 | |
| 85 | 85 | const fx = @bitCast(u64, x); |
| 86 | const hx = u32(fx >> 32); | |
| 86 | const hx = @intCast(u32, fx >> 32); | |
| 87 | 87 | const lx = @truncate(u32, fx); |
| 88 | 88 | const ix = hx & 0x7fffffff; |
| 89 | 89 | |
| 90 | 90 | const fy = @bitCast(u64, y); |
| 91 | const hy = u32(fy >> 32); | |
| 91 | const hy = @intCast(u32, fy >> 32); | |
| 92 | 92 | const ly = @truncate(u32, fy); |
| 93 | 93 | const iy = hy & 0x7fffffff; |
| 94 | 94 |
std/math/complex/exp.zig+3-3| ... | ... | @@ -6,7 +6,7 @@ const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | 7 | const ldexp_cexp = @import("ldexp.zig").ldexp_cexp; |
| 8 | 8 | |
| 9 | pub fn exp(z: var) Complex(@typeOf(z.re)) { | |
| 9 | pub fn exp(z: var) @typeOf(z) { | |
| 10 | 10 | const T = @typeOf(z.re); |
| 11 | 11 | |
| 12 | 12 | return switch (T) { |
| ... | ... | @@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) { |
| 16 | 16 | }; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | fn exp32(z: *const Complex(f32)) Complex(f32) { | |
| 19 | fn exp32(z: Complex(f32)) Complex(f32) { | |
| 20 | 20 | @setFloatMode(this, @import("builtin").FloatMode.Strict); |
| 21 | 21 | |
| 22 | 22 | const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955 |
| ... | ... | @@ -63,7 +63,7 @@ fn exp32(z: *const Complex(f32)) Complex(f32) { |
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | fn exp64(z: *const Complex(f64)) Complex(f64) { | |
| 66 | fn exp64(z: Complex(f64)) Complex(f64) { | |
| 67 | 67 | const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710 |
| 68 | 68 | const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2 |
| 69 | 69 |
std/math/complex/index.zig+7-7| ... | ... | @@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type { |
| 37 | 37 | }; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | pub fn add(self: *const Self, other: *const Self) Self { | |
| 40 | pub fn add(self: Self, other: Self) Self { | |
| 41 | 41 | return Self{ |
| 42 | 42 | .re = self.re + other.re, |
| 43 | 43 | .im = self.im + other.im, |
| 44 | 44 | }; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | pub fn sub(self: *const Self, other: *const Self) Self { | |
| 47 | pub fn sub(self: Self, other: Self) Self { | |
| 48 | 48 | return Self{ |
| 49 | 49 | .re = self.re - other.re, |
| 50 | 50 | .im = self.im - other.im, |
| 51 | 51 | }; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | pub fn mul(self: *const Self, other: *const Self) Self { | |
| 54 | pub fn mul(self: Self, other: Self) Self { | |
| 55 | 55 | return Self{ |
| 56 | 56 | .re = self.re * other.re - self.im * other.im, |
| 57 | 57 | .im = self.im * other.re + self.re * other.im, |
| 58 | 58 | }; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | pub fn div(self: *const Self, other: *const Self) Self { | |
| 61 | pub fn div(self: Self, other: Self) Self { | |
| 62 | 62 | const re_num = self.re * other.re + self.im * other.im; |
| 63 | 63 | const im_num = self.im * other.re - self.re * other.im; |
| 64 | 64 | const den = other.re * other.re + other.im * other.im; |
| ... | ... | @@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type { |
| 69 | 69 | }; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | pub fn conjugate(self: *const Self) Self { | |
| 72 | pub fn conjugate(self: Self) Self { | |
| 73 | 73 | return Self{ |
| 74 | 74 | .re = self.re, |
| 75 | 75 | .im = -self.im, |
| 76 | 76 | }; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | pub fn reciprocal(self: *const Self) Self { | |
| 79 | pub fn reciprocal(self: Self) Self { | |
| 80 | 80 | const m = self.re * self.re + self.im * self.im; |
| 81 | 81 | return Self{ |
| 82 | 82 | .re = self.re / m, |
| ... | ... | @@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type { |
| 84 | 84 | }; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | pub fn magnitude(self: *const Self) T { | |
| 87 | pub fn magnitude(self: Self) T { | |
| 88 | 88 | return math.sqrt(self.re * self.re + self.im * self.im); |
| 89 | 89 | } |
| 90 | 90 | }; |
std/math/complex/ldexp.zig+7-6| ... | ... | @@ -4,7 +4,7 @@ const math = std.math; |
| 4 | 4 | const cmath = math.complex; |
| 5 | 5 | const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) { | |
| 7 | pub fn ldexp_cexp(z: var, expt: i32) @typeOf(z) { | |
| 8 | 8 | const T = @typeOf(z.re); |
| 9 | 9 | |
| 10 | 10 | return switch (T) { |
| ... | ... | @@ -20,11 +20,12 @@ fn frexp_exp32(x: f32, expt: *i32) f32 { |
| 20 | 20 | |
| 21 | 21 | const exp_x = math.exp(x - kln2); |
| 22 | 22 | 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; | |
| 24 | 25 | return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23)); |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) { | |
| 28 | fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { | |
| 28 | 29 | var ex_expt: i32 = undefined; |
| 29 | 30 | const exp_x = frexp_exp32(z.re, &ex_expt); |
| 30 | 31 | const exptf = expt + ex_expt; |
| ... | ... | @@ -45,16 +46,16 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { |
| 45 | 46 | const exp_x = math.exp(x - kln2); |
| 46 | 47 | |
| 47 | 48 | const fx = @bitCast(u64, x); |
| 48 | const hx = u32(fx >> 32); | |
| 49 | const hx = @intCast(u32, fx >> 32); | |
| 49 | 50 | const lx = @truncate(u32, fx); |
| 50 | 51 | |
| 51 | expt.* = i32(hx >> 20) - (0x3ff + 1023) + k; | |
| 52 | expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k; | |
| 52 | 53 | |
| 53 | 54 | const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20); |
| 54 | 55 | return @bitCast(f64, (u64(high_word) << 32) | lx); |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) { | |
| 58 | fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { | |
| 58 | 59 | var ex_expt: i32 = undefined; |
| 59 | 60 | const exp_x = frexp_exp64(z.re, &ex_expt); |
| 60 | 61 | const exptf = i64(expt + ex_expt); |
std/math/complex/sinh.zig+5-5| ... | ... | @@ -6,7 +6,7 @@ const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | 7 | const ldexp_cexp = @import("ldexp.zig").ldexp_cexp; |
| 8 | 8 | |
| 9 | pub fn sinh(z: var) Complex(@typeOf(z.re)) { | |
| 9 | pub fn sinh(z: var) @typeOf(z) { | |
| 10 | 10 | const T = @typeOf(z.re); |
| 11 | 11 | return switch (T) { |
| 12 | 12 | f32 => sinh32(z), |
| ... | ... | @@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) { |
| 15 | 15 | }; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | fn sinh32(z: *const Complex(f32)) Complex(f32) { | |
| 18 | fn sinh32(z: Complex(f32)) Complex(f32) { | |
| 19 | 19 | const x = z.re; |
| 20 | 20 | const y = z.im; |
| 21 | 21 | |
| ... | ... | @@ -78,17 +78,17 @@ fn sinh32(z: *const Complex(f32)) Complex(f32) { |
| 78 | 78 | return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | fn sinh64(z: *const Complex(f64)) Complex(f64) { | |
| 81 | fn sinh64(z: Complex(f64)) Complex(f64) { | |
| 82 | 82 | const x = z.re; |
| 83 | 83 | const y = z.im; |
| 84 | 84 | |
| 85 | 85 | const fx = @bitCast(u64, x); |
| 86 | const hx = u32(fx >> 32); | |
| 86 | const hx = @intCast(u32, fx >> 32); | |
| 87 | 87 | const lx = @truncate(u32, fx); |
| 88 | 88 | const ix = hx & 0x7fffffff; |
| 89 | 89 | |
| 90 | 90 | const fy = @bitCast(u64, y); |
| 91 | const hy = u32(fy >> 32); | |
| 91 | const hy = @intCast(u32, fy >> 32); | |
| 92 | 92 | const ly = @truncate(u32, fy); |
| 93 | 93 | const iy = hy & 0x7fffffff; |
| 94 | 94 |
std/math/complex/sqrt.zig+12-7| ... | ... | @@ -4,18 +4,17 @@ const math = std.math; |
| 4 | 4 | const cmath = math.complex; |
| 5 | 5 | const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | // TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re)) | |
| 8 | pub fn sqrt(z: var) Complex(@typeOf(z.re)) { | |
| 7 | pub fn sqrt(z: var) @typeOf(z) { | |
| 9 | 8 | const T = @typeOf(z.re); |
| 10 | 9 | |
| 11 | 10 | return switch (T) { |
| 12 | 11 | f32 => sqrt32(z), |
| 13 | 12 | f64 => sqrt64(z), |
| 14 | else => @compileError("sqrt not implemented for " ++ @typeName(z)), | |
| 13 | else => @compileError("sqrt not implemented for " ++ @typeName(T)), | |
| 15 | 14 | }; |
| 16 | 15 | } |
| 17 | 16 | |
| 18 | fn sqrt32(z: *const Complex(f32)) Complex(f32) { | |
| 17 | fn sqrt32(z: Complex(f32)) Complex(f32) { | |
| 19 | 18 | const x = z.re; |
| 20 | 19 | const y = z.im; |
| 21 | 20 | |
| ... | ... | @@ -50,14 +49,20 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) { |
| 50 | 49 | |
| 51 | 50 | if (dx >= 0) { |
| 52 | 51 | 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 | ); | |
| 54 | 56 | } else { |
| 55 | 57 | 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 | ); | |
| 57 | 62 | } |
| 58 | 63 | } |
| 59 | 64 | |
| 60 | fn sqrt64(z: *const Complex(f64)) Complex(f64) { | |
| 65 | fn sqrt64(z: Complex(f64)) Complex(f64) { | |
| 61 | 66 | // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2)) |
| 62 | 67 | const threshold = 0x1.a827999fcef32p+1022; |
| 63 | 68 |
std/math/complex/tanh.zig+6-4| ... | ... | @@ -4,7 +4,7 @@ const math = std.math; |
| 4 | 4 | const cmath = math.complex; |
| 5 | 5 | const Complex = cmath.Complex; |
| 6 | 6 | |
| 7 | pub fn tanh(z: var) Complex(@typeOf(z.re)) { | |
| 7 | pub fn tanh(z: var) @typeOf(z) { | |
| 8 | 8 | const T = @typeOf(z.re); |
| 9 | 9 | return switch (T) { |
| 10 | 10 | f32 => tanh32(z), |
| ... | ... | @@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) { |
| 13 | 13 | }; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | fn tanh32(z: *const Complex(f32)) Complex(f32) { | |
| 16 | fn tanh32(z: Complex(f32)) Complex(f32) { | |
| 17 | 17 | const x = z.re; |
| 18 | 18 | const y = z.im; |
| 19 | 19 | |
| ... | ... | @@ -51,12 +51,14 @@ fn tanh32(z: *const Complex(f32)) Complex(f32) { |
| 51 | 51 | return Complex(f32).new((beta * rho * s) / den, t / den); |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | fn tanh64(z: *const Complex(f64)) Complex(f64) { | |
| 54 | fn tanh64(z: Complex(f64)) Complex(f64) { | |
| 55 | 55 | const x = z.re; |
| 56 | 56 | const y = z.im; |
| 57 | 57 | |
| 58 | 58 | 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); | |
| 60 | 62 | const lx = @truncate(u32, fx); |
| 61 | 63 | const ix = hx & 0x7fffffff; |
| 62 | 64 |
std/math/cos.zig+2-2| ... | ... | @@ -55,7 +55,7 @@ fn cos32(x_: f32) f32 { |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | var y = math.floor(x * m4pi); |
| 58 | var j = i64(y); | |
| 58 | var j = @floatToInt(i64, y); | |
| 59 | 59 | |
| 60 | 60 | if (j & 1 == 1) { |
| 61 | 61 | j += 1; |
| ... | ... | @@ -106,7 +106,7 @@ fn cos64(x_: f64) f64 { |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | var y = math.floor(x * m4pi); |
| 109 | var j = i64(y); | |
| 109 | var j = @floatToInt(i64, y); | |
| 110 | 110 | |
| 111 | 111 | if (j & 1 == 1) { |
| 112 | 112 | j += 1; |
std/math/cosh.zig+1-1| ... | ... | @@ -49,7 +49,7 @@ fn cosh32(x: f32) f32 { |
| 49 | 49 | |
| 50 | 50 | fn cosh64(x: f64) f64 { |
| 51 | 51 | const u = @bitCast(u64, x); |
| 52 | const w = u32(u >> 32); | |
| 52 | const w = @intCast(u32, u >> 32); | |
| 53 | 53 | const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); |
| 54 | 54 | |
| 55 | 55 | // TODO: Shouldn't need this explicit check. |
std/math/exp.zig+6-6| ... | ... | @@ -29,7 +29,7 @@ fn exp32(x_: f32) f32 { |
| 29 | 29 | |
| 30 | 30 | var x = x_; |
| 31 | 31 | var hx = @bitCast(u32, x); |
| 32 | const sign = i32(hx >> 31); | |
| 32 | const sign = @intCast(i32, hx >> 31); | |
| 33 | 33 | hx &= 0x7FFFFFFF; |
| 34 | 34 | |
| 35 | 35 | if (math.isNan(x)) { |
| ... | ... | @@ -63,12 +63,12 @@ fn exp32(x_: f32) f32 { |
| 63 | 63 | if (hx > 0x3EB17218) { |
| 64 | 64 | // |x| > 1.5 * ln2 |
| 65 | 65 | if (hx > 0x3F851592) { |
| 66 | k = i32(invln2 * x + half[usize(sign)]); | |
| 66 | k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]); | |
| 67 | 67 | } else { |
| 68 | 68 | k = 1 - sign - sign; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | const fk = f32(k); | |
| 71 | const fk = @intToFloat(f32, k); | |
| 72 | 72 | hi = x - fk * ln2hi; |
| 73 | 73 | lo = fk * ln2lo; |
| 74 | 74 | x = hi - lo; |
| ... | ... | @@ -110,7 +110,7 @@ fn exp64(x_: f64) f64 { |
| 110 | 110 | var x = x_; |
| 111 | 111 | var ux = @bitCast(u64, x); |
| 112 | 112 | var hx = ux >> 32; |
| 113 | const sign = i32(hx >> 31); | |
| 113 | const sign = @intCast(i32, hx >> 31); | |
| 114 | 114 | hx &= 0x7FFFFFFF; |
| 115 | 115 | |
| 116 | 116 | if (math.isNan(x)) { |
| ... | ... | @@ -148,12 +148,12 @@ fn exp64(x_: f64) f64 { |
| 148 | 148 | if (hx > 0x3EB17218) { |
| 149 | 149 | // |x| >= 1.5 * ln2 |
| 150 | 150 | if (hx > 0x3FF0A2B2) { |
| 151 | k = i32(invln2 * x + half[usize(sign)]); | |
| 151 | k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]); | |
| 152 | 152 | } else { |
| 153 | 153 | k = 1 - sign - sign; |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | const dk = f64(k); | |
| 156 | const dk = @intToFloat(f64, k); | |
| 157 | 157 | hi = x - dk * ln2hi; |
| 158 | 158 | lo = dk * ln2lo; |
| 159 | 159 | x = hi - lo; |
std/math/exp2.zig+7-7| ... | ... | @@ -38,8 +38,8 @@ const exp2ft = []const f64{ |
| 38 | 38 | fn exp2_32(x: f32) f32 { |
| 39 | 39 | @setFloatMode(this, @import("builtin").FloatMode.Strict); |
| 40 | 40 | |
| 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); | |
| 43 | 43 | const P1: f32 = 0x1.62e430p-1; |
| 44 | 44 | const P2: f32 = 0x1.ebfbe0p-3; |
| 45 | 45 | const P3: f32 = 0x1.c6b348p-5; |
| ... | ... | @@ -89,7 +89,7 @@ fn exp2_32(x: f32) f32 { |
| 89 | 89 | var r: f64 = exp2ft[i0]; |
| 90 | 90 | const t: f64 = r * z; |
| 91 | 91 | r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4); |
| 92 | return f32(r * uk); | |
| 92 | return @floatCast(f32, r * uk); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | const exp2dt = []f64{ |
| ... | ... | @@ -355,8 +355,8 @@ const exp2dt = []f64{ |
| 355 | 355 | fn exp2_64(x: f64) f64 { |
| 356 | 356 | @setFloatMode(this, @import("builtin").FloatMode.Strict); |
| 357 | 357 | |
| 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); | |
| 360 | 360 | const P1: f64 = 0x1.62e42fefa39efp-1; |
| 361 | 361 | const P2: f64 = 0x1.ebfbdff82c575p-3; |
| 362 | 362 | const P3: f64 = 0x1.c6b08d704a0a6p-5; |
| ... | ... | @@ -364,7 +364,7 @@ fn exp2_64(x: f64) f64 { |
| 364 | 364 | const P5: f64 = 0x1.5d88003875c74p-10; |
| 365 | 365 | |
| 366 | 366 | const ux = @bitCast(u64, x); |
| 367 | const ix = u32(ux >> 32) & 0x7FFFFFFF; | |
| 367 | const ix = @intCast(u32, ux >> 32) & 0x7FFFFFFF; | |
| 368 | 368 | |
| 369 | 369 | // TODO: This should be handled beneath. |
| 370 | 370 | if (math.isNan(x)) { |
| ... | ... | @@ -386,7 +386,7 @@ fn exp2_64(x: f64) f64 { |
| 386 | 386 | if (ux >> 63 != 0) { |
| 387 | 387 | // underflow |
| 388 | 388 | 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)); | |
| 390 | 390 | } |
| 391 | 391 | if (x <= -1075) { |
| 392 | 392 | return 0; |
std/math/expm1.zig+10-10| ... | ... | @@ -78,8 +78,8 @@ fn expm1_32(x_: f32) f32 { |
| 78 | 78 | kf += 0.5; |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | k = i32(kf); | |
| 82 | const t = f32(k); | |
| 81 | k = @floatToInt(i32, kf); | |
| 82 | const t = @intToFloat(f32, k); | |
| 83 | 83 | hi = x - t * ln2_hi; |
| 84 | 84 | lo = t * ln2_lo; |
| 85 | 85 | } |
| ... | ... | @@ -123,7 +123,7 @@ fn expm1_32(x_: f32) f32 { |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | const twopk = @bitCast(f32, u32((0x7F +% k) << 23)); | |
| 126 | const twopk = @bitCast(f32, @intCast(u32, (0x7F +% k) << 23)); | |
| 127 | 127 | |
| 128 | 128 | if (k < 0 or k > 56) { |
| 129 | 129 | var y = x - e + 1.0; |
| ... | ... | @@ -136,7 +136,7 @@ fn expm1_32(x_: f32) f32 { |
| 136 | 136 | return y - 1.0; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | const uf = @bitCast(f32, u32(0x7F -% k) << 23); | |
| 139 | const uf = @bitCast(f32, @intCast(u32, 0x7F -% k) << 23); | |
| 140 | 140 | if (k < 23) { |
| 141 | 141 | return (x - e + (1 - uf)) * twopk; |
| 142 | 142 | } else { |
| ... | ... | @@ -158,7 +158,7 @@ fn expm1_64(x_: f64) f64 { |
| 158 | 158 | |
| 159 | 159 | var x = x_; |
| 160 | 160 | const ux = @bitCast(u64, x); |
| 161 | const hx = u32(ux >> 32) & 0x7FFFFFFF; | |
| 161 | const hx = @intCast(u32, ux >> 32) & 0x7FFFFFFF; | |
| 162 | 162 | const sign = ux >> 63; |
| 163 | 163 | |
| 164 | 164 | if (math.isNegativeInf(x)) { |
| ... | ... | @@ -207,8 +207,8 @@ fn expm1_64(x_: f64) f64 { |
| 207 | 207 | kf += 0.5; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | k = i32(kf); | |
| 211 | const t = f64(k); | |
| 210 | k = @floatToInt(i32, kf); | |
| 211 | const t = @intToFloat(f64, k); | |
| 212 | 212 | hi = x - t * ln2_hi; |
| 213 | 213 | lo = t * ln2_lo; |
| 214 | 214 | } |
| ... | ... | @@ -219,7 +219,7 @@ fn expm1_64(x_: f64) f64 { |
| 219 | 219 | // |x| < 2^(-54) |
| 220 | 220 | else if (hx < 0x3C900000) { |
| 221 | 221 | if (hx < 0x00100000) { |
| 222 | math.forceEval(f32(x)); | |
| 222 | math.forceEval(@floatCast(f32, x)); | |
| 223 | 223 | } |
| 224 | 224 | return x; |
| 225 | 225 | } else { |
| ... | ... | @@ -252,7 +252,7 @@ fn expm1_64(x_: f64) f64 { |
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | const twopk = @bitCast(f64, u64(0x3FF +% k) << 52); | |
| 255 | const twopk = @bitCast(f64, @intCast(u64, 0x3FF +% k) << 52); | |
| 256 | 256 | |
| 257 | 257 | if (k < 0 or k > 56) { |
| 258 | 258 | var y = x - e + 1.0; |
| ... | ... | @@ -265,7 +265,7 @@ fn expm1_64(x_: f64) f64 { |
| 265 | 265 | return y - 1.0; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | const uf = @bitCast(f64, u64(0x3FF -% k) << 52); | |
| 268 | const uf = @bitCast(f64, @intCast(u64, 0x3FF -% k) << 52); | |
| 269 | 269 | if (k < 20) { |
| 270 | 270 | return (x - e + (1 - uf)) * twopk; |
| 271 | 271 | } else { |
std/math/floor.zig+2-2| ... | ... | @@ -20,7 +20,7 @@ pub fn floor(x: var) @typeOf(x) { |
| 20 | 20 | |
| 21 | 21 | fn floor32(x: f32) f32 { |
| 22 | 22 | var u = @bitCast(u32, x); |
| 23 | const e = i32((u >> 23) & 0xFF) - 0x7F; | |
| 23 | const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F; | |
| 24 | 24 | var m: u32 = undefined; |
| 25 | 25 | |
| 26 | 26 | // TODO: Shouldn't need this explicit check. |
| ... | ... | @@ -33,7 +33,7 @@ fn floor32(x: f32) f32 { |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | if (e >= 0) { |
| 36 | m = u32(0x007FFFFF) >> u5(e); | |
| 36 | m = u32(0x007FFFFF) >> @intCast(u5, e); | |
| 37 | 37 | if (u & m == 0) { |
| 38 | 38 | return x; |
| 39 | 39 | } |
std/math/fma.zig+3-3| ... | ... | @@ -17,10 +17,10 @@ fn fma32(x: f32, y: f32, z: f32) f32 { |
| 17 | 17 | const e = (u >> 52) & 0x7FF; |
| 18 | 18 | |
| 19 | 19 | if ((u & 0x1FFFFFFF) != 0x10000000 or e == 0x7FF or xy_z - xy == z) { |
| 20 | return f32(xy_z); | |
| 20 | return @floatCast(f32, xy_z); | |
| 21 | 21 | } else { |
| 22 | 22 | // TODO: Handle inexact case with double-rounding |
| 23 | return f32(xy_z); | |
| 23 | return @floatCast(f32, xy_z); | |
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| ... | ... | @@ -124,7 +124,7 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 { |
| 124 | 124 | var sum = dd_add(a, b); |
| 125 | 125 | if (sum.lo != 0) { |
| 126 | 126 | 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; | |
| 128 | 128 | if ((bits_lost != 1) == (uhii & 1 != 0)) { |
| 129 | 129 | const uloi = @bitCast(u64, sum.lo); |
| 130 | 130 | uhii += 1 - (((uhii ^ uloi) >> 62) & 2); |
std/math/frexp.zig+2-2| ... | ... | @@ -30,7 +30,7 @@ fn frexp32(x: f32) frexp32_result { |
| 30 | 30 | var result: frexp32_result = undefined; |
| 31 | 31 | |
| 32 | 32 | var y = @bitCast(u32, x); |
| 33 | const e = i32(y >> 23) & 0xFF; | |
| 33 | const e = @intCast(i32, y >> 23) & 0xFF; | |
| 34 | 34 | |
| 35 | 35 | if (e == 0) { |
| 36 | 36 | if (x != 0) { |
| ... | ... | @@ -67,7 +67,7 @@ fn frexp64(x: f64) frexp64_result { |
| 67 | 67 | var result: frexp64_result = undefined; |
| 68 | 68 | |
| 69 | 69 | var y = @bitCast(u64, x); |
| 70 | const e = i32(y >> 52) & 0x7FF; | |
| 70 | const e = @intCast(i32, y >> 52) & 0x7FF; | |
| 71 | 71 | |
| 72 | 72 | if (e == 0) { |
| 73 | 73 | if (x != 0) { |
std/math/hypot.zig+1-1| ... | ... | @@ -49,7 +49,7 @@ fn hypot32(x: f32, y: f32) f32 { |
| 49 | 49 | yy *= 0x1.0p-90; |
| 50 | 50 | } |
| 51 | 51 | |
| 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)); | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | fn sq(hi: *f64, lo: *f64, x: f64) void { |
std/math/ilogb.zig+2-2| ... | ... | @@ -23,7 +23,7 @@ const fp_ilogb0 = fp_ilogbnan; |
| 23 | 23 | |
| 24 | 24 | fn ilogb32(x: f32) i32 { |
| 25 | 25 | var u = @bitCast(u32, x); |
| 26 | var e = i32((u >> 23) & 0xFF); | |
| 26 | var e = @intCast(i32, (u >> 23) & 0xFF); | |
| 27 | 27 | |
| 28 | 28 | // TODO: We should be able to merge this with the lower check. |
| 29 | 29 | if (math.isNan(x)) { |
| ... | ... | @@ -59,7 +59,7 @@ fn ilogb32(x: f32) i32 { |
| 59 | 59 | |
| 60 | 60 | fn ilogb64(x: f64) i32 { |
| 61 | 61 | var u = @bitCast(u64, x); |
| 62 | var e = i32((u >> 52) & 0x7FF); | |
| 62 | var e = @intCast(i32, (u >> 52) & 0x7FF); | |
| 63 | 63 | |
| 64 | 64 | if (math.isNan(x)) { |
| 65 | 65 | 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 { |
| 227 | 227 | /// A negative shift amount results in a right shift. |
| 228 | 228 | pub fn shl(comptime T: type, a: T, shift_amt: var) T { |
| 229 | 229 | 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); | |
| 231 | 231 | |
| 232 | 232 | if (@typeOf(shift_amt).is_signed) { |
| 233 | 233 | if (shift_amt >= 0) { |
| ... | ... | @@ -251,7 +251,7 @@ test "math.shl" { |
| 251 | 251 | /// A negative shift amount results in a lefft shift. |
| 252 | 252 | pub fn shr(comptime T: type, a: T, shift_amt: var) T { |
| 253 | 253 | 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); | |
| 255 | 255 | |
| 256 | 256 | if (@typeOf(shift_amt).is_signed) { |
| 257 | 257 | if (shift_amt >= 0) { |
| ... | ... | @@ -473,9 +473,9 @@ fn testRem() void { |
| 473 | 473 | /// Result is an unsigned integer. |
| 474 | 474 | pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) { |
| 475 | 475 | const uint = @IntType(false, @typeOf(x).bit_count); |
| 476 | if (x >= 0) return uint(x); | |
| 476 | if (x >= 0) return @intCast(uint, x); | |
| 477 | 477 | |
| 478 | return uint(-(x + 1)) + 1; | |
| 478 | return @intCast(uint, -(x + 1)) + 1; | |
| 479 | 479 | } |
| 480 | 480 | |
| 481 | 481 | test "math.absCast" { |
| ... | ... | @@ -499,7 +499,7 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) { |
| 499 | 499 | |
| 500 | 500 | if (x == -@minValue(int)) return @minValue(int); |
| 501 | 501 | |
| 502 | return -int(x); | |
| 502 | return -@intCast(int, x); | |
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | test "math.negateCast" { |
| ... | ... | @@ -522,7 +522,7 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) { |
| 522 | 522 | } else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) { |
| 523 | 523 | return error.Overflow; |
| 524 | 524 | } else { |
| 525 | return T(x); | |
| 525 | return @intCast(T, x); | |
| 526 | 526 | } |
| 527 | 527 | } |
| 528 | 528 | |
| ... | ... | @@ -565,7 +565,7 @@ test "math.floorPowerOfTwo" { |
| 565 | 565 | |
| 566 | 566 | pub fn log2_int(comptime T: type, x: T) Log2Int(T) { |
| 567 | 567 | assert(x != 0); |
| 568 | return Log2Int(T)(T.bit_count - 1 - @clz(x)); | |
| 568 | return @intCast(Log2Int(T), T.bit_count - 1 - @clz(x)); | |
| 569 | 569 | } |
| 570 | 570 | |
| 571 | 571 | pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) { |
| ... | ... | @@ -597,3 +597,14 @@ fn testFloorPowerOfTwo() void { |
| 597 | 597 | assert(floorPowerOfTwo(u4, 8) == 8); |
| 598 | 598 | assert(floorPowerOfTwo(u4, 9) == 8); |
| 599 | 599 | } |
| 600 | ||
| 601 | pub 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 { |
| 71 | 71 | |
| 72 | 72 | // x into [sqrt(2) / 2, sqrt(2)] |
| 73 | 73 | ix += 0x3F800000 - 0x3F3504F3; |
| 74 | k += i32(ix >> 23) - 0x7F; | |
| 74 | k += @intCast(i32, ix >> 23) - 0x7F; | |
| 75 | 75 | ix = (ix & 0x007FFFFF) + 0x3F3504F3; |
| 76 | 76 | x = @bitCast(f32, ix); |
| 77 | 77 | |
| ... | ... | @@ -83,7 +83,7 @@ pub fn ln_32(x_: f32) f32 { |
| 83 | 83 | const t2 = z * (Lg1 + w * Lg3); |
| 84 | 84 | const R = t2 + t1; |
| 85 | 85 | const hfsq = 0.5 * f * f; |
| 86 | const dk = f32(k); | |
| 86 | const dk = @intToFloat(f32, k); | |
| 87 | 87 | |
| 88 | 88 | return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; |
| 89 | 89 | } |
| ... | ... | @@ -103,7 +103,7 @@ pub fn ln_64(x_: f64) f64 { |
| 103 | 103 | |
| 104 | 104 | var x = x_; |
| 105 | 105 | var ix = @bitCast(u64, x); |
| 106 | var hx = u32(ix >> 32); | |
| 106 | var hx = @intCast(u32, ix >> 32); | |
| 107 | 107 | var k: i32 = 0; |
| 108 | 108 | |
| 109 | 109 | if (hx < 0x00100000 or hx >> 31 != 0) { |
| ... | ... | @@ -119,7 +119,7 @@ pub fn ln_64(x_: f64) f64 { |
| 119 | 119 | // subnormal, scale x |
| 120 | 120 | k -= 54; |
| 121 | 121 | x *= 0x1.0p54; |
| 122 | hx = u32(@bitCast(u64, ix) >> 32); | |
| 122 | hx = @intCast(u32, @bitCast(u64, ix) >> 32); | |
| 123 | 123 | } else if (hx >= 0x7FF00000) { |
| 124 | 124 | return x; |
| 125 | 125 | } else if (hx == 0x3FF00000 and ix << 32 == 0) { |
| ... | ... | @@ -128,7 +128,7 @@ pub fn ln_64(x_: f64) f64 { |
| 128 | 128 | |
| 129 | 129 | // x into [sqrt(2) / 2, sqrt(2)] |
| 130 | 130 | hx += 0x3FF00000 - 0x3FE6A09E; |
| 131 | k += i32(hx >> 20) - 0x3FF; | |
| 131 | k += @intCast(i32, hx >> 20) - 0x3FF; | |
| 132 | 132 | hx = (hx & 0x000FFFFF) + 0x3FE6A09E; |
| 133 | 133 | ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF); |
| 134 | 134 | x = @bitCast(f64, ix); |
| ... | ... | @@ -141,7 +141,7 @@ pub fn ln_64(x_: f64) f64 { |
| 141 | 141 | const t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); |
| 142 | 142 | const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); |
| 143 | 143 | const R = t2 + t1; |
| 144 | const dk = f64(k); | |
| 144 | const dk = @intToFloat(f64, k); | |
| 145 | 145 | |
| 146 | 146 | return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; |
| 147 | 147 | } |
std/math/log.zig+6-5| ... | ... | @@ -13,22 +13,23 @@ pub fn log(comptime T: type, base: T, x: T) T { |
| 13 | 13 | return math.ln(x); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | const float_base = math.lossyCast(f64, base); | |
| 16 | 17 | switch (@typeId(T)) { |
| 17 | 18 | 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)); | |
| 19 | 20 | }, |
| 20 | 21 | 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))); | |
| 22 | 23 | }, |
| 23 | 24 | builtin.TypeId.Int => { |
| 24 | 25 | // 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))); | |
| 26 | 27 | }, |
| 27 | 28 | |
| 28 | 29 | builtin.TypeId.Float => { |
| 29 | 30 | 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), | |
| 32 | 33 | else => @compileError("log not implemented for " ++ @typeName(T)), |
| 33 | 34 | } |
| 34 | 35 | }, |
std/math/log10.zig+7-7| ... | ... | @@ -28,7 +28,7 @@ pub fn log10(x: var) @typeOf(x) { |
| 28 | 28 | return @typeOf(1)(math.floor(log10_64(f64(x)))); |
| 29 | 29 | }, |
| 30 | 30 | TypeId.Int => { |
| 31 | return T(math.floor(log10_64(f64(x)))); | |
| 31 | return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))); | |
| 32 | 32 | }, |
| 33 | 33 | else => @compileError("log10 not implemented for " ++ @typeName(T)), |
| 34 | 34 | } |
| ... | ... | @@ -71,7 +71,7 @@ pub fn log10_32(x_: f32) f32 { |
| 71 | 71 | |
| 72 | 72 | // x into [sqrt(2) / 2, sqrt(2)] |
| 73 | 73 | ix += 0x3F800000 - 0x3F3504F3; |
| 74 | k += i32(ix >> 23) - 0x7F; | |
| 74 | k += @intCast(i32, ix >> 23) - 0x7F; | |
| 75 | 75 | ix = (ix & 0x007FFFFF) + 0x3F3504F3; |
| 76 | 76 | x = @bitCast(f32, ix); |
| 77 | 77 | |
| ... | ... | @@ -89,7 +89,7 @@ pub fn log10_32(x_: f32) f32 { |
| 89 | 89 | u &= 0xFFFFF000; |
| 90 | 90 | hi = @bitCast(f32, u); |
| 91 | 91 | const lo = f - hi - hfsq + s * (hfsq + R); |
| 92 | const dk = f32(k); | |
| 92 | const dk = @intToFloat(f32, k); | |
| 93 | 93 | |
| 94 | 94 | return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi; |
| 95 | 95 | } |
| ... | ... | @@ -109,7 +109,7 @@ pub fn log10_64(x_: f64) f64 { |
| 109 | 109 | |
| 110 | 110 | var x = x_; |
| 111 | 111 | var ix = @bitCast(u64, x); |
| 112 | var hx = u32(ix >> 32); | |
| 112 | var hx = @intCast(u32, ix >> 32); | |
| 113 | 113 | var k: i32 = 0; |
| 114 | 114 | |
| 115 | 115 | if (hx < 0x00100000 or hx >> 31 != 0) { |
| ... | ... | @@ -125,7 +125,7 @@ pub fn log10_64(x_: f64) f64 { |
| 125 | 125 | // subnormal, scale x |
| 126 | 126 | k -= 54; |
| 127 | 127 | x *= 0x1.0p54; |
| 128 | hx = u32(@bitCast(u64, x) >> 32); | |
| 128 | hx = @intCast(u32, @bitCast(u64, x) >> 32); | |
| 129 | 129 | } else if (hx >= 0x7FF00000) { |
| 130 | 130 | return x; |
| 131 | 131 | } else if (hx == 0x3FF00000 and ix << 32 == 0) { |
| ... | ... | @@ -134,7 +134,7 @@ pub fn log10_64(x_: f64) f64 { |
| 134 | 134 | |
| 135 | 135 | // x into [sqrt(2) / 2, sqrt(2)] |
| 136 | 136 | hx += 0x3FF00000 - 0x3FE6A09E; |
| 137 | k += i32(hx >> 20) - 0x3FF; | |
| 137 | k += @intCast(i32, hx >> 20) - 0x3FF; | |
| 138 | 138 | hx = (hx & 0x000FFFFF) + 0x3FE6A09E; |
| 139 | 139 | ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF); |
| 140 | 140 | x = @bitCast(f64, ix); |
| ... | ... | @@ -157,7 +157,7 @@ pub fn log10_64(x_: f64) f64 { |
| 157 | 157 | |
| 158 | 158 | // val_hi + val_lo ~ log10(1 + f) + k * log10(2) |
| 159 | 159 | var val_hi = hi * ivln10hi; |
| 160 | const dk = f64(k); | |
| 160 | const dk = @intToFloat(f64, k); | |
| 161 | 161 | const y = dk * log10_2hi; |
| 162 | 162 | var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi; |
| 163 | 163 |
std/math/log1p.zig+6-6| ... | ... | @@ -68,7 +68,7 @@ fn log1p_32(x: f32) f32 { |
| 68 | 68 | const uf = 1 + x; |
| 69 | 69 | var iu = @bitCast(u32, uf); |
| 70 | 70 | iu += 0x3F800000 - 0x3F3504F3; |
| 71 | k = i32(iu >> 23) - 0x7F; | |
| 71 | k = @intCast(i32, iu >> 23) - 0x7F; | |
| 72 | 72 | |
| 73 | 73 | // correction to avoid underflow in c / u |
| 74 | 74 | if (k < 25) { |
| ... | ... | @@ -90,7 +90,7 @@ fn log1p_32(x: f32) f32 { |
| 90 | 90 | const t2 = z * (Lg1 + w * Lg3); |
| 91 | 91 | const R = t2 + t1; |
| 92 | 92 | const hfsq = 0.5 * f * f; |
| 93 | const dk = f32(k); | |
| 93 | const dk = @intToFloat(f32, k); | |
| 94 | 94 | |
| 95 | 95 | return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; |
| 96 | 96 | } |
| ... | ... | @@ -107,7 +107,7 @@ fn log1p_64(x: f64) f64 { |
| 107 | 107 | const Lg7: f64 = 1.479819860511658591e-01; |
| 108 | 108 | |
| 109 | 109 | var ix = @bitCast(u64, x); |
| 110 | var hx = u32(ix >> 32); | |
| 110 | var hx = @intCast(u32, ix >> 32); | |
| 111 | 111 | var k: i32 = 1; |
| 112 | 112 | var c: f64 = undefined; |
| 113 | 113 | var f: f64 = undefined; |
| ... | ... | @@ -145,9 +145,9 @@ fn log1p_64(x: f64) f64 { |
| 145 | 145 | if (k != 0) { |
| 146 | 146 | const uf = 1 + x; |
| 147 | 147 | const hu = @bitCast(u64, uf); |
| 148 | var iu = u32(hu >> 32); | |
| 148 | var iu = @intCast(u32, hu >> 32); | |
| 149 | 149 | iu += 0x3FF00000 - 0x3FE6A09E; |
| 150 | k = i32(iu >> 20) - 0x3FF; | |
| 150 | k = @intCast(i32, iu >> 20) - 0x3FF; | |
| 151 | 151 | |
| 152 | 152 | // correction to avoid underflow in c / u |
| 153 | 153 | if (k < 54) { |
| ... | ... | @@ -170,7 +170,7 @@ fn log1p_64(x: f64) f64 { |
| 170 | 170 | const t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); |
| 171 | 171 | const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); |
| 172 | 172 | const R = t2 + t1; |
| 173 | const dk = f64(k); | |
| 173 | const dk = @intToFloat(f64, k); | |
| 174 | 174 | |
| 175 | 175 | return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; |
| 176 | 176 | } |
std/math/log2.zig+6-6| ... | ... | @@ -75,7 +75,7 @@ pub fn log2_32(x_: f32) f32 { |
| 75 | 75 | |
| 76 | 76 | // x into [sqrt(2) / 2, sqrt(2)] |
| 77 | 77 | ix += 0x3F800000 - 0x3F3504F3; |
| 78 | k += i32(ix >> 23) - 0x7F; | |
| 78 | k += @intCast(i32, ix >> 23) - 0x7F; | |
| 79 | 79 | ix = (ix & 0x007FFFFF) + 0x3F3504F3; |
| 80 | 80 | x = @bitCast(f32, ix); |
| 81 | 81 | |
| ... | ... | @@ -93,7 +93,7 @@ pub fn log2_32(x_: f32) f32 { |
| 93 | 93 | u &= 0xFFFFF000; |
| 94 | 94 | hi = @bitCast(f32, u); |
| 95 | 95 | 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); | |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | pub fn log2_64(x_: f64) f64 { |
| ... | ... | @@ -109,7 +109,7 @@ pub fn log2_64(x_: f64) f64 { |
| 109 | 109 | |
| 110 | 110 | var x = x_; |
| 111 | 111 | var ix = @bitCast(u64, x); |
| 112 | var hx = u32(ix >> 32); | |
| 112 | var hx = @intCast(u32, ix >> 32); | |
| 113 | 113 | var k: i32 = 0; |
| 114 | 114 | |
| 115 | 115 | if (hx < 0x00100000 or hx >> 31 != 0) { |
| ... | ... | @@ -125,7 +125,7 @@ pub fn log2_64(x_: f64) f64 { |
| 125 | 125 | // subnormal, scale x |
| 126 | 126 | k -= 54; |
| 127 | 127 | x *= 0x1.0p54; |
| 128 | hx = u32(@bitCast(u64, x) >> 32); | |
| 128 | hx = @intCast(u32, @bitCast(u64, x) >> 32); | |
| 129 | 129 | } else if (hx >= 0x7FF00000) { |
| 130 | 130 | return x; |
| 131 | 131 | } else if (hx == 0x3FF00000 and ix << 32 == 0) { |
| ... | ... | @@ -134,7 +134,7 @@ pub fn log2_64(x_: f64) f64 { |
| 134 | 134 | |
| 135 | 135 | // x into [sqrt(2) / 2, sqrt(2)] |
| 136 | 136 | hx += 0x3FF00000 - 0x3FE6A09E; |
| 137 | k += i32(hx >> 20) - 0x3FF; | |
| 137 | k += @intCast(i32, hx >> 20) - 0x3FF; | |
| 138 | 138 | hx = (hx & 0x000FFFFF) + 0x3FE6A09E; |
| 139 | 139 | ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF); |
| 140 | 140 | x = @bitCast(f64, ix); |
| ... | ... | @@ -159,7 +159,7 @@ pub fn log2_64(x_: f64) f64 { |
| 159 | 159 | var val_lo = (lo + hi) * ivln2lo + lo * ivln2hi; |
| 160 | 160 | |
| 161 | 161 | // spadd(val_hi, val_lo, y) |
| 162 | const y = f64(k); | |
| 162 | const y = @intToFloat(f64, k); | |
| 163 | 163 | const ww = y + val_hi; |
| 164 | 164 | val_lo += (y - ww) + val_hi; |
| 165 | 165 | val_hi = ww; |
std/math/modf.zig+4-4| ... | ... | @@ -29,7 +29,7 @@ fn modf32(x: f32) modf32_result { |
| 29 | 29 | var result: modf32_result = undefined; |
| 30 | 30 | |
| 31 | 31 | const u = @bitCast(u32, x); |
| 32 | const e = i32((u >> 23) & 0xFF) - 0x7F; | |
| 32 | const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F; | |
| 33 | 33 | const us = u & 0x80000000; |
| 34 | 34 | |
| 35 | 35 | // TODO: Shouldn't need this. |
| ... | ... | @@ -57,7 +57,7 @@ fn modf32(x: f32) modf32_result { |
| 57 | 57 | return result; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | const mask = u32(0x007FFFFF) >> u5(e); | |
| 60 | const mask = u32(0x007FFFFF) >> @intCast(u5, e); | |
| 61 | 61 | if (u & mask == 0) { |
| 62 | 62 | result.ipart = x; |
| 63 | 63 | result.fpart = @bitCast(f32, us); |
| ... | ... | @@ -74,7 +74,7 @@ fn modf64(x: f64) modf64_result { |
| 74 | 74 | var result: modf64_result = undefined; |
| 75 | 75 | |
| 76 | 76 | const u = @bitCast(u64, x); |
| 77 | const e = i32((u >> 52) & 0x7FF) - 0x3FF; | |
| 77 | const e = @intCast(i32, (u >> 52) & 0x7FF) - 0x3FF; | |
| 78 | 78 | const us = u & (1 << 63); |
| 79 | 79 | |
| 80 | 80 | if (math.isInf(x)) { |
| ... | ... | @@ -101,7 +101,7 @@ fn modf64(x: f64) modf64_result { |
| 101 | 101 | return result; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | const mask = u64(@maxValue(u64) >> 12) >> u6(e); | |
| 104 | const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e); | |
| 105 | 105 | if (u & mask == 0) { |
| 106 | 106 | result.ipart = x; |
| 107 | 107 | 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 { |
| 146 | 146 | var xe = r2.exponent; |
| 147 | 147 | var x1 = r2.significand; |
| 148 | 148 | |
| 149 | var i = i32(yi); | |
| 149 | var i = @floatToInt(i32, yi); | |
| 150 | 150 | while (i != 0) : (i >>= 1) { |
| 151 | 151 | if (i & 1 == 1) { |
| 152 | 152 | a1 *= x1; |
| ... | ... | @@ -171,7 +171,7 @@ pub fn pow(comptime T: type, x: T, y: T) T { |
| 171 | 171 | |
| 172 | 172 | fn isOddInteger(x: f64) bool { |
| 173 | 173 | 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; | |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | test "math.pow" { |
std/math/scalbn.zig+2-2| ... | ... | @@ -37,7 +37,7 @@ fn scalbn32(x: f32, n_: i32) f32 { |
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | const u = u32(n +% 0x7F) << 23; | |
| 40 | const u = @intCast(u32, n +% 0x7F) << 23; | |
| 41 | 41 | return y * @bitCast(f32, u); |
| 42 | 42 | } |
| 43 | 43 | |
| ... | ... | @@ -67,7 +67,7 @@ fn scalbn64(x: f64, n_: i32) f64 { |
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | const u = u64(n +% 0x3FF) << 52; | |
| 70 | const u = @intCast(u64, n +% 0x3FF) << 52; | |
| 71 | 71 | return y * @bitCast(f64, u); |
| 72 | 72 | } |
| 73 | 73 |
std/math/sin.zig+2-2| ... | ... | @@ -60,7 +60,7 @@ fn sin32(x_: f32) f32 { |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | var y = math.floor(x * m4pi); |
| 63 | var j = i64(y); | |
| 63 | var j = @floatToInt(i64, y); | |
| 64 | 64 | |
| 65 | 65 | if (j & 1 == 1) { |
| 66 | 66 | j += 1; |
| ... | ... | @@ -112,7 +112,7 @@ fn sin64(x_: f64) f64 { |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | var y = math.floor(x * m4pi); |
| 115 | var j = i64(y); | |
| 115 | var j = @floatToInt(i64, y); | |
| 116 | 116 | |
| 117 | 117 | if (j & 1 == 1) { |
| 118 | 118 | j += 1; |
std/math/sinh.zig+1-1| ... | ... | @@ -57,7 +57,7 @@ fn sinh64(x: f64) f64 { |
| 57 | 57 | @setFloatMode(this, @import("builtin").FloatMode.Strict); |
| 58 | 58 | |
| 59 | 59 | const u = @bitCast(u64, x); |
| 60 | const w = u32(u >> 32); | |
| 60 | const w = @intCast(u32, u >> 32); | |
| 61 | 61 | const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); |
| 62 | 62 | |
| 63 | 63 | 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) { |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | const ResultType = @IntType(false, T.bit_count / 2); |
| 102 | return ResultType(res); | |
| 102 | return @intCast(ResultType, res); | |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | test "math.sqrt_int" { |
std/math/tan.zig+2-2| ... | ... | @@ -53,7 +53,7 @@ fn tan32(x_: f32) f32 { |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | var y = math.floor(x * m4pi); |
| 56 | var j = i64(y); | |
| 56 | var j = @floatToInt(i64, y); | |
| 57 | 57 | |
| 58 | 58 | if (j & 1 == 1) { |
| 59 | 59 | j += 1; |
| ... | ... | @@ -102,7 +102,7 @@ fn tan64(x_: f64) f64 { |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | var y = math.floor(x * m4pi); |
| 105 | var j = i64(y); | |
| 105 | var j = @floatToInt(i64, y); | |
| 106 | 106 | |
| 107 | 107 | if (j & 1 == 1) { |
| 108 | 108 | j += 1; |
std/math/tanh.zig+2-2| ... | ... | @@ -68,7 +68,7 @@ fn tanh32(x: f32) f32 { |
| 68 | 68 | |
| 69 | 69 | fn tanh64(x: f64) f64 { |
| 70 | 70 | const u = @bitCast(u64, x); |
| 71 | const w = u32(u >> 32); | |
| 71 | const w = @intCast(u32, u >> 32); | |
| 72 | 72 | const ax = @bitCast(f64, u & (@maxValue(u64) >> 1)); |
| 73 | 73 | |
| 74 | 74 | var t: f64 = undefined; |
| ... | ... | @@ -100,7 +100,7 @@ fn tanh64(x: f64) f64 { |
| 100 | 100 | } |
| 101 | 101 | // |x| is subnormal |
| 102 | 102 | else { |
| 103 | math.forceEval(f32(x)); | |
| 103 | math.forceEval(@floatCast(f32, x)); | |
| 104 | 104 | t = x; |
| 105 | 105 | } |
| 106 | 106 |
std/math/trunc.zig+4-4| ... | ... | @@ -19,7 +19,7 @@ pub fn trunc(x: var) @typeOf(x) { |
| 19 | 19 | |
| 20 | 20 | fn trunc32(x: f32) f32 { |
| 21 | 21 | const u = @bitCast(u32, x); |
| 22 | var e = i32(((u >> 23) & 0xFF)) - 0x7F + 9; | |
| 22 | var e = @intCast(i32, ((u >> 23) & 0xFF)) - 0x7F + 9; | |
| 23 | 23 | var m: u32 = undefined; |
| 24 | 24 | |
| 25 | 25 | if (e >= 23 + 9) { |
| ... | ... | @@ -29,7 +29,7 @@ fn trunc32(x: f32) f32 { |
| 29 | 29 | e = 1; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | m = u32(@maxValue(u32)) >> u5(e); | |
| 32 | m = u32(@maxValue(u32)) >> @intCast(u5, e); | |
| 33 | 33 | if (u & m == 0) { |
| 34 | 34 | return x; |
| 35 | 35 | } else { |
| ... | ... | @@ -40,7 +40,7 @@ fn trunc32(x: f32) f32 { |
| 40 | 40 | |
| 41 | 41 | fn trunc64(x: f64) f64 { |
| 42 | 42 | const u = @bitCast(u64, x); |
| 43 | var e = i32(((u >> 52) & 0x7FF)) - 0x3FF + 12; | |
| 43 | var e = @intCast(i32, ((u >> 52) & 0x7FF)) - 0x3FF + 12; | |
| 44 | 44 | var m: u64 = undefined; |
| 45 | 45 | |
| 46 | 46 | if (e >= 52 + 12) { |
| ... | ... | @@ -50,7 +50,7 @@ fn trunc64(x: f64) f64 { |
| 50 | 50 | e = 1; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | m = u64(@maxValue(u64)) >> u6(e); | |
| 53 | m = u64(@maxValue(u64)) >> @intCast(u6, e); | |
| 54 | 54 | if (u & m == 0) { |
| 55 | 55 | return x; |
| 56 | 56 | } else { |
std/mem.zig+1-1| ... | ... | @@ -334,7 +334,7 @@ pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) T { |
| 334 | 334 | builtin.Endian.Little => { |
| 335 | 335 | const ShiftType = math.Log2Int(T); |
| 336 | 336 | for (bytes) |b, index| { |
| 337 | result = result | (T(b) << ShiftType(index * 8)); | |
| 337 | result = result | (T(b) << @intCast(ShiftType, index * 8)); | |
| 338 | 338 | } |
| 339 | 339 | }, |
| 340 | 340 | } |
std/os/child_process.zig+1-1| ... | ... | @@ -413,7 +413,7 @@ pub const ChildProcess = struct { |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | // we are the parent |
| 416 | const pid = i32(pid_result); | |
| 416 | const pid = @intCast(i32, pid_result); | |
| 417 | 417 | if (self.stdin_behavior == StdIo.Pipe) { |
| 418 | 418 | self.stdin = os.File.openHandle(stdin_pipe[1]); |
| 419 | 419 | } else { |
std/os/darwin.zig+9-2| ... | ... | @@ -290,7 +290,7 @@ pub fn WIFSIGNALED(x: i32) bool { |
| 290 | 290 | /// Get the errno from a syscall return value, or 0 for no error. |
| 291 | 291 | pub fn getErrno(r: usize) usize { |
| 292 | 292 | 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; | |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | pub fn close(fd: i32) usize { |
| ... | ... | @@ -339,7 +339,14 @@ pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize { |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | pub 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 | ); | |
| 343 | 350 | const isize_result = @bitCast(isize, @ptrToInt(ptr_result)); |
| 344 | 351 | return errnoWrap(isize_result); |
| 345 | 352 | } |
std/os/file.zig+3-3| ... | ... | @@ -266,7 +266,7 @@ pub const File = struct { |
| 266 | 266 | pub fn getEndPos(self: *File) !usize { |
| 267 | 267 | if (is_posix) { |
| 268 | 268 | const stat = try os.posixFStat(self.handle); |
| 269 | return usize(stat.size); | |
| 269 | return @intCast(usize, stat.size); | |
| 270 | 270 | } else if (is_windows) { |
| 271 | 271 | var file_size: windows.LARGE_INTEGER = undefined; |
| 272 | 272 | if (windows.GetFileSizeEx(self.handle, &file_size) == 0) { |
| ... | ... | @@ -277,7 +277,7 @@ pub const File = struct { |
| 277 | 277 | } |
| 278 | 278 | if (file_size < 0) |
| 279 | 279 | return error.Overflow; |
| 280 | return math.cast(usize, u64(file_size)); | |
| 280 | return math.cast(usize, @intCast(u64, file_size)); | |
| 281 | 281 | } else { |
| 282 | 282 | @compileError("TODO support getEndPos on this OS"); |
| 283 | 283 | } |
| ... | ... | @@ -343,7 +343,7 @@ pub const File = struct { |
| 343 | 343 | } else if (is_windows) { |
| 344 | 344 | var index: usize = 0; |
| 345 | 345 | 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)); | |
| 347 | 347 | var amt_read: windows.DWORD = undefined; |
| 348 | 348 | if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) { |
| 349 | 349 | const err = windows.GetLastError(); |
std/os/index.zig+8-8| ... | ... | @@ -126,7 +126,7 @@ pub fn getRandomBytes(buf: []u8) !void { |
| 126 | 126 | } |
| 127 | 127 | defer _ = windows.CryptReleaseContext(hCryptProv, 0); |
| 128 | 128 | |
| 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) { | |
| 130 | 130 | const err = windows.GetLastError(); |
| 131 | 131 | return switch (err) { |
| 132 | 132 | else => unexpectedErrorWindows(err), |
| ... | ... | @@ -343,7 +343,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 { |
| 343 | 343 | else => return unexpectedErrorPosix(err), |
| 344 | 344 | } |
| 345 | 345 | } |
| 346 | return i32(result); | |
| 346 | return @intCast(i32, result); | |
| 347 | 347 | } |
| 348 | 348 | } |
| 349 | 349 | |
| ... | ... | @@ -586,7 +586,7 @@ pub fn getCwd(allocator: *Allocator) ![]u8 { |
| 586 | 586 | errdefer allocator.free(buf); |
| 587 | 587 | |
| 588 | 588 | 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); | |
| 590 | 590 | |
| 591 | 591 | if (result == 0) { |
| 592 | 592 | const err = windows.GetLastError(); |
| ... | ... | @@ -2019,7 +2019,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 { |
| 2019 | 2019 | const rc = posix.socket(domain, socket_type, protocol); |
| 2020 | 2020 | const err = posix.getErrno(rc); |
| 2021 | 2021 | switch (err) { |
| 2022 | 0 => return i32(rc), | |
| 2022 | 0 => return @intCast(i32, rc), | |
| 2023 | 2023 | posix.EACCES => return PosixSocketError.PermissionDenied, |
| 2024 | 2024 | posix.EAFNOSUPPORT => return PosixSocketError.AddressFamilyNotSupported, |
| 2025 | 2025 | posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable, |
| ... | ... | @@ -2183,7 +2183,7 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError! |
| 2183 | 2183 | const rc = posix.accept4(fd, addr, &sockaddr_size, flags); |
| 2184 | 2184 | const err = posix.getErrno(rc); |
| 2185 | 2185 | switch (err) { |
| 2186 | 0 => return i32(rc), | |
| 2186 | 0 => return @intCast(i32, rc), | |
| 2187 | 2187 | posix.EINTR => continue, |
| 2188 | 2188 | else => return unexpectedErrorPosix(err), |
| 2189 | 2189 | |
| ... | ... | @@ -2226,7 +2226,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 { |
| 2226 | 2226 | const rc = posix.epoll_create1(flags); |
| 2227 | 2227 | const err = posix.getErrno(rc); |
| 2228 | 2228 | switch (err) { |
| 2229 | 0 => return i32(rc), | |
| 2229 | 0 => return @intCast(i32, rc), | |
| 2230 | 2230 | else => return unexpectedErrorPosix(err), |
| 2231 | 2231 | |
| 2232 | 2232 | posix.EINVAL => return LinuxEpollCreateError.InvalidSyscall, |
| ... | ... | @@ -2296,7 +2296,7 @@ pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) Lin |
| 2296 | 2296 | |
| 2297 | 2297 | pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize { |
| 2298 | 2298 | 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); | |
| 2300 | 2300 | const err = posix.getErrno(rc); |
| 2301 | 2301 | switch (err) { |
| 2302 | 2302 | 0 => return rc, |
| ... | ... | @@ -2661,7 +2661,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread |
| 2661 | 2661 | posix.EAGAIN => return SpawnThreadError.SystemResources, |
| 2662 | 2662 | posix.EPERM => unreachable, |
| 2663 | 2663 | posix.EINVAL => unreachable, |
| 2664 | else => return unexpectedErrorPosix(usize(err)), | |
| 2664 | else => return unexpectedErrorPosix(@intCast(usize, err)), | |
| 2665 | 2665 | } |
| 2666 | 2666 | } else if (builtin.os == builtin.Os.linux) { |
| 2667 | 2667 | // 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 { |
| 642 | 642 | return WTERMSIG(s) == 0; |
| 643 | 643 | } |
| 644 | 644 | pub fn WIFSTOPPED(s: i32) bool { |
| 645 | return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; | |
| 645 | return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00; | |
| 646 | 646 | } |
| 647 | 647 | pub fn WIFSIGNALED(s: i32) bool { |
| 648 | 648 | return (unsigned(s) & 0xffff) -% 1 < 0xff; |
| ... | ... | @@ -658,11 +658,11 @@ pub const winsize = extern struct { |
| 658 | 658 | /// Get the errno from a syscall return value, or 0 for no error. |
| 659 | 659 | pub fn getErrno(r: usize) usize { |
| 660 | 660 | 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; | |
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | pub 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)); | |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | 668 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -693,12 +693,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize { |
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | pub 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); | |
| 697 | 697 | } |
| 698 | 698 | |
| 699 | 699 | pub fn isatty(fd: i32) bool { |
| 700 | 700 | 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; | |
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -727,7 +727,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize { |
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | pub 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)); | |
| 731 | 731 | } |
| 732 | 732 | |
| 733 | 733 | pub fn munmap(address: usize, length: usize) usize { |
| ... | ... | @@ -735,7 +735,7 @@ pub fn munmap(address: usize, length: usize) usize { |
| 735 | 735 | } |
| 736 | 736 | |
| 737 | 737 | pub 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); | |
| 739 | 739 | } |
| 740 | 740 | |
| 741 | 741 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -749,7 +749,7 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { |
| 749 | 749 | } |
| 750 | 750 | |
| 751 | 751 | pub 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); | |
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -766,11 +766,11 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize { |
| 766 | 766 | } |
| 767 | 767 | |
| 768 | 768 | pub 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); | |
| 770 | 770 | } |
| 771 | 771 | |
| 772 | 772 | pub 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); | |
| 774 | 774 | } |
| 775 | 775 | |
| 776 | 776 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -790,7 +790,7 @@ pub fn create(path: [*]const u8, perm: usize) usize { |
| 790 | 790 | |
| 791 | 791 | // TODO https://github.com/ziglang/zig/issues/265 |
| 792 | 792 | pub 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); | |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | /// See also `clone` (from the arch-specific include) |
| ... | ... | @@ -804,11 +804,11 @@ pub fn clone2(flags: usize, child_stack_ptr: usize) usize { |
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | pub fn close(fd: i32) usize { |
| 807 | return syscall1(SYS_close, usize(fd)); | |
| 807 | return syscall1(SYS_close, @intCast(usize, fd)); | |
| 808 | 808 | } |
| 809 | 809 | |
| 810 | 810 | pub 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); | |
| 812 | 812 | } |
| 813 | 813 | |
| 814 | 814 | pub fn exit(status: i32) noreturn { |
| ... | ... | @@ -817,11 +817,11 @@ pub fn exit(status: i32) noreturn { |
| 817 | 817 | } |
| 818 | 818 | |
| 819 | 819 | pub 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)); | |
| 821 | 821 | } |
| 822 | 822 | |
| 823 | 823 | pub 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)); | |
| 825 | 825 | } |
| 826 | 826 | |
| 827 | 827 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -999,8 +999,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len; |
| 999 | 999 | pub fn raise(sig: i32) usize { |
| 1000 | 1000 | var set: sigset_t = undefined; |
| 1001 | 1001 | 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)); | |
| 1004 | 1004 | restoreSignals(&set); |
| 1005 | 1005 | return ret; |
| 1006 | 1006 | } |
| ... | ... | @@ -1019,12 +1019,12 @@ fn restoreSignals(set: *sigset_t) void { |
| 1019 | 1019 | |
| 1020 | 1020 | pub fn sigaddset(set: *sigset_t, sig: u6) void { |
| 1021 | 1021 | 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)); | |
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | 1025 | pub fn sigismember(set: *const sigset_t, sig: u6) bool { |
| 1026 | 1026 | 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; | |
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | 1030 | pub const in_port_t = u16; |
| ... | ... | @@ -1057,11 +1057,11 @@ pub const iovec = extern struct { |
| 1057 | 1057 | }; |
| 1058 | 1058 | |
| 1059 | 1059 | pub 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)); | |
| 1061 | 1061 | } |
| 1062 | 1062 | |
| 1063 | 1063 | pub 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)); | |
| 1065 | 1065 | } |
| 1066 | 1066 | |
| 1067 | 1067 | pub 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 { |
| 1069 | 1069 | } |
| 1070 | 1070 | |
| 1071 | 1071 | pub 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)); | |
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | pub 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)); | |
| 1077 | 1077 | } |
| 1078 | 1078 | |
| 1079 | 1079 | pub 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); | |
| 1081 | 1081 | } |
| 1082 | 1082 | |
| 1083 | 1083 | pub 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)); | |
| 1085 | 1085 | } |
| 1086 | 1086 | |
| 1087 | 1087 | pub 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); | |
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | 1091 | pub 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)); | |
| 1093 | 1093 | } |
| 1094 | 1094 | |
| 1095 | 1095 | pub 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)); | |
| 1097 | 1097 | } |
| 1098 | 1098 | |
| 1099 | 1099 | pub 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)); | |
| 1101 | 1101 | } |
| 1102 | 1102 | |
| 1103 | 1103 | pub fn listen(fd: i32, backlog: u32) usize { |
| 1104 | return syscall2(SYS_listen, usize(fd), backlog); | |
| 1104 | return syscall2(SYS_listen, @intCast(usize, fd), backlog); | |
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | pub 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)); | |
| 1109 | 1109 | } |
| 1110 | 1110 | |
| 1111 | 1111 | pub 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])); | |
| 1113 | 1113 | } |
| 1114 | 1114 | |
| 1115 | 1115 | pub 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 { |
| 1117 | 1117 | } |
| 1118 | 1118 | |
| 1119 | 1119 | pub 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); | |
| 1121 | 1121 | } |
| 1122 | 1122 | |
| 1123 | 1123 | pub 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)); | |
| 1125 | 1125 | } |
| 1126 | 1126 | |
| 1127 | 1127 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -1214,15 +1214,15 @@ pub fn epoll_create1(flags: usize) usize { |
| 1214 | 1214 | } |
| 1215 | 1215 | |
| 1216 | 1216 | pub 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)); | |
| 1218 | 1218 | } |
| 1219 | 1219 | |
| 1220 | 1220 | pub 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)); | |
| 1222 | 1222 | } |
| 1223 | 1223 | |
| 1224 | 1224 | pub 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)); | |
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | pub const itimerspec = extern struct { |
| ... | ... | @@ -1231,11 +1231,11 @@ pub const itimerspec = extern struct { |
| 1231 | 1231 | }; |
| 1232 | 1232 | |
| 1233 | 1233 | pub 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)); | |
| 1235 | 1235 | } |
| 1236 | 1236 | |
| 1237 | 1237 | pub 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)); | |
| 1239 | 1239 | } |
| 1240 | 1240 | |
| 1241 | 1241 | pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330; |
| ... | ... | @@ -1345,7 +1345,7 @@ pub const cap_user_data_t = extern struct { |
| 1345 | 1345 | }; |
| 1346 | 1346 | |
| 1347 | 1347 | pub fn unshare(flags: usize) usize { |
| 1348 | return syscall1(SYS_unshare, usize(flags)); | |
| 1348 | return syscall1(SYS_unshare, @intCast(usize, flags)); | |
| 1349 | 1349 | } |
| 1350 | 1350 | |
| 1351 | 1351 | pub 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" { |
| 21 | 21 | .it_value = time_interval, |
| 22 | 22 | }; |
| 23 | 23 | |
| 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); | |
| 25 | 25 | assert(err == 0); |
| 26 | 26 | |
| 27 | 27 | var event = linux.epoll_event{ |
| ... | ... | @@ -29,12 +29,12 @@ test "timer" { |
| 29 | 29 | .data = linux.epoll_data{ .ptr = 0 }, |
| 30 | 30 | }; |
| 31 | 31 | |
| 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); | |
| 33 | 33 | assert(err == 0); |
| 34 | 34 | |
| 35 | 35 | const events_one: linux.epoll_event = undefined; |
| 36 | 36 | var events = []linux.epoll_event{events_one} ** 8; |
| 37 | 37 | |
| 38 | 38 | // 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); | |
| 40 | 40 | } |
std/os/linux/vdso.zig+2-2| ... | ... | @@ -62,8 +62,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { |
| 62 | 62 | |
| 63 | 63 | var i: usize = 0; |
| 64 | 64 | 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; | |
| 67 | 67 | if (0 == syms[i].st_shndx) continue; |
| 68 | 68 | if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue; |
| 69 | 69 | if (maybe_versym) |versym| { |
std/os/time.zig+12-12| ... | ... | @@ -14,12 +14,12 @@ pub const epoch = @import("epoch.zig"); |
| 14 | 14 | pub fn sleep(seconds: usize, nanoseconds: usize) void { |
| 15 | 15 | switch (builtin.os) { |
| 16 | 16 | Os.linux, Os.macosx, Os.ios => { |
| 17 | posixSleep(u63(seconds), u63(nanoseconds)); | |
| 17 | posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds)); | |
| 18 | 18 | }, |
| 19 | 19 | Os.windows => { |
| 20 | 20 | const ns_per_ms = ns_per_s / ms_per_s; |
| 21 | 21 | const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms; |
| 22 | windows.Sleep(windows.DWORD(milliseconds)); | |
| 22 | windows.Sleep(@intCast(windows.DWORD, milliseconds)); | |
| 23 | 23 | }, |
| 24 | 24 | else => @compileError("Unsupported OS"), |
| 25 | 25 | } |
| ... | ... | @@ -83,8 +83,8 @@ fn milliTimestampDarwin() u64 { |
| 83 | 83 | var tv: darwin.timeval = undefined; |
| 84 | 84 | var err = darwin.gettimeofday(&tv, null); |
| 85 | 85 | 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); | |
| 88 | 88 | return u64(sec_ms) + u64(usec_ms); |
| 89 | 89 | } |
| 90 | 90 | |
| ... | ... | @@ -95,8 +95,8 @@ fn milliTimestampPosix() u64 { |
| 95 | 95 | var ts: posix.timespec = undefined; |
| 96 | 96 | const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts); |
| 97 | 97 | 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); | |
| 100 | 100 | return sec_ms + nsec_ms; |
| 101 | 101 | } |
| 102 | 102 | |
| ... | ... | @@ -162,13 +162,13 @@ pub const Timer = struct { |
| 162 | 162 | var freq: i64 = undefined; |
| 163 | 163 | var err = windows.QueryPerformanceFrequency(&freq); |
| 164 | 164 | if (err == windows.FALSE) return error.TimerUnsupported; |
| 165 | self.frequency = u64(freq); | |
| 165 | self.frequency = @intCast(u64, freq); | |
| 166 | 166 | self.resolution = @divFloor(ns_per_s, self.frequency); |
| 167 | 167 | |
| 168 | 168 | var start_time: i64 = undefined; |
| 169 | 169 | err = windows.QueryPerformanceCounter(&start_time); |
| 170 | 170 | debug.assert(err != windows.FALSE); |
| 171 | self.start_time = u64(start_time); | |
| 171 | self.start_time = @intCast(u64, start_time); | |
| 172 | 172 | }, |
| 173 | 173 | Os.linux => { |
| 174 | 174 | //On Linux, seccomp can do arbitrary things to our ability to call |
| ... | ... | @@ -184,12 +184,12 @@ pub const Timer = struct { |
| 184 | 184 | posix.EINVAL => return error.TimerUnsupported, |
| 185 | 185 | else => return std.os.unexpectedErrorPosix(errno), |
| 186 | 186 | } |
| 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); | |
| 188 | 188 | |
| 189 | 189 | result = posix.clock_gettime(monotonic_clock_id, &ts); |
| 190 | 190 | errno = posix.getErrno(result); |
| 191 | 191 | 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); | |
| 193 | 193 | }, |
| 194 | 194 | Os.macosx, Os.ios => { |
| 195 | 195 | darwin.mach_timebase_info(&self.frequency); |
| ... | ... | @@ -236,7 +236,7 @@ pub const Timer = struct { |
| 236 | 236 | var result: i64 = undefined; |
| 237 | 237 | var err = windows.QueryPerformanceCounter(&result); |
| 238 | 238 | debug.assert(err != windows.FALSE); |
| 239 | return u64(result); | |
| 239 | return @intCast(u64, result); | |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | fn clockDarwin() u64 { |
| ... | ... | @@ -247,7 +247,7 @@ pub const Timer = struct { |
| 247 | 247 | var ts: posix.timespec = undefined; |
| 248 | 248 | var result = posix.clock_gettime(monotonic_clock_id, &ts); |
| 249 | 249 | 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); | |
| 251 | 251 | } |
| 252 | 252 | }; |
| 253 | 253 |
std/os/windows/util.zig+7-2| ... | ... | @@ -42,7 +42,7 @@ pub const WriteError = error{ |
| 42 | 42 | }; |
| 43 | 43 | |
| 44 | 44 | pub 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) { | |
| 46 | 46 | const err = windows.GetLastError(); |
| 47 | 47 | return switch (err) { |
| 48 | 48 | windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources, |
| ... | ... | @@ -68,7 +68,12 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool { |
| 68 | 68 | const size = @sizeOf(windows.FILE_NAME_INFO); |
| 69 | 69 | var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH); |
| 70 | 70 | |
| 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) { | |
| 72 | 77 | return true; |
| 73 | 78 | } |
| 74 | 79 |
std/rand/index.zig+8-8| ... | ... | @@ -55,16 +55,16 @@ pub const Random = struct { |
| 55 | 55 | if (T.is_signed) { |
| 56 | 56 | const uint = @IntType(false, T.bit_count); |
| 57 | 57 | 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))); | |
| 59 | 59 | } else if (start < 0 and end < 0) { |
| 60 | 60 | // Can't overflow because the range is over signed ints |
| 61 | 61 | return math.negateCast(r.range(uint, math.absCast(end), math.absCast(start)) + 1) catch unreachable; |
| 62 | 62 | } else if (start < 0 and end >= 0) { |
| 63 | const end_uint = uint(end); | |
| 63 | const end_uint = @intCast(uint, end); | |
| 64 | 64 | const total_range = math.absCast(start) + end_uint; |
| 65 | 65 | const value = r.range(uint, 0, total_range); |
| 66 | 66 | const result = if (value < end_uint) x: { |
| 67 | break :x T(value); | |
| 67 | break :x @intCast(T, value); | |
| 68 | 68 | } else if (value == end_uint) x: { |
| 69 | 69 | break :x start; |
| 70 | 70 | } else x: { |
| ... | ... | @@ -213,9 +213,9 @@ pub const Pcg = struct { |
| 213 | 213 | self.s = l *% default_multiplier +% (self.i | 1); |
| 214 | 214 | |
| 215 | 215 | const xor_s = @truncate(u32, ((l >> 18) ^ l) >> 27); |
| 216 | const rot = u32(l >> 59); | |
| 216 | const rot = @intCast(u32, l >> 59); | |
| 217 | 217 | |
| 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)); | |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | fn seed(self: *Pcg, init_s: u64) void { |
| ... | ... | @@ -322,7 +322,7 @@ pub const Xoroshiro128 = struct { |
| 322 | 322 | inline for (table) |entry| { |
| 323 | 323 | var b: usize = 0; |
| 324 | 324 | while (b < 64) : (b += 1) { |
| 325 | if ((entry & (u64(1) << u6(b))) != 0) { | |
| 325 | if ((entry & (u64(1) << @intCast(u6, b))) != 0) { | |
| 326 | 326 | s0 ^= self.s[0]; |
| 327 | 327 | s1 ^= self.s[1]; |
| 328 | 328 | } |
| ... | ... | @@ -667,13 +667,13 @@ test "Random range" { |
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | fn testRange(r: *Random, start: i32, end: i32) void { |
| 670 | const count = usize(end - start); | |
| 670 | const count = @intCast(usize, end - start); | |
| 671 | 671 | var values_buffer = []bool{false} ** 20; |
| 672 | 672 | const values = values_buffer[0..count]; |
| 673 | 673 | var i: usize = 0; |
| 674 | 674 | while (i < count) { |
| 675 | 675 | const value = r.range(i32, start, end); |
| 676 | const index = usize(value - start); | |
| 676 | const index = @intCast(usize, value - start); | |
| 677 | 677 | if (!values[index]) { |
| 678 | 678 | i += 1; |
| 679 | 679 | 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 |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | 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); | |
| 108 | 108 | self.allocator.free(self.dynamic_segments); |
| 109 | 109 | self.* = undefined; |
| 110 | 110 | } |
| ... | ... | @@ -158,7 +158,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type |
| 158 | 158 | /// Only grows capacity, or retains current capacity |
| 159 | 159 | pub fn growCapacity(self: *Self, new_capacity: usize) !void { |
| 160 | 160 | 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); | |
| 162 | 162 | if (new_cap_shelf_count > old_shelf_count) { |
| 163 | 163 | self.dynamic_segments = try self.allocator.realloc([*]T, self.dynamic_segments, new_cap_shelf_count); |
| 164 | 164 | var i = old_shelf_count; |
| ... | ... | @@ -175,7 +175,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type |
| 175 | 175 | /// Only shrinks capacity or retains current capacity |
| 176 | 176 | pub fn shrinkCapacity(self: *Self, new_capacity: usize) void { |
| 177 | 177 | if (new_capacity <= prealloc_item_count) { |
| 178 | const len = ShelfIndex(self.dynamic_segments.len); | |
| 178 | const len = @intCast(ShelfIndex, self.dynamic_segments.len); | |
| 179 | 179 | self.freeShelves(len, 0); |
| 180 | 180 | self.allocator.free(self.dynamic_segments); |
| 181 | 181 | self.dynamic_segments = [][*]T{}; |
| ... | ... | @@ -183,7 +183,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | 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); | |
| 187 | 187 | assert(new_cap_shelf_count <= old_shelf_count); |
| 188 | 188 | if (new_cap_shelf_count == old_shelf_count) { |
| 189 | 189 | return; |
| ... | ... | @@ -338,7 +338,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void { |
| 338 | 338 | { |
| 339 | 339 | var i: usize = 0; |
| 340 | 340 | while (i < 100) : (i += 1) { |
| 341 | try list.push(i32(i + 1)); | |
| 341 | try list.push(@intCast(i32, i + 1)); | |
| 342 | 342 | assert(list.len == i + 1); |
| 343 | 343 | } |
| 344 | 344 | } |
| ... | ... | @@ -346,7 +346,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void { |
| 346 | 346 | { |
| 347 | 347 | var i: usize = 0; |
| 348 | 348 | while (i < 100) : (i += 1) { |
| 349 | assert(list.at(i).* == i32(i + 1)); | |
| 349 | assert(list.at(i).* == @intCast(i32, i + 1)); | |
| 350 | 350 | } |
| 351 | 351 | } |
| 352 | 352 |
std/special/bootstrap.zig+1-1| ... | ... | @@ -80,7 +80,7 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 { |
| 80 | 80 | var env_count: usize = 0; |
| 81 | 81 | while (c_envp[env_count] != null) : (env_count += 1) {} |
| 82 | 82 | 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); | |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | fn callMain() u8 { |
std/special/builtin.zig+15-15| ... | ... | @@ -135,9 +135,9 @@ fn generic_fmod(comptime T: type, x: T, y: T) T { |
| 135 | 135 | const mask = if (T == f32) 0xff else 0x7ff; |
| 136 | 136 | var ux = @bitCast(uint, x); |
| 137 | 137 | 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); | |
| 141 | 141 | var i: uint = undefined; |
| 142 | 142 | |
| 143 | 143 | 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 { |
| 156 | 156 | ex -= 1; |
| 157 | 157 | i <<= 1; |
| 158 | 158 | }) {} |
| 159 | ux <<= log2uint(@bitCast(u32, -ex + 1)); | |
| 159 | ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1)); | |
| 160 | 160 | } else { |
| 161 | 161 | ux &= @maxValue(uint) >> exp_bits; |
| 162 | 162 | ux |= 1 << digits; |
| ... | ... | @@ -167,7 +167,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T { |
| 167 | 167 | ey -= 1; |
| 168 | 168 | i <<= 1; |
| 169 | 169 | }) {} |
| 170 | uy <<= log2uint(@bitCast(u32, -ey + 1)); | |
| 170 | uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1)); | |
| 171 | 171 | } else { |
| 172 | 172 | uy &= @maxValue(uint) >> exp_bits; |
| 173 | 173 | uy |= 1 << digits; |
| ... | ... | @@ -199,12 +199,12 @@ fn generic_fmod(comptime T: type, x: T, y: T) T { |
| 199 | 199 | ux -%= 1 << digits; |
| 200 | 200 | ux |= uint(@bitCast(u32, ex)) << digits; |
| 201 | 201 | } else { |
| 202 | ux >>= log2uint(@bitCast(u32, -ex + 1)); | |
| 202 | ux >>= @intCast(log2uint, @bitCast(u32, -ex + 1)); | |
| 203 | 203 | } |
| 204 | 204 | if (T == f32) { |
| 205 | 205 | ux |= sx; |
| 206 | 206 | } else { |
| 207 | ux |= uint(sx) << bits_minus_1; | |
| 207 | ux |= @intCast(uint, sx) << bits_minus_1; | |
| 208 | 208 | } |
| 209 | 209 | return @bitCast(T, ux); |
| 210 | 210 | } |
| ... | ... | @@ -227,8 +227,8 @@ export fn sqrt(x: f64) f64 { |
| 227 | 227 | const sign: u32 = 0x80000000; |
| 228 | 228 | const u = @bitCast(u64, x); |
| 229 | 229 | |
| 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); | |
| 232 | 232 | |
| 233 | 233 | // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan |
| 234 | 234 | if (ix0 & 0x7FF00000 == 0x7FF00000) { |
| ... | ... | @@ -245,7 +245,7 @@ export fn sqrt(x: f64) f64 { |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | // normalize x |
| 248 | var m = i32(ix0 >> 20); | |
| 248 | var m = @intCast(i32, ix0 >> 20); | |
| 249 | 249 | if (m == 0) { |
| 250 | 250 | // subnormal |
| 251 | 251 | while (ix0 == 0) { |
| ... | ... | @@ -259,9 +259,9 @@ export fn sqrt(x: f64) f64 { |
| 259 | 259 | while (ix0 & 0x00100000 == 0) : (i += 1) { |
| 260 | 260 | ix0 <<= 1; |
| 261 | 261 | } |
| 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); | |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | // unbias exponent |
| ... | ... | @@ -345,10 +345,10 @@ export fn sqrt(x: f64) f64 { |
| 345 | 345 | |
| 346 | 346 | // NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same |
| 347 | 347 | // behaviour at least. |
| 348 | var iix0 = i32(ix0); | |
| 348 | var iix0 = @intCast(i32, ix0); | |
| 349 | 349 | iix0 = iix0 +% (m << 20); |
| 350 | 350 | |
| 351 | const uz = (u64(iix0) << 32) | ix1; | |
| 351 | const uz = (@intCast(u64, iix0) << 32) | ix1; | |
| 352 | 352 | return @bitCast(f64, uz); |
| 353 | 353 | } |
| 354 | 354 |
std/special/compiler_rt/divti3.zig+1-1| ... | ... | @@ -13,7 +13,7 @@ pub extern fn __divti3(a: i128, b: i128) i128 { |
| 13 | 13 | |
| 14 | 14 | const r = udivmod(u128, @bitCast(u128, an), @bitCast(u128, bn), null); |
| 15 | 15 | const s = s_a ^ s_b; |
| 16 | return (i128(r) ^ s) -% s; | |
| 16 | return (@bitCast(i128, r) ^ s) -% s; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | pub 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 |
| 32 | 32 | const aAbs: rep_t = aRep & absMask; |
| 33 | 33 | |
| 34 | 34 | 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; | |
| 36 | 36 | const significand: rep_t = (aAbs & significandMask) | implicitBit; |
| 37 | 37 | |
| 38 | 38 | // If either the value or the exponent is negative, the result is zero. |
| 39 | 39 | if (sign == -1 or exponent < 0) return 0; |
| 40 | 40 | |
| 41 | 41 | // 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); | |
| 43 | 43 | |
| 44 | 44 | // If 0 <= exponent < significandBits, right shift to get the result. |
| 45 | 45 | // Otherwise, shift left. |
| ... | ... | @@ -47,11 +47,11 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t |
| 47 | 47 | // TODO this is a workaround for the mysterious "integer cast truncated bits" |
| 48 | 48 | // happening on the next line |
| 49 | 49 | @setRuntimeSafety(false); |
| 50 | return fixuint_t(significand >> Log2Int(rep_t)(significandBits - exponent)); | |
| 50 | return @intCast(fixuint_t, significand >> @intCast(Log2Int(rep_t), significandBits - exponent)); | |
| 51 | 51 | } else { |
| 52 | 52 | // TODO this is a workaround for the mysterious "integer cast truncated bits" |
| 53 | 53 | // happening on the next line |
| 54 | 54 | @setRuntimeSafety(false); |
| 55 | return fixuint_t(significand) << Log2Int(fixuint_t)(exponent - significandBits); | |
| 55 | return @intCast(fixuint_t, significand) << @intCast(Log2Int(fixuint_t), exponent - significandBits); | |
| 56 | 56 | } |
| 57 | 57 | } |
std/special/compiler_rt/index.zig+6-6| ... | ... | @@ -292,7 +292,7 @@ extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 { |
| 292 | 292 | @setRuntimeSafety(is_test); |
| 293 | 293 | |
| 294 | 294 | 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))); | |
| 296 | 296 | return d; |
| 297 | 297 | } |
| 298 | 298 | |
| ... | ... | @@ -316,12 +316,12 @@ extern fn __udivsi3(n: u32, d: u32) u32 { |
| 316 | 316 | sr += 1; |
| 317 | 317 | // 1 <= sr <= n_uword_bits - 1 |
| 318 | 318 | // 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); | |
| 321 | 321 | var carry: u32 = 0; |
| 322 | 322 | while (sr > 0) : (sr -= 1) { |
| 323 | 323 | // 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)); | |
| 325 | 325 | q = (q << 1) | carry; |
| 326 | 326 | // carry = 0; |
| 327 | 327 | // if (r.all >= d.all) |
| ... | ... | @@ -329,8 +329,8 @@ extern fn __udivsi3(n: u32, d: u32) u32 { |
| 329 | 329 | // r.all -= d.all; |
| 330 | 330 | // carry = 1; |
| 331 | 331 | // } |
| 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); | |
| 334 | 334 | r -= d & @bitCast(u32, s); |
| 335 | 335 | } |
| 336 | 336 | 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: |
| 71 | 71 | r[high] = n[high] & (d[high] - 1); |
| 72 | 72 | rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421 |
| 73 | 73 | } |
| 74 | return n[high] >> Log2SingleInt(@ctz(d[high])); | |
| 74 | return n[high] >> @intCast(Log2SingleInt, @ctz(d[high])); | |
| 75 | 75 | } |
| 76 | 76 | // K K |
| 77 | 77 | // --- |
| ... | ... | @@ -88,10 +88,10 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: |
| 88 | 88 | // 1 <= sr <= SingleInt.bit_count - 1 |
| 89 | 89 | // q.all = a << (DoubleInt.bit_count - sr); |
| 90 | 90 | q[low] = 0; |
| 91 | q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr); | |
| 91 | q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr); | |
| 92 | 92 | // 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)); | |
| 95 | 95 | } else { |
| 96 | 96 | // d[low] != 0 |
| 97 | 97 | if (d[high] == 0) { |
| ... | ... | @@ -107,8 +107,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: |
| 107 | 107 | return a; |
| 108 | 108 | } |
| 109 | 109 | 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)); | |
| 112 | 112 | return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421 |
| 113 | 113 | } |
| 114 | 114 | // K X |
| ... | ... | @@ -126,15 +126,15 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: |
| 126 | 126 | } else if (sr < SingleInt.bit_count) { |
| 127 | 127 | // 2 <= sr <= SingleInt.bit_count - 1 |
| 128 | 128 | 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)); | |
| 132 | 132 | } else { |
| 133 | 133 | // 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)); | |
| 136 | 136 | r[high] = 0; |
| 137 | r[low] = n[high] >> Log2SingleInt(sr - SingleInt.bit_count); | |
| 137 | r[low] = n[high] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count); | |
| 138 | 138 | } |
| 139 | 139 | } else { |
| 140 | 140 | // K X |
| ... | ... | @@ -158,9 +158,9 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: |
| 158 | 158 | r[high] = 0; |
| 159 | 159 | r[low] = n[high]; |
| 160 | 160 | } 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); | |
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | } |
| ... | ... | @@ -184,8 +184,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: |
| 184 | 184 | // carry = 1; |
| 185 | 185 | // } |
| 186 | 186 | 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); | |
| 189 | 189 | r_all -= b & @bitCast(DoubleInt, s); |
| 190 | 190 | r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421 |
| 191 | 191 | } |
std/unicode.zig+10-10| ... | ... | @@ -35,22 +35,22 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 { |
| 35 | 35 | // - Increasing the initial shift by 6 each time |
| 36 | 36 | // - Each time after the first shorten the shifted |
| 37 | 37 | // 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 | |
| 39 | 39 | 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)); | |
| 42 | 42 | }, |
| 43 | 43 | 3 => { |
| 44 | 44 | 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)); | |
| 48 | 48 | }, |
| 49 | 49 | 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)); | |
| 54 | 54 | }, |
| 55 | 55 | else => unreachable, |
| 56 | 56 | } |
std/zig/tokenizer.zig+1-1| ... | ... | @@ -1128,7 +1128,7 @@ pub const Tokenizer = struct { |
| 1128 | 1128 | // check utf8-encoded character. |
| 1129 | 1129 | const length = std.unicode.utf8ByteSequenceLength(c0) catch return 1; |
| 1130 | 1130 | if (self.index + length > self.buffer.len) { |
| 1131 | return u3(self.buffer.len - self.index); | |
| 1131 | return @intCast(u3, self.buffer.len - self.index); | |
| 1132 | 1132 | } |
| 1133 | 1133 | const bytes = self.buffer[self.index .. self.index + length]; |
| 1134 | 1134 | switch (length) { |
test/cases/cast.zig+17-1| ... | ... | @@ -343,7 +343,7 @@ fn testPeerErrorAndArray2(x: u8) error![]const u8 { |
| 343 | 343 | test "explicit cast float number literal to integer if no fraction component" { |
| 344 | 344 | const x = i32(1e4); |
| 345 | 345 | assert(x == 10000); |
| 346 | const y = i32(f32(1e4)); | |
| 346 | const y = @floatToInt(i32, f32(1e4)); | |
| 347 | 347 | assert(y == 10000); |
| 348 | 348 | } |
| 349 | 349 | |
| ... | ... | @@ -398,3 +398,19 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" { |
| 398 | 398 | const x: [*]const ?[*]const u8 = &window_name; |
| 399 | 399 | assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); |
| 400 | 400 | } |
| 401 | ||
| 402 | test "@intCast comptime_int" { | |
| 403 | const result = @intCast(i32, 1234); | |
| 404 | assert(@typeOf(result) == i32); | |
| 405 | assert(result == 1234); | |
| 406 | } | |
| 407 | ||
| 408 | test "@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" { |
| 99 | 99 | testIntToEnumEval(3); |
| 100 | 100 | } |
| 101 | 101 | fn testIntToEnumEval(x: i32) void { |
| 102 | assert(IntToEnumNumber(u3(x)) == IntToEnumNumber.Three); | |
| 102 | assert(IntToEnumNumber(@intCast(u3, x)) == IntToEnumNumber.Three); | |
| 103 | 103 | } |
| 104 | 104 | const IntToEnumNumber = enum { |
| 105 | 105 | Zero, |
test/cases/eval.zig+4-4| ... | ... | @@ -5,7 +5,7 @@ const builtin = @import("builtin"); |
| 5 | 5 | test "compile time recursion" { |
| 6 | 6 | assert(some_data.len == 21); |
| 7 | 7 | } |
| 8 | var some_data: [usize(fibonacci(7))]u8 = undefined; | |
| 8 | var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined; | |
| 9 | 9 | fn fibonacci(x: i32) i32 { |
| 10 | 10 | if (x <= 1) return 1; |
| 11 | 11 | return fibonacci(x - 1) + fibonacci(x - 2); |
| ... | ... | @@ -356,7 +356,7 @@ const global_array = x: { |
| 356 | 356 | test "compile-time downcast when the bits fit" { |
| 357 | 357 | comptime { |
| 358 | 358 | const spartan_count: u16 = 255; |
| 359 | const byte = u8(spartan_count); | |
| 359 | const byte = @intCast(u8, spartan_count); | |
| 360 | 360 | assert(byte == 255); |
| 361 | 361 | } |
| 362 | 362 | } |
| ... | ... | @@ -440,7 +440,7 @@ test "binary math operator in partially inlined function" { |
| 440 | 440 | var b: [16]u8 = undefined; |
| 441 | 441 | |
| 442 | 442 | for (b) |*r, i| |
| 443 | r.* = u8(i + 1); | |
| 443 | r.* = @intCast(u8, i + 1); | |
| 444 | 444 | |
| 445 | 445 | copyWithPartialInline(s[0..], b[0..]); |
| 446 | 446 | assert(s[0] == 0x1020304); |
| ... | ... | @@ -480,7 +480,7 @@ fn generateTable(comptime T: type) [1010]T { |
| 480 | 480 | var res: [1010]T = undefined; |
| 481 | 481 | var i: usize = 0; |
| 482 | 482 | while (i < 1010) : (i += 1) { |
| 483 | res[i] = T(i); | |
| 483 | res[i] = @intCast(T, i); | |
| 484 | 484 | } |
| 485 | 485 | return res; |
| 486 | 486 | } |
test/cases/fn.zig+1-1| ... | ... | @@ -80,7 +80,7 @@ test "function pointers" { |
| 80 | 80 | fn4, |
| 81 | 81 | }; |
| 82 | 82 | for (fns) |f, i| { |
| 83 | assert(f() == u32(i) + 5); | |
| 83 | assert(f() == @intCast(u32, i) + 5); | |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | fn fn1() u32 { |
test/cases/for.zig+2-2| ... | ... | @@ -46,7 +46,7 @@ test "basic for loop" { |
| 46 | 46 | buf_index += 1; |
| 47 | 47 | } |
| 48 | 48 | for (array) |item, index| { |
| 49 | buffer[buf_index] = u8(index); | |
| 49 | buffer[buf_index] = @intCast(u8, index); | |
| 50 | 50 | buf_index += 1; |
| 51 | 51 | } |
| 52 | 52 | const unknown_size: []const u8 = array; |
| ... | ... | @@ -55,7 +55,7 @@ test "basic for loop" { |
| 55 | 55 | buf_index += 1; |
| 56 | 56 | } |
| 57 | 57 | for (unknown_size) |item, index| { |
| 58 | buffer[buf_index] = u8(index); | |
| 58 | buffer[buf_index] = @intCast(u8, index); | |
| 59 | 59 | buf_index += 1; |
| 60 | 60 | } |
| 61 | 61 |
test/cases/struct.zig+4-4| ... | ... | @@ -365,14 +365,14 @@ test "runtime struct initialization of bitfield" { |
| 365 | 365 | .y = x1, |
| 366 | 366 | }; |
| 367 | 367 | const s2 = Nibbles{ |
| 368 | .x = u4(x2), | |
| 369 | .y = u4(x2), | |
| 368 | .x = @intCast(u4, x2), | |
| 369 | .y = @intCast(u4, x2), | |
| 370 | 370 | }; |
| 371 | 371 | |
| 372 | 372 | assert(s1.x == x1); |
| 373 | 373 | 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)); | |
| 376 | 376 | } |
| 377 | 377 | |
| 378 | 378 | var x1 = u4(1); |
test/compare_output.zig+3-3| ... | ... | @@ -299,7 +299,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 299 | 299 | \\export fn main() c_int { |
| 300 | 300 | \\ var array = []u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 }; |
| 301 | 301 | \\ |
| 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); | |
| 303 | 303 | \\ |
| 304 | 304 | \\ for (array) |item, i| { |
| 305 | 305 | \\ if (item != i) { |
| ... | ... | @@ -331,8 +331,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 331 | 331 | \\ } |
| 332 | 332 | \\ const small: f32 = 3.25; |
| 333 | 333 | \\ 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); | |
| 336 | 336 | \\ _ = c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4)); |
| 337 | 337 | \\ return 0; |
| 338 | 338 | \\} |
test/compile_errors.zig+4-4| ... | ... | @@ -2931,10 +2931,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 2931 | 2931 | "cast negative value to unsigned integer", |
| 2932 | 2932 | \\comptime { |
| 2933 | 2933 | \\ const value: i32 = -1; |
| 2934 | \\ const unsigned = u32(value); | |
| 2934 | \\ const unsigned = @intCast(u32, value); | |
| 2935 | 2935 | \\} |
| 2936 | 2936 | , |
| 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", | |
| 2938 | 2938 | ); |
| 2939 | 2939 | |
| 2940 | 2940 | cases.add( |
| ... | ... | @@ -2963,10 +2963,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 2963 | 2963 | "compile-time integer cast truncates bits", |
| 2964 | 2964 | \\comptime { |
| 2965 | 2965 | \\ const spartan_count: u16 = 300; |
| 2966 | \\ const byte = u8(spartan_count); | |
| 2966 | \\ const byte = @intCast(u8, spartan_count); | |
| 2967 | 2967 | \\} |
| 2968 | 2968 | , |
| 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", | |
| 2970 | 2970 | ); |
| 2971 | 2971 | |
| 2972 | 2972 | cases.add( |
test/runtime_safety.zig+2-2| ... | ... | @@ -188,7 +188,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 188 | 188 | \\ if (x == 0) return error.Whatever; |
| 189 | 189 | \\} |
| 190 | 190 | \\fn shorten_cast(x: i32) i8 { |
| 191 | \\ return i8(x); | |
| 191 | \\ return @intCast(i8, x); | |
| 192 | 192 | \\} |
| 193 | 193 | ); |
| 194 | 194 | |
| ... | ... | @@ -201,7 +201,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void { |
| 201 | 201 | \\ if (x == 0) return error.Whatever; |
| 202 | 202 | \\} |
| 203 | 203 | \\fn unsigned_cast(x: i32) u32 { |
| 204 | \\ return u32(x); | |
| 204 | \\ return @intCast(u32, x); | |
| 205 | 205 | \\} |
| 206 | 206 | ); |
| 207 | 207 |