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

Remove number casting syntax
master
Andrew Kelley 2018-06-17 14:40:07 -04:00 committed by GitHub
commit 431fda4141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
90 changed files with 823 additions and 438 deletions

View File

@ -1355,7 +1355,7 @@ var some_integers: [100]i32 = undefined;
test "modify an array" {
for (some_integers) |*item, i| {
item.* = i32(i);
item.* = @intCast(i32, i);
}
assert(some_integers[10] == 10);
assert(some_integers[99] == 99);
@ -1397,8 +1397,8 @@ var fancy_array = init: {
var initial_value: [10]Point = undefined;
for (initial_value) |*pt, i| {
pt.* = Point{
.x = i32(i),
.y = i32(i) * 2,
.x = @intCast(i32, i),
.y = @intCast(i32, i) * 2,
};
}
break :init initial_value;
@ -2410,7 +2410,7 @@ test "for basics" {
var sum2: i32 = 0;
for (items) |value, i| {
assert(@typeOf(i) == usize);
sum2 += i32(i);
sum2 += @intCast(i32, i);
}
assert(sum2 == 10);
}
@ -5730,7 +5730,7 @@ comptime {
{#code_begin|test_err|attempt to cast negative value to unsigned integer#}
comptime {
const value: i32 = -1;
const unsigned = u32(value);
const unsigned = @intCast(u32, value);
}
{#code_end#}
<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 {
{#code_begin|test_err|cast from 'u16' to 'u8' truncates bits#}
comptime {
const spartan_count: u16 = 300;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
}
{#code_end#}
<p>At runtime crashes with the message <code>integer cast truncated bits</code> and a stack trace.</p>

View File

@ -8,7 +8,7 @@ const c = @cImport({
const msg = c"Hello, world!\n";
export fn main(argc: c_int, argv: **u8) c_int {
if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
if (c.printf(msg) != @intCast(c_int, c.strlen(msg))) return -1;
return 0;
}

View File

@ -1357,6 +1357,10 @@ enum BuiltinFnId {
BuiltinFnIdMod,
BuiltinFnIdSqrt,
BuiltinFnIdTruncate,
BuiltinFnIdIntCast,
BuiltinFnIdFloatCast,
BuiltinFnIdIntToFloat,
BuiltinFnIdFloatToInt,
BuiltinFnIdIntType,
BuiltinFnIdSetCold,
BuiltinFnIdSetRuntimeSafety,
@ -2040,6 +2044,10 @@ enum IrInstructionId {
IrInstructionIdCmpxchg,
IrInstructionIdFence,
IrInstructionIdTruncate,
IrInstructionIdIntCast,
IrInstructionIdFloatCast,
IrInstructionIdIntToFloat,
IrInstructionIdFloatToInt,
IrInstructionIdIntType,
IrInstructionIdBoolNot,
IrInstructionIdMemset,
@ -2632,6 +2640,34 @@ struct IrInstructionTruncate {
IrInstruction *target;
};
struct IrInstructionIntCast {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionFloatCast {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionIntToFloat {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionFloatToInt {
IrInstruction base;
IrInstruction *dest_type;
IrInstruction *target;
};
struct IrInstructionIntType {
IrInstruction base;

View File

@ -4722,6 +4722,10 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdPromiseResultType:
case IrInstructionIdAwaitBookkeeping:
case IrInstructionIdAddImplicitReturnType:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
zig_unreachable();
case IrInstructionIdReturn:
@ -6310,6 +6314,10 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6);
create_builtin_fn(g, BuiltinFnIdFence, "fence", 1);
create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2);
create_builtin_fn(g, BuiltinFnIdIntCast, "intCast", 2);
create_builtin_fn(g, BuiltinFnIdFloatCast, "floatCast", 2);
create_builtin_fn(g, BuiltinFnIdIntToFloat, "intToFloat", 2);
create_builtin_fn(g, BuiltinFnIdFloatToInt, "floatToInt", 2);
create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1);
create_builtin_fn(g, BuiltinFnIdCompileLog, "compileLog", SIZE_MAX);
create_builtin_fn(g, BuiltinFnIdIntType, "IntType", 2); // TODO rename to Int

View File

@ -460,6 +460,22 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionTruncate *) {
return IrInstructionIdTruncate;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntCast *) {
return IrInstructionIdIntCast;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatCast *) {
return IrInstructionIdFloatCast;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntToFloat *) {
return IrInstructionIdIntToFloat;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionFloatToInt *) {
return IrInstructionIdFloatToInt;
}
static constexpr IrInstructionId ir_instruction_id(IrInstructionIntType *) {
return IrInstructionIdIntType;
}
@ -1899,10 +1915,48 @@ static IrInstruction *ir_build_truncate(IrBuilder *irb, Scope *scope, AstNode *s
return &instruction->base;
}
static IrInstruction *ir_build_truncate_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *dest_type, IrInstruction *target) {
IrInstruction *new_instruction = ir_build_truncate(irb, old_instruction->scope, old_instruction->source_node, dest_type, target);
ir_link_new_instruction(new_instruction, old_instruction);
return new_instruction;
static IrInstruction *ir_build_int_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionIntCast *instruction = ir_build_instruction<IrInstructionIntCast>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_float_cast(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionFloatCast *instruction = ir_build_instruction<IrInstructionFloatCast>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_int_to_float(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionIntToFloat *instruction = ir_build_instruction<IrInstructionIntToFloat>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstruction *ir_build_float_to_int(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_type, IrInstruction *target) {
IrInstructionFloatToInt *instruction = ir_build_instruction<IrInstructionFloatToInt>(irb, scope, source_node);
instruction->dest_type = dest_type;
instruction->target = target;
ir_ref_instruction(dest_type, irb->current_basic_block);
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
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
IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, truncate, lval);
}
case BuiltinFnIdIntCast:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_int_cast(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdFloatCast:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_float_cast(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdIntToFloat:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_int_to_float(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdFloatToInt:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_instruction)
return arg0_value;
AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
if (arg1_value == irb->codegen->invalid_instruction)
return arg1_value;
IrInstruction *result = ir_build_float_to_int(irb, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(irb, scope, result, lval);
}
case BuiltinFnIdIntType:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
@ -9948,15 +10062,32 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);
}
// explicit widening or shortening cast
if ((wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdInt) ||
(wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdFloat))
// explicit widening conversion
if (wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdInt &&
wanted_type->data.integral.is_signed == actual_type->data.integral.is_signed &&
wanted_type->data.integral.bit_count >= actual_type->data.integral.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// small enough unsigned ints can get casted to large enough signed ints
if (wanted_type->id == TypeTableEntryIdInt && wanted_type->data.integral.is_signed &&
actual_type->id == TypeTableEntryIdInt && !actual_type->data.integral.is_signed &&
wanted_type->data.integral.bit_count > actual_type->data.integral.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// explicit float widening conversion
if (wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdFloat &&
wanted_type->data.floating.bit_count >= actual_type->data.floating.bit_count)
{
return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
}
// explicit error set cast
if (wanted_type->id == TypeTableEntryIdErrorSet &&
actual_type->id == TypeTableEntryIdErrorSet)
@ -9964,20 +10095,6 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ir_analyze_err_set_cast(ira, source_instr, value, wanted_type);
}
// explicit cast from int to float
if (wanted_type->id == TypeTableEntryIdFloat &&
actual_type->id == TypeTableEntryIdInt)
{
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpIntToFloat, false);
}
// explicit cast from float to int
if (wanted_type->id == TypeTableEntryIdInt &&
actual_type->id == TypeTableEntryIdFloat)
{
return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpFloatToInt, false);
}
// explicit cast from [N]T to []const T
if (is_slice(wanted_type) && actual_type->id == TypeTableEntryIdArray) {
TypeTableEntry *ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;
@ -17365,7 +17482,126 @@ static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstruc
return dest_type;
}
ir_build_truncate_from(&ira->new_irb, &instruction->base, dest_type_value, target);
IrInstruction *new_instruction = ir_build_truncate(&ira->new_irb, instruction->base.scope,
instruction->base.source_node, dest_type_value, target);
ir_link_new_instruction(new_instruction, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstructionIntCast *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
if (dest_type->id != TypeTableEntryIdInt) {
ir_add_error(ira, instruction->dest_type, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id == TypeTableEntryIdComptimeInt) {
if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type,
CastOpNumLitToConcrete, false);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
} else {
return ira->codegen->builtin_types.entry_invalid;
}
}
if (target->value.type->id != TypeTableEntryIdInt) {
ir_add_error(ira, instruction->target, buf_sprintf("expected integer type, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstructionFloatCast *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
if (dest_type->id != TypeTableEntryIdFloat) {
ir_add_error(ira, instruction->dest_type,
buf_sprintf("expected float type, found '%s'", buf_ptr(&dest_type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
if (target->value.type->id == TypeTableEntryIdComptimeInt ||
target->value.type->id == TypeTableEntryIdComptimeFloat)
{
if (ir_num_lit_fits_in_other_type(ira, target, dest_type, true)) {
CastOp op;
if (target->value.type->id == TypeTableEntryIdComptimeInt) {
op = CastOpIntToFloat;
} else {
op = CastOpNumLitToConcrete;
}
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, op, false);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
} else {
return ira->codegen->builtin_types.entry_invalid;
}
}
if (target->value.type->id != TypeTableEntryIdFloat) {
ir_add_error(ira, instruction->target, buf_sprintf("expected float type, found '%s'",
buf_ptr(&target->value.type->name)));
return ira->codegen->builtin_types.entry_invalid;
}
IrInstruction *result = ir_analyze_widen_or_shorten(ira, &instruction->base, target, dest_type);
if (type_is_invalid(result->value.type))
return ira->codegen->builtin_types.entry_invalid;
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInstructionIntToFloat *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
static TypeTableEntry *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstructionFloatToInt *instruction) {
TypeTableEntry *dest_type = ir_resolve_type(ira, instruction->dest_type->other);
if (type_is_invalid(dest_type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *target = instruction->target->other;
if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid;
IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpFloatToInt, false);
ir_link_new_instruction(result, &instruction->base);
return dest_type;
}
@ -19899,6 +20135,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_fence(ira, (IrInstructionFence *)instruction);
case IrInstructionIdTruncate:
return ir_analyze_instruction_truncate(ira, (IrInstructionTruncate *)instruction);
case IrInstructionIdIntCast:
return ir_analyze_instruction_int_cast(ira, (IrInstructionIntCast *)instruction);
case IrInstructionIdFloatCast:
return ir_analyze_instruction_float_cast(ira, (IrInstructionFloatCast *)instruction);
case IrInstructionIdIntToFloat:
return ir_analyze_instruction_int_to_float(ira, (IrInstructionIntToFloat *)instruction);
case IrInstructionIdFloatToInt:
return ir_analyze_instruction_float_to_int(ira, (IrInstructionFloatToInt *)instruction);
case IrInstructionIdIntType:
return ir_analyze_instruction_int_type(ira, (IrInstructionIntType *)instruction);
case IrInstructionIdBoolNot:
@ -20242,6 +20486,10 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdPromiseResultType:
case IrInstructionIdSqrt:
case IrInstructionIdAtomicLoad:
case IrInstructionIdIntCast:
case IrInstructionIdFloatCast:
case IrInstructionIdIntToFloat:
case IrInstructionIdFloatToInt:
return false;
case IrInstructionIdAsm:

View File

@ -648,6 +648,38 @@ static void ir_print_truncate(IrPrint *irp, IrInstructionTruncate *instruction)
fprintf(irp->f, ")");
}
static void ir_print_int_cast(IrPrint *irp, IrInstructionIntCast *instruction) {
fprintf(irp->f, "@intCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_cast(IrPrint *irp, IrInstructionFloatCast *instruction) {
fprintf(irp->f, "@floatCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_to_float(IrPrint *irp, IrInstructionIntToFloat *instruction) {
fprintf(irp->f, "@intToFloat(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_to_int(IrPrint *irp, IrInstructionFloatToInt *instruction) {
fprintf(irp->f, "@floatToInt(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_type(IrPrint *irp, IrInstructionIntType *instruction) {
fprintf(irp->f, "@IntType(");
ir_print_other_instruction(irp, instruction->is_signed);
@ -1417,6 +1449,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdTruncate:
ir_print_truncate(irp, (IrInstructionTruncate *)instruction);
break;
case IrInstructionIdIntCast:
ir_print_int_cast(irp, (IrInstructionIntCast *)instruction);
break;
case IrInstructionIdFloatCast:
ir_print_float_cast(irp, (IrInstructionFloatCast *)instruction);
break;
case IrInstructionIdIntToFloat:
ir_print_int_to_float(irp, (IrInstructionIntToFloat *)instruction);
break;
case IrInstructionIdFloatToInt:
ir_print_float_to_int(irp, (IrInstructionFloatToInt *)instruction);
break;
case IrInstructionIdIntType:
ir_print_int_type(irp, (IrInstructionIntType *)instruction);
break;

View File

@ -34,7 +34,7 @@ static int usage(const char *arg0) {
" --assembly [source] add assembly file to build\n"
" --cache-dir [path] override the cache directory\n"
" --color [auto|off|on] enable or disable colored error messages\n"
" --emit [filetype] emit a specific file format as compilation output\n"
" --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
" --enable-timing-info print timing diagnostics\n"
" --libc-include-dir [path] directory where libc stdlib.h resides\n"
" --name [name] override output name\n"

View File

@ -185,23 +185,23 @@ test "basic ArrayList test" {
{
var i: usize = 0;
while (i < 10) : (i += 1) {
list.append(i32(i + 1)) catch unreachable;
list.append(@intCast(i32, i + 1)) catch unreachable;
}
}
{
var i: usize = 0;
while (i < 10) : (i += 1) {
assert(list.items[i] == i32(i + 1));
assert(list.items[i] == @intCast(i32, i + 1));
}
}
for (list.toSlice()) |v, i| {
assert(v == i32(i + 1));
assert(v == @intCast(i32, i + 1));
}
for (list.toSliceConst()) |v, i| {
assert(v == i32(i + 1));
assert(v == @intCast(i32, i + 1));
}
assert(list.pop() == 10);

View File

@ -99,7 +99,7 @@ pub const Base64Decoder = struct {
assert(!result.char_in_alphabet[c]);
assert(c != pad_char);
result.char_to_index[c] = u8(i);
result.char_to_index[c] = @intCast(u8, i);
result.char_in_alphabet[c] = true;
}
@ -284,7 +284,7 @@ pub const Base64DecoderUnsafe = struct {
};
for (alphabet_chars) |c, i| {
assert(c != pad_char);
result.char_to_index[c] = u8(i);
result.char_to_index[c] = @intCast(u8, i);
}
return result;
}

View File

@ -79,7 +79,7 @@ fn Blake2s(comptime out_len: usize) type {
mem.copy(u32, d.h[0..], iv[0..]);
// No key plus default parameters
d.h[0] ^= 0x01010000 ^ u32(out_len >> 3);
d.h[0] ^= 0x01010000 ^ @intCast(u32, out_len >> 3);
d.t = 0;
d.buf_len = 0;
}
@ -110,7 +110,7 @@ fn Blake2s(comptime out_len: usize) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
}
pub fn final(d: *Self, out: []u8) void {
@ -144,7 +144,7 @@ fn Blake2s(comptime out_len: usize) type {
}
v[12] ^= @truncate(u32, d.t);
v[13] ^= u32(d.t >> 32);
v[13] ^= @intCast(u32, d.t >> 32);
if (last) v[14] = ~v[14];
const rounds = comptime []RoundParam{
@ -345,7 +345,7 @@ fn Blake2b(comptime out_len: usize) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
}
pub fn final(d: *Self, out: []u8) void {
@ -377,7 +377,7 @@ fn Blake2b(comptime out_len: usize) type {
}
v[12] ^= @truncate(u64, d.t);
v[13] ^= u64(d.t >> 64);
v[13] ^= @intCast(u64, d.t >> 64);
if (last) v[14] = ~v[14];
const rounds = comptime []RoundParam{

View File

@ -78,7 +78,7 @@ pub const Md5 = struct {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
// Md5 uses the bottom 64-bits for length padding
d.total_len +%= b.len;
@ -103,9 +103,9 @@ pub const Md5 = struct {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[56] = u8(d.total_len & 0x1f) << 3;
d.buf[56] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[56 + i] = u8(len & 0xff);
d.buf[56 + i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -78,7 +78,7 @@ pub const Sha1 = struct {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -102,9 +102,9 @@ pub const Sha1 = struct {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[63] = u8(d.total_len & 0x1f) << 3;
d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[63 - i] = u8(len & 0xff);
d.buf[63 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -131,7 +131,7 @@ fn Sha2_32(comptime params: Sha2Params32) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -155,9 +155,9 @@ fn Sha2_32(comptime params: Sha2Params32) type {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[63] = u8(d.total_len & 0x1f) << 3;
d.buf[63] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 8) : (i += 1) {
d.buf[63 - i] = u8(len & 0xff);
d.buf[63 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}
@ -472,7 +472,7 @@ fn Sha2_64(comptime params: Sha2Params64) type {
// Copy any remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.total_len += b.len;
}
@ -496,9 +496,9 @@ fn Sha2_64(comptime params: Sha2Params64) type {
// Append message length.
var i: usize = 1;
var len = d.total_len >> 5;
d.buf[127] = u8(d.total_len & 0x1f) << 3;
d.buf[127] = @intCast(u8, d.total_len & 0x1f) << 3;
while (i < 16) : (i += 1) {
d.buf[127 - i] = u8(len & 0xff);
d.buf[127 - i] = @intCast(u8, len & 0xff);
len >>= 8;
}

View File

@ -554,7 +554,7 @@ const LineNumberProgram = struct {
const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
errdefer self.file_entries.allocator.free(file_name);
return LineInfo{
.line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
.line = if (self.prev_line >= 0) @intCast(usize, self.prev_line) else 0,
.column = self.prev_column,
.file_name = file_name,
.allocator = self.file_entries.allocator,
@ -1070,7 +1070,7 @@ fn readULeb128(in_stream: var) !u64 {
var operand: u64 = undefined;
if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
if (@shlWithOverflow(u64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
result |= operand;
@ -1089,13 +1089,13 @@ fn readILeb128(in_stream: var) !i64 {
var operand: i64 = undefined;
if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
if (@shlWithOverflow(i64, byte & 0b01111111, @intCast(u6, shift), &operand)) return error.InvalidDebugInfo;
result |= operand;
shift += 7;
if ((byte & 0b10000000) == 0) {
if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << u6(shift));
if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << @intCast(u6, shift));
return result;
}
}

View File

@ -29,11 +29,11 @@ pub fn roundToPrecision(float_decimal: *FloatDecimal, precision: usize, mode: Ro
switch (mode) {
RoundMode.Decimal => {
if (float_decimal.exp >= 0) {
round_digit = precision + usize(float_decimal.exp);
round_digit = precision + @intCast(usize, float_decimal.exp);
} else {
// if a small negative exp, then adjust we need to offset by the number
// of leading zeros that will occur.
const min_exp_required = usize(-float_decimal.exp);
const min_exp_required = @intCast(usize, -float_decimal.exp);
if (precision > min_exp_required) {
round_digit = precision - min_exp_required;
}
@ -107,16 +107,16 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
// normalize the midpoint
const e = math.frexp(val).exponent;
var exp = i16(math.floor(307 + f64(e) * 0.30103));
var exp = @floatToInt(i16, math.floor(307 + @intToFloat(f64, e) * 0.30103));
if (exp < 20) {
exp = 20;
} else if (usize(exp) >= lookup_table.len) {
exp = i16(lookup_table.len - 1);
} else if (@intCast(usize, exp) >= lookup_table.len) {
exp = @intCast(i16, lookup_table.len - 1);
}
var mid = lookup_table[usize(exp)];
var mid = lookup_table[@intCast(usize, exp)];
mid = hpProd(mid, val);
const lten = lookup_table[usize(exp)].val;
const lten = lookup_table[@intCast(usize, exp)].val;
exp -= 307;
@ -168,25 +168,25 @@ fn errol3u(val: f64, buffer: []u8) FloatDecimal {
// the 0-index for this extra digit.
var buf_index: usize = 1;
while (true) {
var hdig = u8(math.floor(high.val));
if ((high.val == f64(hdig)) and (high.off < 0)) hdig -= 1;
var hdig = @floatToInt(u8, math.floor(high.val));
if ((high.val == @intToFloat(f64, hdig)) and (high.off < 0)) hdig -= 1;
var ldig = u8(math.floor(low.val));
if ((low.val == f64(ldig)) and (low.off < 0)) ldig -= 1;
var ldig = @floatToInt(u8, math.floor(low.val));
if ((low.val == @intToFloat(f64, ldig)) and (low.off < 0)) ldig -= 1;
if (ldig != hdig) break;
buffer[buf_index] = hdig + '0';
buf_index += 1;
high.val -= f64(hdig);
low.val -= f64(ldig);
high.val -= @intToFloat(f64, hdig);
low.val -= @intToFloat(f64, ldig);
hpMul10(&high);
hpMul10(&low);
}
const tmp = (high.val + low.val) / 2.0;
var mdig = u8(math.floor(tmp + 0.5));
if ((f64(mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
var mdig = @floatToInt(u8, math.floor(tmp + 0.5));
if ((@intToFloat(f64, mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;
buffer[buf_index] = mdig + '0';
buf_index += 1;
@ -304,7 +304,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
assert((val > 9.007199254740992e15) and val < (3.40282366920938e38));
var mid = u128(val);
var mid = @floatToInt(u128, val);
var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);
var high: u128 = mid + fpeint((val - fpprev(val)) / 2.0);
@ -314,11 +314,11 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
low -= 1;
}
var l64 = u64(low % pow19);
const lf = u64((low / pow19) % pow19);
var l64 = @intCast(u64, low % pow19);
const lf = @intCast(u64, (low / pow19) % pow19);
var h64 = u64(high % pow19);
const hf = u64((high / pow19) % pow19);
var h64 = @intCast(u64, high % pow19);
const hf = @intCast(u64, (high / pow19) % pow19);
if (lf != hf) {
l64 = lf;
@ -348,7 +348,7 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
return FloatDecimal{
.digits = buffer[0..buf_index],
.exp = i32(buf_index) + mi,
.exp = @intCast(i32, buf_index) + mi,
};
}
@ -359,33 +359,33 @@ fn errolInt(val: f64, buffer: []u8) FloatDecimal {
fn errolFixed(val: f64, buffer: []u8) FloatDecimal {
assert((val >= 16.0) and (val < 9.007199254740992e15));
const u = u64(val);
const n = f64(u);
const u = @floatToInt(u64, val);
const n = @intToFloat(f64, u);
var mid = val - n;
var lo = ((fpprev(val) - n) + mid) / 2.0;
var hi = ((fpnext(val) - n) + mid) / 2.0;
var buf_index = u64toa(u, buffer);
var exp = i32(buf_index);
var exp = @intCast(i32, buf_index);
var j = buf_index;
buffer[j] = 0;
if (mid != 0.0) {
while (mid != 0.0) {
lo *= 10.0;
const ldig = i32(lo);
lo -= f64(ldig);
const ldig = @floatToInt(i32, lo);
lo -= @intToFloat(f64, ldig);
mid *= 10.0;
const mdig = i32(mid);
mid -= f64(mdig);
const mdig = @floatToInt(i32, mid);
mid -= @intToFloat(f64, mdig);
hi *= 10.0;
const hdig = i32(hi);
hi -= f64(hdig);
const hdig = @floatToInt(i32, hi);
hi -= @intToFloat(f64, hdig);
buffer[j] = u8(mdig + '0');
buffer[j] = @intCast(u8, mdig + '0');
j += 1;
if (hdig != ldig or j > 50) break;
@ -452,7 +452,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
var buf_index: usize = 0;
if (value < kTen8) {
const v = u32(value);
const v = @intCast(u32, value);
if (v < 10000) {
const d1: u32 = (v / 100) << 1;
const d2: u32 = (v % 100) << 1;
@ -507,8 +507,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buf_index += 1;
}
} else if (value < kTen16) {
const v0: u32 = u32(value / kTen8);
const v1: u32 = u32(value % kTen8);
const v0: u32 = @intCast(u32, value / kTen8);
const v1: u32 = @intCast(u32, value % kTen8);
const b0: u32 = v0 / 10000;
const c0: u32 = v0 % 10000;
@ -578,11 +578,11 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buffer[buf_index] = c_digits_lut[d8 + 1];
buf_index += 1;
} else {
const a = u32(value / kTen16); // 1 to 1844
const a = @intCast(u32, value / kTen16); // 1 to 1844
value %= kTen16;
if (a < 10) {
buffer[buf_index] = '0' + u8(a);
buffer[buf_index] = '0' + @intCast(u8, a);
buf_index += 1;
} else if (a < 100) {
const i: u32 = a << 1;
@ -591,7 +591,7 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buffer[buf_index] = c_digits_lut[i + 1];
buf_index += 1;
} else if (a < 1000) {
buffer[buf_index] = '0' + u8(a / 100);
buffer[buf_index] = '0' + @intCast(u8, a / 100);
buf_index += 1;
const i: u32 = (a % 100) << 1;
@ -612,8 +612,8 @@ fn u64toa(value_param: u64, buffer: []u8) usize {
buf_index += 1;
}
const v0 = u32(value / kTen8);
const v1 = u32(value % kTen8);
const v0 = @intCast(u32, value / kTen8);
const v1 = @intCast(u32, value % kTen8);
const b0: u32 = v0 / 10000;
const c0: u32 = v0 % 10000;

View File

@ -5,6 +5,7 @@ const assert = debug.assert;
const mem = std.mem;
const builtin = @import("builtin");
const errol = @import("errol/index.zig");
const lossyCast = std.math.lossyCast;
const max_int_digits = 65;
@ -463,7 +464,7 @@ pub fn formatFloatDecimal(
errol.roundToPrecision(&float_decimal, precision, errol.RoundMode.Decimal);
// exp < 0 means the leading is always 0 as errol result is normalized.
var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
@ -492,7 +493,7 @@ pub fn formatFloatDecimal(
// Zero-fill until we reach significant digits or run out of precision.
if (float_decimal.exp <= 0) {
const zero_digit_count = usize(-float_decimal.exp);
const zero_digit_count = @intCast(usize, -float_decimal.exp);
const zeros_to_print = math.min(zero_digit_count, precision);
var i: usize = 0;
@ -521,7 +522,7 @@ pub fn formatFloatDecimal(
}
} else {
// exp < 0 means the leading is always 0 as errol result is normalized.
var num_digits_whole = if (float_decimal.exp > 0) usize(float_decimal.exp) else 0;
var num_digits_whole = if (float_decimal.exp > 0) @intCast(usize, float_decimal.exp) else 0;
// the actual slice into the buffer, we may need to zero-pad between num_digits_whole and this.
var num_digits_whole_no_pad = math.min(num_digits_whole, float_decimal.digits.len);
@ -547,7 +548,7 @@ pub fn formatFloatDecimal(
// Zero-fill until we reach significant digits or run out of precision.
if (float_decimal.exp < 0) {
const zero_digit_count = usize(-float_decimal.exp);
const zero_digit_count = @intCast(usize, -float_decimal.exp);
var i: usize = 0;
while (i < zero_digit_count) : (i += 1) {
@ -578,7 +579,7 @@ pub fn formatBytes(
1024 => math.min(math.log2(value) / 10, mags_iec.len - 1),
else => unreachable,
};
const new_value = f64(value) / math.pow(f64, f64(radix), f64(magnitude));
const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));
const suffix = switch (radix) {
1000 => mags_si[magnitude],
1024 => mags_iec[magnitude],
@ -628,15 +629,15 @@ fn formatIntSigned(
if (value < 0) {
const minus_sign: u8 = '-';
try output(context, (*[1]u8)(&minus_sign)[0..]);
const new_value = uint(-(value + 1)) + 1;
const new_value = @intCast(uint, -(value + 1)) + 1;
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
} else if (width == 0) {
return formatIntUnsigned(uint(value), base, uppercase, width, context, Errors, output);
return formatIntUnsigned(@intCast(uint, value), base, uppercase, width, context, Errors, output);
} else {
const plus_sign: u8 = '+';
try output(context, (*[1]u8)(&plus_sign)[0..]);
const new_value = uint(value);
const new_value = @intCast(uint, value);
const new_width = if (width == 0) 0 else (width - 1);
return formatIntUnsigned(new_value, base, uppercase, new_width, context, Errors, output);
}
@ -660,7 +661,7 @@ fn formatIntUnsigned(
while (true) {
const digit = a % base;
index -= 1;
buf[index] = digitToChar(u8(digit), uppercase);
buf[index] = digitToChar(@intCast(u8, digit), uppercase);
a /= base;
if (a == 0) break;
}

View File

@ -26,7 +26,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
var tables: [8][256]u32 = undefined;
for (tables[0]) |*e, i| {
var crc = u32(i);
var crc = @intCast(u32, i);
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
@ -122,7 +122,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
var table: [16]u32 = undefined;
for (table) |*e, i| {
var crc = u32(i * 16);
var crc = @intCast(u32, i * 16);
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {

View File

@ -81,7 +81,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
// Remainder for next pass.
mem.copy(u8, d.buf[d.buf_len..], b[off..]);
d.buf_len += u8(b[off..].len);
d.buf_len += @intCast(u8, b[off..].len);
d.msg_len +%= @truncate(u8, b.len);
}
@ -233,7 +233,7 @@ test "siphash64-2-4 sanity" {
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
buffer[i] = u8(i);
buffer[i] = @intCast(u8, i);
const expected = mem.readInt(vector, u64, Endian.Little);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
@ -312,7 +312,7 @@ test "siphash128-2-4 sanity" {
var buffer: [64]u8 = undefined;
for (vectors) |vector, i| {
buffer[i] = u8(i);
buffer[i] = @intCast(u8, i);
const expected = mem.readInt(vector, u128, Endian.Little);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);

View File

@ -408,7 +408,7 @@ fn testAllocator(allocator: *mem.Allocator) !void {
for (slice) |*item, i| {
item.* = try allocator.create(i32);
item.*.* = i32(i);
item.*.* = @intCast(i32, i);
}
for (slice) |item, i| {

View File

@ -180,7 +180,7 @@ pub const StreamingParser = struct {
pub fn fromInt(x: var) State {
debug.assert(x == 0 or x == 1);
const T = @TagType(State);
return State(T(x));
return State(@intCast(T, x));
}
};

View File

@ -95,12 +95,12 @@ fn acos64(x: f64) f64 {
const pio2_lo: f64 = 6.12323399573676603587e-17;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32);
const hx = @intCast(u32, ux >> 32);
const ix = hx & 0x7FFFFFFF;
// |x| >= 1 or nan
if (ix >= 0x3FF00000) {
const lx = u32(ux & 0xFFFFFFFF);
const lx = @intCast(u32, ux & 0xFFFFFFFF);
// acos(1) = 0, acos(-1) = pi
if ((ix - 0x3FF00000) | lx == 0) {

View File

@ -87,12 +87,12 @@ fn asin64(x: f64) f64 {
const pio2_lo: f64 = 6.12323399573676603587e-17;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32);
const hx = @intCast(u32, ux >> 32);
const ix = hx & 0x7FFFFFFF;
// |x| >= 1 or nan
if (ix >= 0x3FF00000) {
const lx = u32(ux & 0xFFFFFFFF);
const lx = @intCast(u32, ux & 0xFFFFFFFF);
// asin(1) = +-pi/2 with inexact
if ((ix - 0x3FF00000) | lx == 0) {

View File

@ -138,7 +138,7 @@ fn atan64(x_: f64) f64 {
var x = x_;
var ux = @bitCast(u64, x);
var ix = u32(ux >> 32);
var ix = @intCast(u32, ux >> 32);
const sign = ix >> 31;
ix &= 0x7FFFFFFF;
@ -159,7 +159,7 @@ fn atan64(x_: f64) f64 {
// |x| < 2^(-27)
if (ix < 0x3E400000) {
if (ix < 0x00100000) {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
}
return x;
}

View File

@ -124,12 +124,12 @@ fn atan2_64(y: f64, x: f64) f64 {
}
var ux = @bitCast(u64, x);
var ix = u32(ux >> 32);
var lx = u32(ux & 0xFFFFFFFF);
var ix = @intCast(u32, ux >> 32);
var lx = @intCast(u32, ux & 0xFFFFFFFF);
var uy = @bitCast(u64, y);
var iy = u32(uy >> 32);
var ly = u32(uy & 0xFFFFFFFF);
var iy = @intCast(u32, uy >> 32);
var ly = @intCast(u32, uy & 0xFFFFFFFF);
// x = 1.0
if ((ix -% 0x3FF00000) | lx == 0) {

View File

@ -62,7 +62,7 @@ fn atanh_64(x: f64) f64 {
if (e < 0x3FF - 32) {
// underflow
if (e == 0) {
math.forceEval(f32(y));
math.forceEval(@floatCast(f32, y));
}
}
// |x| < 0.5

View File

@ -135,7 +135,7 @@ pub const Int = struct {
self.positive = value >= 0;
self.len = 0;
var w_value: UT = if (value < 0) UT(-value) else UT(value);
var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value);
if (info.bits <= Limb.bit_count) {
self.limbs[0] = Limb(w_value);
@ -198,7 +198,7 @@ pub const Int = struct {
var r: UT = 0;
if (@sizeOf(UT) <= @sizeOf(Limb)) {
r = UT(self.limbs[0]);
r = @intCast(UT, self.limbs[0]);
} else {
for (self.limbs[0..self.len]) |_, ri| {
const limb = self.limbs[self.len - ri - 1];
@ -210,7 +210,7 @@ pub const Int = struct {
if (!T.is_signed) {
return if (self.positive) r else error.NegativeIntoUnsigned;
} else {
return if (self.positive) T(r) else -T(r);
return if (self.positive) @intCast(T, r) else -@intCast(T, r);
}
},
else => {
@ -295,7 +295,7 @@ pub const Int = struct {
for (self.limbs[0..self.len]) |limb| {
var shift: usize = 0;
while (shift < Limb.bit_count) : (shift += base_shift) {
const r = u8((limb >> Log2Limb(shift)) & Limb(base - 1));
const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & Limb(base - 1));
const ch = try digitToChar(r, base);
try digits.append(ch);
}
@ -329,7 +329,7 @@ pub const Int = struct {
var r_word = r.limbs[0];
var i: usize = 0;
while (i < digits_per_limb) : (i += 1) {
const ch = try digitToChar(u8(r_word % base), base);
const ch = try digitToChar(@intCast(u8, r_word % base), base);
r_word /= base;
try digits.append(ch);
}
@ -340,7 +340,7 @@ pub const Int = struct {
var r_word = q.limbs[0];
while (r_word != 0) {
const ch = try digitToChar(u8(r_word % base), base);
const ch = try digitToChar(@intCast(u8, r_word % base), base);
r_word /= base;
try digits.append(ch);
}
@ -801,7 +801,7 @@ pub const Int = struct {
q.limbs[i - t - 1] = @maxValue(Limb);
} else {
const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]);
const z = Limb(num / DoubleLimb(y.limbs[t]));
const z = @intCast(Limb, num / DoubleLimb(y.limbs[t]));
q.limbs[i - t - 1] = if (z > @maxValue(Limb)) @maxValue(Limb) else Limb(z);
}
@ -860,7 +860,7 @@ pub const Int = struct {
debug.assert(r.len >= a.len + (shift / Limb.bit_count) + 1);
const limb_shift = shift / Limb.bit_count + 1;
const interior_limb_shift = Log2Limb(shift % Limb.bit_count);
const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
var carry: Limb = 0;
var i: usize = 0;
@ -869,7 +869,7 @@ pub const Int = struct {
const dst_i = src_i + limb_shift;
const src_digit = a[src_i];
r[dst_i] = carry | @inlineCall(math.shr, Limb, src_digit, Limb.bit_count - Limb(interior_limb_shift));
r[dst_i] = carry | @inlineCall(math.shr, Limb, src_digit, Limb.bit_count - @intCast(Limb, interior_limb_shift));
carry = (src_digit << interior_limb_shift);
}
@ -898,7 +898,7 @@ pub const Int = struct {
debug.assert(r.len >= a.len - (shift / Limb.bit_count));
const limb_shift = shift / Limb.bit_count;
const interior_limb_shift = Log2Limb(shift % Limb.bit_count);
const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
var carry: Limb = 0;
var i: usize = 0;
@ -908,7 +908,7 @@ pub const Int = struct {
const src_digit = a[src_i];
r[dst_i] = carry | (src_digit >> interior_limb_shift);
carry = @inlineCall(math.shl, Limb, src_digit, Limb.bit_count - Limb(interior_limb_shift));
carry = @inlineCall(math.shl, Limb, src_digit, Limb.bit_count - @intCast(Limb, interior_limb_shift));
}
}

View File

@ -54,7 +54,7 @@ fn cbrt32(x: f32) f32 {
r = t * t * t;
t = t * (f64(x) + x + r) / (x + r + r);
return f32(t);
return @floatCast(f32, t);
}
fn cbrt64(x: f64) f64 {
@ -69,7 +69,7 @@ fn cbrt64(x: f64) f64 {
const P4: f64 = 0.145996192886612446982;
var u = @bitCast(u64, x);
var hx = u32(u >> 32) & 0x7FFFFFFF;
var hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
// cbrt(nan, inf) = itself
if (hx >= 0x7FF00000) {
@ -79,7 +79,7 @@ fn cbrt64(x: f64) f64 {
// cbrt to ~5bits
if (hx < 0x00100000) {
u = @bitCast(u64, x * 0x1.0p54);
hx = u32(u >> 32) & 0x7FFFFFFF;
hx = @intCast(u32, u >> 32) & 0x7FFFFFFF;
// cbrt(0) is itself
if (hx == 0) {

View File

@ -20,7 +20,7 @@ pub fn ceil(x: var) @typeOf(x) {
fn ceil32(x: f32) f32 {
var u = @bitCast(u32, x);
var e = i32((u >> 23) & 0xFF) - 0x7F;
var e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
var m: u32 = undefined;
// TODO: Shouldn't need this explicit check.
@ -31,7 +31,7 @@ fn ceil32(x: f32) f32 {
if (e >= 23) {
return x;
} else if (e >= 0) {
m = u32(0x007FFFFF) >> u5(e);
m = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & m == 0) {
return x;
}

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn atan(z: var) Complex(@typeOf(z.re)) {
pub fn atan(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => atan32(z),
@ -25,11 +25,11 @@ fn redupif32(x: f32) f32 {
t -= 0.5;
}
const u = f32(i32(t));
const u = @intToFloat(f32, @floatToInt(i32, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
fn atan32(z: *const Complex(f32)) Complex(f32) {
fn atan32(z: Complex(f32)) Complex(f32) {
const maxnum = 1.0e38;
const x = z.re;
@ -74,11 +74,11 @@ fn redupif64(x: f64) f64 {
t -= 0.5;
}
const u = f64(i64(t));
const u = @intToFloat(f64, @floatToInt(i64, t));
return ((x - u * DP1) - u * DP2) - t * DP3;
}
fn atan64(z: *const Complex(f64)) Complex(f64) {
fn atan64(z: Complex(f64)) Complex(f64) {
const maxnum = 1.0e308;
const x = z.re;

View File

@ -83,12 +83,12 @@ fn cosh64(z: *const Complex(f64)) Complex(f64) {
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
const hy = u32(fy >> 32);
const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;

View File

@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
pub fn exp(z: var) Complex(@typeOf(z.re)) {
pub fn exp(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@ -16,7 +16,7 @@ pub fn exp(z: var) Complex(@typeOf(z.re)) {
};
}
fn exp32(z: *const Complex(f32)) Complex(f32) {
fn exp32(z: Complex(f32)) Complex(f32) {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955
@ -63,7 +63,7 @@ fn exp32(z: *const Complex(f32)) Complex(f32) {
}
}
fn exp64(z: *const Complex(f64)) Complex(f64) {
fn exp64(z: Complex(f64)) Complex(f64) {
const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710
const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2

View File

@ -37,28 +37,28 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn add(self: *const Self, other: *const Self) Self {
pub fn add(self: Self, other: Self) Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
pub fn sub(self: *const Self, other: *const Self) Self {
pub fn sub(self: Self, other: Self) Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
pub fn mul(self: *const Self, other: *const Self) Self {
pub fn mul(self: Self, other: Self) Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
}
pub fn div(self: *const Self, other: *const Self) Self {
pub fn div(self: Self, other: Self) Self {
const re_num = self.re * other.re + self.im * other.im;
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
@ -69,14 +69,14 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn conjugate(self: *const Self) Self {
pub fn conjugate(self: Self) Self {
return Self{
.re = self.re,
.im = -self.im,
};
}
pub fn reciprocal(self: *const Self) Self {
pub fn reciprocal(self: Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self{
.re = self.re / m,
@ -84,7 +84,7 @@ pub fn Complex(comptime T: type) type {
};
}
pub fn magnitude(self: *const Self) T {
pub fn magnitude(self: Self) T {
return math.sqrt(self.re * self.re + self.im * self.im);
}
};

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) {
pub fn ldexp_cexp(z: var, expt: i32) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
@ -20,11 +20,12 @@ fn frexp_exp32(x: f32, expt: *i32) f32 {
const exp_x = math.exp(x - kln2);
const hx = @bitCast(u32, exp_x);
expt.* = i32(hx >> 23) - (0x7f + 127) + k;
// TODO zig should allow this cast implicitly because it should know the value is in range
expt.* = @intCast(i32, hx >> 23) - (0x7f + 127) + k;
return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23));
}
fn ldexp_cexp32(z: *const Complex(f32), expt: i32) Complex(f32) {
fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp32(z.re, &ex_expt);
const exptf = expt + ex_expt;
@ -45,16 +46,16 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
const exp_x = math.exp(x - kln2);
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
expt.* = i32(hx >> 20) - (0x3ff + 1023) + k;
expt.* = @intCast(i32, hx >> 20) - (0x3ff + 1023) + k;
const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
return @bitCast(f64, (u64(high_word) << 32) | lx);
}
fn ldexp_cexp64(z: *const Complex(f64), expt: i32) Complex(f64) {
fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
var ex_expt: i32 = undefined;
const exp_x = frexp_exp64(z.re, &ex_expt);
const exptf = i64(expt + ex_expt);

View File

@ -6,7 +6,7 @@ const Complex = cmath.Complex;
const ldexp_cexp = @import("ldexp.zig").ldexp_cexp;
pub fn sinh(z: var) Complex(@typeOf(z.re)) {
pub fn sinh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sinh32(z),
@ -15,7 +15,7 @@ pub fn sinh(z: var) Complex(@typeOf(z.re)) {
};
}
fn sinh32(z: *const Complex(f32)) Complex(f32) {
fn sinh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -78,17 +78,17 @@ fn sinh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y));
}
fn sinh64(z: *const Complex(f64)) Complex(f64) {
fn sinh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;
const fy = @bitCast(u64, y);
const hy = u32(fy >> 32);
const hy = @intCast(u32, fy >> 32);
const ly = @truncate(u32, fy);
const iy = hy & 0x7fffffff;

View File

@ -4,18 +4,17 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re))
pub fn sqrt(z: var) Complex(@typeOf(z.re)) {
pub fn sqrt(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => sqrt32(z),
f64 => sqrt64(z),
else => @compileError("sqrt not implemented for " ++ @typeName(z)),
else => @compileError("sqrt not implemented for " ++ @typeName(T)),
};
}
fn sqrt32(z: *const Complex(f32)) Complex(f32) {
fn sqrt32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -50,14 +49,20 @@ fn sqrt32(z: *const Complex(f32)) Complex(f32) {
if (dx >= 0) {
const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(f32(t), f32(dy / (2.0 * t)));
return Complex(f32).new(
@floatCast(f32, t),
@floatCast(f32, dy / (2.0 * t)),
);
} else {
const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5);
return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y)));
return Complex(f32).new(
@floatCast(f32, math.fabs(y) / (2.0 * t)),
@floatCast(f32, math.copysign(f64, t, y)),
);
}
}
fn sqrt64(z: *const Complex(f64)) Complex(f64) {
fn sqrt64(z: Complex(f64)) Complex(f64) {
// may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2))
const threshold = 0x1.a827999fcef32p+1022;

View File

@ -4,7 +4,7 @@ const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
pub fn tanh(z: var) Complex(@typeOf(z.re)) {
pub fn tanh(z: var) @typeOf(z) {
const T = @typeOf(z.re);
return switch (T) {
f32 => tanh32(z),
@ -13,7 +13,7 @@ pub fn tanh(z: var) Complex(@typeOf(z.re)) {
};
}
fn tanh32(z: *const Complex(f32)) Complex(f32) {
fn tanh32(z: Complex(f32)) Complex(f32) {
const x = z.re;
const y = z.im;
@ -51,12 +51,14 @@ fn tanh32(z: *const Complex(f32)) Complex(f32) {
return Complex(f32).new((beta * rho * s) / den, t / den);
}
fn tanh64(z: *const Complex(f64)) Complex(f64) {
fn tanh64(z: Complex(f64)) Complex(f64) {
const x = z.re;
const y = z.im;
const fx = @bitCast(u64, x);
const hx = u32(fx >> 32);
// TODO: zig should allow this conversion implicitly because it can notice that the value necessarily
// fits in range.
const hx = @intCast(u32, fx >> 32);
const lx = @truncate(u32, fx);
const ix = hx & 0x7fffffff;

View File

@ -55,7 +55,7 @@ fn cos32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -106,7 +106,7 @@ fn cos64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -49,7 +49,7 @@ fn cosh32(x: f32) f32 {
fn cosh64(x: f64) f64 {
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
// TODO: Shouldn't need this explicit check.

View File

@ -29,7 +29,7 @@ fn exp32(x_: f32) f32 {
var x = x_;
var hx = @bitCast(u32, x);
const sign = i32(hx >> 31);
const sign = @intCast(i32, hx >> 31);
hx &= 0x7FFFFFFF;
if (math.isNan(x)) {
@ -63,12 +63,12 @@ fn exp32(x_: f32) f32 {
if (hx > 0x3EB17218) {
// |x| > 1.5 * ln2
if (hx > 0x3F851592) {
k = i32(invln2 * x + half[usize(sign)]);
k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
} else {
k = 1 - sign - sign;
}
const fk = f32(k);
const fk = @intToFloat(f32, k);
hi = x - fk * ln2hi;
lo = fk * ln2lo;
x = hi - lo;
@ -110,7 +110,7 @@ fn exp64(x_: f64) f64 {
var x = x_;
var ux = @bitCast(u64, x);
var hx = ux >> 32;
const sign = i32(hx >> 31);
const sign = @intCast(i32, hx >> 31);
hx &= 0x7FFFFFFF;
if (math.isNan(x)) {
@ -148,12 +148,12 @@ fn exp64(x_: f64) f64 {
if (hx > 0x3EB17218) {
// |x| >= 1.5 * ln2
if (hx > 0x3FF0A2B2) {
k = i32(invln2 * x + half[usize(sign)]);
k = @floatToInt(i32, invln2 * x + half[@intCast(usize, sign)]);
} else {
k = 1 - sign - sign;
}
const dk = f64(k);
const dk = @intToFloat(f64, k);
hi = x - dk * ln2hi;
lo = dk * ln2lo;
x = hi - lo;

View File

@ -38,8 +38,8 @@ const exp2ft = []const f64{
fn exp2_32(x: f32) f32 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const tblsiz = u32(exp2ft.len);
const redux: f32 = 0x1.8p23 / f32(tblsiz);
const tblsiz = @intCast(u32, exp2ft.len);
const redux: f32 = 0x1.8p23 / @intToFloat(f32, tblsiz);
const P1: f32 = 0x1.62e430p-1;
const P2: f32 = 0x1.ebfbe0p-3;
const P3: f32 = 0x1.c6b348p-5;
@ -89,7 +89,7 @@ fn exp2_32(x: f32) f32 {
var r: f64 = exp2ft[i0];
const t: f64 = r * z;
r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
return f32(r * uk);
return @floatCast(f32, r * uk);
}
const exp2dt = []f64{
@ -355,8 +355,8 @@ const exp2dt = []f64{
fn exp2_64(x: f64) f64 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const tblsiz = u32(exp2dt.len / 2);
const redux: f64 = 0x1.8p52 / f64(tblsiz);
const tblsiz = @intCast(u32, exp2dt.len / 2);
const redux: f64 = 0x1.8p52 / @intToFloat(f64, tblsiz);
const P1: f64 = 0x1.62e42fefa39efp-1;
const P2: f64 = 0x1.ebfbdff82c575p-3;
const P3: f64 = 0x1.c6b08d704a0a6p-5;
@ -364,7 +364,7 @@ fn exp2_64(x: f64) f64 {
const P5: f64 = 0x1.5d88003875c74p-10;
const ux = @bitCast(u64, x);
const ix = u32(ux >> 32) & 0x7FFFFFFF;
const ix = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
// TODO: This should be handled beneath.
if (math.isNan(x)) {
@ -386,7 +386,7 @@ fn exp2_64(x: f64) f64 {
if (ux >> 63 != 0) {
// underflow
if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
math.forceEval(f32(-0x1.0p-149 / x));
math.forceEval(@floatCast(f32, -0x1.0p-149 / x));
}
if (x <= -1075) {
return 0;

View File

@ -78,8 +78,8 @@ fn expm1_32(x_: f32) f32 {
kf += 0.5;
}
k = i32(kf);
const t = f32(k);
k = @floatToInt(i32, kf);
const t = @intToFloat(f32, k);
hi = x - t * ln2_hi;
lo = t * ln2_lo;
}
@ -123,7 +123,7 @@ fn expm1_32(x_: f32) f32 {
}
}
const twopk = @bitCast(f32, u32((0x7F +% k) << 23));
const twopk = @bitCast(f32, @intCast(u32, (0x7F +% k) << 23));
if (k < 0 or k > 56) {
var y = x - e + 1.0;
@ -136,7 +136,7 @@ fn expm1_32(x_: f32) f32 {
return y - 1.0;
}
const uf = @bitCast(f32, u32(0x7F -% k) << 23);
const uf = @bitCast(f32, @intCast(u32, 0x7F -% k) << 23);
if (k < 23) {
return (x - e + (1 - uf)) * twopk;
} else {
@ -158,7 +158,7 @@ fn expm1_64(x_: f64) f64 {
var x = x_;
const ux = @bitCast(u64, x);
const hx = u32(ux >> 32) & 0x7FFFFFFF;
const hx = @intCast(u32, ux >> 32) & 0x7FFFFFFF;
const sign = ux >> 63;
if (math.isNegativeInf(x)) {
@ -207,8 +207,8 @@ fn expm1_64(x_: f64) f64 {
kf += 0.5;
}
k = i32(kf);
const t = f64(k);
k = @floatToInt(i32, kf);
const t = @intToFloat(f64, k);
hi = x - t * ln2_hi;
lo = t * ln2_lo;
}
@ -219,7 +219,7 @@ fn expm1_64(x_: f64) f64 {
// |x| < 2^(-54)
else if (hx < 0x3C900000) {
if (hx < 0x00100000) {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
}
return x;
} else {
@ -252,7 +252,7 @@ fn expm1_64(x_: f64) f64 {
}
}
const twopk = @bitCast(f64, u64(0x3FF +% k) << 52);
const twopk = @bitCast(f64, @intCast(u64, 0x3FF +% k) << 52);
if (k < 0 or k > 56) {
var y = x - e + 1.0;
@ -265,7 +265,7 @@ fn expm1_64(x_: f64) f64 {
return y - 1.0;
}
const uf = @bitCast(f64, u64(0x3FF -% k) << 52);
const uf = @bitCast(f64, @intCast(u64, 0x3FF -% k) << 52);
if (k < 20) {
return (x - e + (1 - uf)) * twopk;
} else {

View File

@ -20,7 +20,7 @@ pub fn floor(x: var) @typeOf(x) {
fn floor32(x: f32) f32 {
var u = @bitCast(u32, x);
const e = i32((u >> 23) & 0xFF) - 0x7F;
const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
var m: u32 = undefined;
// TODO: Shouldn't need this explicit check.
@ -33,7 +33,7 @@ fn floor32(x: f32) f32 {
}
if (e >= 0) {
m = u32(0x007FFFFF) >> u5(e);
m = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & m == 0) {
return x;
}

View File

@ -17,10 +17,10 @@ fn fma32(x: f32, y: f32, z: f32) f32 {
const e = (u >> 52) & 0x7FF;
if ((u & 0x1FFFFFFF) != 0x10000000 or e == 0x7FF or xy_z - xy == z) {
return f32(xy_z);
return @floatCast(f32, xy_z);
} else {
// TODO: Handle inexact case with double-rounding
return f32(xy_z);
return @floatCast(f32, xy_z);
}
}
@ -124,7 +124,7 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 {
var sum = dd_add(a, b);
if (sum.lo != 0) {
var uhii = @bitCast(u64, sum.hi);
const bits_lost = -i32((uhii >> 52) & 0x7FF) - scale + 1;
const bits_lost = -@intCast(i32, (uhii >> 52) & 0x7FF) - scale + 1;
if ((bits_lost != 1) == (uhii & 1 != 0)) {
const uloi = @bitCast(u64, sum.lo);
uhii += 1 - (((uhii ^ uloi) >> 62) & 2);

View File

@ -30,7 +30,7 @@ fn frexp32(x: f32) frexp32_result {
var result: frexp32_result = undefined;
var y = @bitCast(u32, x);
const e = i32(y >> 23) & 0xFF;
const e = @intCast(i32, y >> 23) & 0xFF;
if (e == 0) {
if (x != 0) {
@ -67,7 +67,7 @@ fn frexp64(x: f64) frexp64_result {
var result: frexp64_result = undefined;
var y = @bitCast(u64, x);
const e = i32(y >> 52) & 0x7FF;
const e = @intCast(i32, y >> 52) & 0x7FF;
if (e == 0) {
if (x != 0) {

View File

@ -49,7 +49,7 @@ fn hypot32(x: f32, y: f32) f32 {
yy *= 0x1.0p-90;
}
return z * math.sqrt(f32(f64(x) * x + f64(y) * y));
return z * math.sqrt(@floatCast(f32, f64(x) * x + f64(y) * y));
}
fn sq(hi: *f64, lo: *f64, x: f64) void {

View File

@ -23,7 +23,7 @@ const fp_ilogb0 = fp_ilogbnan;
fn ilogb32(x: f32) i32 {
var u = @bitCast(u32, x);
var e = i32((u >> 23) & 0xFF);
var e = @intCast(i32, (u >> 23) & 0xFF);
// TODO: We should be able to merge this with the lower check.
if (math.isNan(x)) {
@ -59,7 +59,7 @@ fn ilogb32(x: f32) i32 {
fn ilogb64(x: f64) i32 {
var u = @bitCast(u64, x);
var e = i32((u >> 52) & 0x7FF);
var e = @intCast(i32, (u >> 52) & 0x7FF);
if (math.isNan(x)) {
return @maxValue(i32);

View File

@ -227,7 +227,7 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
/// A negative shift amount results in a right shift.
pub fn shl(comptime T: type, a: T, shift_amt: var) T {
const abs_shift_amt = absCast(shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
if (@typeOf(shift_amt).is_signed) {
if (shift_amt >= 0) {
@ -251,7 +251,7 @@ test "math.shl" {
/// A negative shift amount results in a lefft shift.
pub fn shr(comptime T: type, a: T, shift_amt: var) T {
const abs_shift_amt = absCast(shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else Log2Int(T)(abs_shift_amt);
const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
if (@typeOf(shift_amt).is_signed) {
if (shift_amt >= 0) {
@ -473,9 +473,9 @@ fn testRem() void {
/// Result is an unsigned integer.
pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
const uint = @IntType(false, @typeOf(x).bit_count);
if (x >= 0) return uint(x);
if (x >= 0) return @intCast(uint, x);
return uint(-(x + 1)) + 1;
return @intCast(uint, -(x + 1)) + 1;
}
test "math.absCast" {
@ -499,7 +499,7 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
if (x == -@minValue(int)) return @minValue(int);
return -int(x);
return -@intCast(int, x);
}
test "math.negateCast" {
@ -522,7 +522,7 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
} else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {
return error.Overflow;
} else {
return T(x);
return @intCast(T, x);
}
}
@ -565,7 +565,7 @@ test "math.floorPowerOfTwo" {
pub fn log2_int(comptime T: type, x: T) Log2Int(T) {
assert(x != 0);
return Log2Int(T)(T.bit_count - 1 - @clz(x));
return @intCast(Log2Int(T), T.bit_count - 1 - @clz(x));
}
pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
@ -597,3 +597,14 @@ fn testFloorPowerOfTwo() void {
assert(floorPowerOfTwo(u4, 8) == 8);
assert(floorPowerOfTwo(u4, 9) == 8);
}
pub fn lossyCast(comptime T: type, value: var) T {
switch (@typeInfo(@typeOf(value))) {
builtin.TypeId.Int => return @intToFloat(T, value),
builtin.TypeId.Float => return @floatCast(T, value),
builtin.TypeId.ComptimeInt => return T(value),
builtin.TypeId.ComptimeFloat => return T(value),
else => @compileError("bad type"),
}
}

View File

@ -71,7 +71,7 @@ pub fn ln_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -83,7 +83,7 @@ pub fn ln_32(x_: f32) f32 {
const t2 = z * (Lg1 + w * Lg3);
const R = t2 + t1;
const hfsq = 0.5 * f * f;
const dk = f32(k);
const dk = @intToFloat(f32, k);
return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
}
@ -103,7 +103,7 @@ pub fn ln_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -119,7 +119,7 @@ pub fn ln_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, ix) >> 32);
hx = @intCast(u32, @bitCast(u64, ix) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -128,7 +128,7 @@ pub fn ln_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -141,7 +141,7 @@ pub fn ln_64(x_: f64) f64 {
const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
const R = t2 + t1;
const dk = f64(k);
const dk = @intToFloat(f64, k);
return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi;
}

View File

@ -13,22 +13,23 @@ pub fn log(comptime T: type, base: T, x: T) T {
return math.ln(x);
}
const float_base = math.lossyCast(f64, base);
switch (@typeId(T)) {
TypeId.ComptimeFloat => {
return @typeOf(1.0)(math.ln(f64(x)) / math.ln(f64(base)));
return @typeOf(1.0)(math.ln(f64(x)) / math.ln(float_base));
},
TypeId.ComptimeInt => {
return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
return @typeOf(1)(math.floor(math.ln(f64(x)) / math.ln(float_base)));
},
builtin.TypeId.Int => {
// TODO implement integer log without using float math
return T(math.floor(math.ln(f64(x)) / math.ln(f64(base))));
return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
},
builtin.TypeId.Float => {
switch (T) {
f32 => return f32(math.ln(f64(x)) / math.ln(f64(base))),
f64 => return math.ln(x) / math.ln(f64(base)),
f32 => return @floatCast(f32, math.ln(f64(x)) / math.ln(float_base)),
f64 => return math.ln(x) / math.ln(float_base),
else => @compileError("log not implemented for " ++ @typeName(T)),
}
},

View File

@ -28,7 +28,7 @@ pub fn log10(x: var) @typeOf(x) {
return @typeOf(1)(math.floor(log10_64(f64(x))));
},
TypeId.Int => {
return T(math.floor(log10_64(f64(x))));
return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
},
else => @compileError("log10 not implemented for " ++ @typeName(T)),
}
@ -71,7 +71,7 @@ pub fn log10_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -89,7 +89,7 @@ pub fn log10_32(x_: f32) f32 {
u &= 0xFFFFF000;
hi = @bitCast(f32, u);
const lo = f - hi - hfsq + s * (hfsq + R);
const dk = f32(k);
const dk = @intToFloat(f32, k);
return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi;
}
@ -109,7 +109,7 @@ pub fn log10_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -125,7 +125,7 @@ pub fn log10_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, x) >> 32);
hx = @intCast(u32, @bitCast(u64, x) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -134,7 +134,7 @@ pub fn log10_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -157,7 +157,7 @@ pub fn log10_64(x_: f64) f64 {
// val_hi + val_lo ~ log10(1 + f) + k * log10(2)
var val_hi = hi * ivln10hi;
const dk = f64(k);
const dk = @intToFloat(f64, k);
const y = dk * log10_2hi;
var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;

View File

@ -68,7 +68,7 @@ fn log1p_32(x: f32) f32 {
const uf = 1 + x;
var iu = @bitCast(u32, uf);
iu += 0x3F800000 - 0x3F3504F3;
k = i32(iu >> 23) - 0x7F;
k = @intCast(i32, iu >> 23) - 0x7F;
// correction to avoid underflow in c / u
if (k < 25) {
@ -90,7 +90,7 @@ fn log1p_32(x: f32) f32 {
const t2 = z * (Lg1 + w * Lg3);
const R = t2 + t1;
const hfsq = 0.5 * f * f;
const dk = f32(k);
const dk = @intToFloat(f32, k);
return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}
@ -107,7 +107,7 @@ fn log1p_64(x: f64) f64 {
const Lg7: f64 = 1.479819860511658591e-01;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 1;
var c: f64 = undefined;
var f: f64 = undefined;
@ -145,9 +145,9 @@ fn log1p_64(x: f64) f64 {
if (k != 0) {
const uf = 1 + x;
const hu = @bitCast(u64, uf);
var iu = u32(hu >> 32);
var iu = @intCast(u32, hu >> 32);
iu += 0x3FF00000 - 0x3FE6A09E;
k = i32(iu >> 20) - 0x3FF;
k = @intCast(i32, iu >> 20) - 0x3FF;
// correction to avoid underflow in c / u
if (k < 54) {
@ -170,7 +170,7 @@ fn log1p_64(x: f64) f64 {
const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
const R = t2 + t1;
const dk = f64(k);
const dk = @intToFloat(f64, k);
return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi;
}

View File

@ -75,7 +75,7 @@ pub fn log2_32(x_: f32) f32 {
// x into [sqrt(2) / 2, sqrt(2)]
ix += 0x3F800000 - 0x3F3504F3;
k += i32(ix >> 23) - 0x7F;
k += @intCast(i32, ix >> 23) - 0x7F;
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
x = @bitCast(f32, ix);
@ -93,7 +93,7 @@ pub fn log2_32(x_: f32) f32 {
u &= 0xFFFFF000;
hi = @bitCast(f32, u);
const lo = f - hi - hfsq + s * (hfsq + R);
return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + f32(k);
return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + @intToFloat(f32, k);
}
pub fn log2_64(x_: f64) f64 {
@ -109,7 +109,7 @@ pub fn log2_64(x_: f64) f64 {
var x = x_;
var ix = @bitCast(u64, x);
var hx = u32(ix >> 32);
var hx = @intCast(u32, ix >> 32);
var k: i32 = 0;
if (hx < 0x00100000 or hx >> 31 != 0) {
@ -125,7 +125,7 @@ pub fn log2_64(x_: f64) f64 {
// subnormal, scale x
k -= 54;
x *= 0x1.0p54;
hx = u32(@bitCast(u64, x) >> 32);
hx = @intCast(u32, @bitCast(u64, x) >> 32);
} else if (hx >= 0x7FF00000) {
return x;
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@ -134,7 +134,7 @@ pub fn log2_64(x_: f64) f64 {
// x into [sqrt(2) / 2, sqrt(2)]
hx += 0x3FF00000 - 0x3FE6A09E;
k += i32(hx >> 20) - 0x3FF;
k += @intCast(i32, hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
x = @bitCast(f64, ix);
@ -159,7 +159,7 @@ pub fn log2_64(x_: f64) f64 {
var val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
// spadd(val_hi, val_lo, y)
const y = f64(k);
const y = @intToFloat(f64, k);
const ww = y + val_hi;
val_lo += (y - ww) + val_hi;
val_hi = ww;

View File

@ -29,7 +29,7 @@ fn modf32(x: f32) modf32_result {
var result: modf32_result = undefined;
const u = @bitCast(u32, x);
const e = i32((u >> 23) & 0xFF) - 0x7F;
const e = @intCast(i32, (u >> 23) & 0xFF) - 0x7F;
const us = u & 0x80000000;
// TODO: Shouldn't need this.
@ -57,7 +57,7 @@ fn modf32(x: f32) modf32_result {
return result;
}
const mask = u32(0x007FFFFF) >> u5(e);
const mask = u32(0x007FFFFF) >> @intCast(u5, e);
if (u & mask == 0) {
result.ipart = x;
result.fpart = @bitCast(f32, us);
@ -74,7 +74,7 @@ fn modf64(x: f64) modf64_result {
var result: modf64_result = undefined;
const u = @bitCast(u64, x);
const e = i32((u >> 52) & 0x7FF) - 0x3FF;
const e = @intCast(i32, (u >> 52) & 0x7FF) - 0x3FF;
const us = u & (1 << 63);
if (math.isInf(x)) {
@ -101,7 +101,7 @@ fn modf64(x: f64) modf64_result {
return result;
}
const mask = u64(@maxValue(u64) >> 12) >> u6(e);
const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e);
if (u & mask == 0) {
result.ipart = x;
result.fpart = @bitCast(f64, us);

View File

@ -146,7 +146,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
var xe = r2.exponent;
var x1 = r2.significand;
var i = i32(yi);
var i = @floatToInt(i32, yi);
while (i != 0) : (i >>= 1) {
if (i & 1 == 1) {
a1 *= x1;
@ -171,7 +171,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
fn isOddInteger(x: f64) bool {
const r = math.modf(x);
return r.fpart == 0.0 and i64(r.ipart) & 1 == 1;
return r.fpart == 0.0 and @floatToInt(i64, r.ipart) & 1 == 1;
}
test "math.pow" {

View File

@ -37,7 +37,7 @@ fn scalbn32(x: f32, n_: i32) f32 {
}
}
const u = u32(n +% 0x7F) << 23;
const u = @intCast(u32, n +% 0x7F) << 23;
return y * @bitCast(f32, u);
}
@ -67,7 +67,7 @@ fn scalbn64(x: f64, n_: i32) f64 {
}
}
const u = u64(n +% 0x3FF) << 52;
const u = @intCast(u64, n +% 0x3FF) << 52;
return y * @bitCast(f64, u);
}

View File

@ -60,7 +60,7 @@ fn sin32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -112,7 +112,7 @@ fn sin64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -57,7 +57,7 @@ fn sinh64(x: f64) f64 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
if (x == 0.0 or math.isNan(x)) {

View File

@ -99,7 +99,7 @@ fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) {
}
const ResultType = @IntType(false, T.bit_count / 2);
return ResultType(res);
return @intCast(ResultType, res);
}
test "math.sqrt_int" {

View File

@ -53,7 +53,7 @@ fn tan32(x_: f32) f32 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;
@ -102,7 +102,7 @@ fn tan64(x_: f64) f64 {
}
var y = math.floor(x * m4pi);
var j = i64(y);
var j = @floatToInt(i64, y);
if (j & 1 == 1) {
j += 1;

View File

@ -68,7 +68,7 @@ fn tanh32(x: f32) f32 {
fn tanh64(x: f64) f64 {
const u = @bitCast(u64, x);
const w = u32(u >> 32);
const w = @intCast(u32, u >> 32);
const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));
var t: f64 = undefined;
@ -100,7 +100,7 @@ fn tanh64(x: f64) f64 {
}
// |x| is subnormal
else {
math.forceEval(f32(x));
math.forceEval(@floatCast(f32, x));
t = x;
}

View File

@ -19,7 +19,7 @@ pub fn trunc(x: var) @typeOf(x) {
fn trunc32(x: f32) f32 {
const u = @bitCast(u32, x);
var e = i32(((u >> 23) & 0xFF)) - 0x7F + 9;
var e = @intCast(i32, ((u >> 23) & 0xFF)) - 0x7F + 9;
var m: u32 = undefined;
if (e >= 23 + 9) {
@ -29,7 +29,7 @@ fn trunc32(x: f32) f32 {
e = 1;
}
m = u32(@maxValue(u32)) >> u5(e);
m = u32(@maxValue(u32)) >> @intCast(u5, e);
if (u & m == 0) {
return x;
} else {
@ -40,7 +40,7 @@ fn trunc32(x: f32) f32 {
fn trunc64(x: f64) f64 {
const u = @bitCast(u64, x);
var e = i32(((u >> 52) & 0x7FF)) - 0x3FF + 12;
var e = @intCast(i32, ((u >> 52) & 0x7FF)) - 0x3FF + 12;
var m: u64 = undefined;
if (e >= 52 + 12) {
@ -50,7 +50,7 @@ fn trunc64(x: f64) f64 {
e = 1;
}
m = u64(@maxValue(u64)) >> u6(e);
m = u64(@maxValue(u64)) >> @intCast(u6, e);
if (u & m == 0) {
return x;
} else {

View File

@ -334,7 +334,7 @@ pub fn readInt(bytes: []const u8, comptime T: type, endian: builtin.Endian) T {
builtin.Endian.Little => {
const ShiftType = math.Log2Int(T);
for (bytes) |b, index| {
result = result | (T(b) << ShiftType(index * 8));
result = result | (T(b) << @intCast(ShiftType, index * 8));
}
},
}

View File

@ -413,7 +413,7 @@ pub const ChildProcess = struct {
}
// we are the parent
const pid = i32(pid_result);
const pid = @intCast(i32, pid_result);
if (self.stdin_behavior == StdIo.Pipe) {
self.stdin = os.File.openHandle(stdin_pipe[1]);
} else {

View File

@ -290,7 +290,7 @@ pub fn WIFSIGNALED(x: i32) bool {
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}
pub fn close(fd: i32) usize {
@ -339,7 +339,14 @@ pub fn write(fd: i32, buf: [*]const u8, nbyte: usize) usize {
}
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
const ptr_result = c.mmap(@ptrCast(*c_void, address), length, @bitCast(c_int, c_uint(prot)), @bitCast(c_int, c_uint(flags)), fd, offset);
const ptr_result = c.mmap(
@ptrCast(*c_void, address),
length,
@bitCast(c_int, @intCast(c_uint, prot)),
@bitCast(c_int, c_uint(flags)),
fd,
offset,
);
const isize_result = @bitCast(isize, @ptrToInt(ptr_result));
return errnoWrap(isize_result);
}

View File

@ -266,7 +266,7 @@ pub const File = struct {
pub fn getEndPos(self: *File) !usize {
if (is_posix) {
const stat = try os.posixFStat(self.handle);
return usize(stat.size);
return @intCast(usize, stat.size);
} else if (is_windows) {
var file_size: windows.LARGE_INTEGER = undefined;
if (windows.GetFileSizeEx(self.handle, &file_size) == 0) {
@ -277,7 +277,7 @@ pub const File = struct {
}
if (file_size < 0)
return error.Overflow;
return math.cast(usize, u64(file_size));
return math.cast(usize, @intCast(u64, file_size));
} else {
@compileError("TODO support getEndPos on this OS");
}
@ -343,7 +343,7 @@ pub const File = struct {
} else if (is_windows) {
var index: usize = 0;
while (index < buffer.len) {
const want_read_count = windows.DWORD(math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
const want_read_count = @intCast(windows.DWORD, math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));
var amt_read: windows.DWORD = undefined;
if (windows.ReadFile(self.handle, @ptrCast(*c_void, buffer.ptr + index), want_read_count, &amt_read, null) == 0) {
const err = windows.GetLastError();

View File

@ -126,7 +126,7 @@ pub fn getRandomBytes(buf: []u8) !void {
}
defer _ = windows.CryptReleaseContext(hCryptProv, 0);
if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) {
if (windows.CryptGenRandom(hCryptProv, @intCast(windows.DWORD, buf.len), buf.ptr) == 0) {
const err = windows.GetLastError();
return switch (err) {
else => unexpectedErrorWindows(err),
@ -343,7 +343,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 {
else => return unexpectedErrorPosix(err),
}
}
return i32(result);
return @intCast(i32, result);
}
}
@ -586,7 +586,7 @@ pub fn getCwd(allocator: *Allocator) ![]u8 {
errdefer allocator.free(buf);
while (true) {
const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
const result = windows.GetCurrentDirectoryA(@intCast(windows.WORD, buf.len), buf.ptr);
if (result == 0) {
const err = windows.GetLastError();
@ -2019,7 +2019,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
const rc = posix.socket(domain, socket_type, protocol);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
posix.EACCES => return PosixSocketError.PermissionDenied,
posix.EAFNOSUPPORT => return PosixSocketError.AddressFamilyNotSupported,
posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable,
@ -2183,7 +2183,7 @@ pub fn posixAccept(fd: i32, addr: *posix.sockaddr, flags: u32) PosixAcceptError!
const rc = posix.accept4(fd, addr, &sockaddr_size, flags);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
posix.EINTR => continue,
else => return unexpectedErrorPosix(err),
@ -2226,7 +2226,7 @@ pub fn linuxEpollCreate(flags: u32) LinuxEpollCreateError!i32 {
const rc = posix.epoll_create1(flags);
const err = posix.getErrno(rc);
switch (err) {
0 => return i32(rc),
0 => return @intCast(i32, rc),
else => return unexpectedErrorPosix(err),
posix.EINVAL => return LinuxEpollCreateError.InvalidSyscall,
@ -2296,7 +2296,7 @@ pub fn linuxEpollCtl(epfd: i32, op: u32, fd: i32, event: *linux.epoll_event) Lin
pub fn linuxEpollWait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
while (true) {
const rc = posix.epoll_wait(epfd, events.ptr, u32(events.len), timeout);
const rc = posix.epoll_wait(epfd, events.ptr, @intCast(u32, events.len), timeout);
const err = posix.getErrno(rc);
switch (err) {
0 => return rc,
@ -2661,7 +2661,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
posix.EAGAIN => return SpawnThreadError.SystemResources,
posix.EPERM => unreachable,
posix.EINVAL => unreachable,
else => return unexpectedErrorPosix(usize(err)),
else => return unexpectedErrorPosix(@intCast(usize, err)),
}
} else if (builtin.os == builtin.Os.linux) {
// use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly

View File

@ -642,7 +642,7 @@ pub fn WIFEXITED(s: i32) bool {
return WTERMSIG(s) == 0;
}
pub fn WIFSTOPPED(s: i32) bool {
return (u16)(((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
return @intCast(u16, ((unsigned(s) & 0xffff) *% 0x10001) >> 8) > 0x7f00;
}
pub fn WIFSIGNALED(s: i32) bool {
return (unsigned(s) & 0xffff) -% 1 < 0xff;
@ -658,11 +658,11 @@ pub const winsize = extern struct {
/// Get the errno from a syscall return value, or 0 for no error.
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0;
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}
pub fn dup2(old: i32, new: i32) usize {
return syscall2(SYS_dup2, usize(old), usize(new));
return syscall2(SYS_dup2, @intCast(usize, old), @intCast(usize, new));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -693,12 +693,12 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
}
pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
return syscall3(SYS_getdents, usize(fd), @ptrToInt(dirp), count);
return syscall3(SYS_getdents, @intCast(usize, fd), @ptrToInt(dirp), count);
}
pub fn isatty(fd: i32) bool {
var wsz: winsize = undefined;
return syscall3(SYS_ioctl, usize(fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
return syscall3(SYS_ioctl, @intCast(usize, fd), TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
}
// TODO https://github.com/ziglang/zig/issues/265
@ -727,7 +727,7 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
}
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), @bitCast(usize, offset));
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @intCast(usize, fd), @bitCast(usize, offset));
}
pub fn munmap(address: usize, length: usize) usize {
@ -735,7 +735,7 @@ pub fn munmap(address: usize, length: usize) usize {
}
pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
return syscall3(SYS_read, usize(fd), @ptrToInt(buf), count);
return syscall3(SYS_read, @intCast(usize, fd), @ptrToInt(buf), count);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -749,7 +749,7 @@ pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
}
pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
return syscall4(SYS_pread, usize(fd), @ptrToInt(buf), count, offset);
return syscall4(SYS_pread, @intCast(usize, fd), @ptrToInt(buf), count, offset);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -766,11 +766,11 @@ pub fn pipe2(fd: *[2]i32, flags: usize) usize {
}
pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
return syscall3(SYS_write, usize(fd), @ptrToInt(buf), count);
return syscall3(SYS_write, @intCast(usize, fd), @ptrToInt(buf), count);
}
pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
return syscall4(SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset);
return syscall4(SYS_pwrite, @intCast(usize, fd), @ptrToInt(buf), count, offset);
}
// TODO https://github.com/ziglang/zig/issues/265
@ -790,7 +790,7 @@ pub fn create(path: [*]const u8, perm: usize) usize {
// TODO https://github.com/ziglang/zig/issues/265
pub fn openat(dirfd: i32, path: [*]const u8, flags: usize, mode: usize) usize {
return syscall4(SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode);
return syscall4(SYS_openat, @intCast(usize, dirfd), @ptrToInt(path), flags, mode);
}
/// See also `clone` (from the arch-specific include)
@ -804,11 +804,11 @@ pub fn clone2(flags: usize, child_stack_ptr: usize) usize {
}
pub fn close(fd: i32) usize {
return syscall1(SYS_close, usize(fd));
return syscall1(SYS_close, @intCast(usize, fd));
}
pub fn lseek(fd: i32, offset: isize, ref_pos: usize) usize {
return syscall3(SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos);
return syscall3(SYS_lseek, @intCast(usize, fd), @bitCast(usize, offset), ref_pos);
}
pub fn exit(status: i32) noreturn {
@ -817,11 +817,11 @@ pub fn exit(status: i32) noreturn {
}
pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
return syscall3(SYS_getrandom, @ptrToInt(buf), count, usize(flags));
return syscall3(SYS_getrandom, @ptrToInt(buf), count, @intCast(usize, flags));
}
pub fn kill(pid: i32, sig: i32) usize {
return syscall2(SYS_kill, @bitCast(usize, isize(pid)), usize(sig));
return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @intCast(usize, sig));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -999,8 +999,8 @@ pub const empty_sigset = []usize{0} ** sigset_t.len;
pub fn raise(sig: i32) usize {
var set: sigset_t = undefined;
blockAppSignals(&set);
const tid = i32(syscall0(SYS_gettid));
const ret = syscall2(SYS_tkill, usize(tid), usize(sig));
const tid = @intCast(i32, syscall0(SYS_gettid));
const ret = syscall2(SYS_tkill, @intCast(usize, tid), @intCast(usize, sig));
restoreSignals(&set);
return ret;
}
@ -1019,12 +1019,12 @@ fn restoreSignals(set: *sigset_t) void {
pub fn sigaddset(set: *sigset_t, sig: u6) void {
const s = sig - 1;
(set.*)[usize(s) / usize.bit_count] |= usize(1) << (s & (usize.bit_count - 1));
(set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
}
pub fn sigismember(set: *const sigset_t, sig: u6) bool {
const s = sig - 1;
return ((set.*)[usize(s) / usize.bit_count] & (usize(1) << (s & (usize.bit_count - 1)))) != 0;
return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
}
pub const in_port_t = u16;
@ -1057,11 +1057,11 @@ pub const iovec = extern struct {
};
pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len));
return syscall3(SYS_getsockname, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
}
pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
return syscall3(SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len));
return syscall3(SYS_getpeername, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len));
}
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 {
}
pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
return syscall5(SYS_setsockopt, usize(fd), level, optname, usize(optval), @ptrToInt(optlen));
return syscall5(SYS_setsockopt, @intCast(usize, fd), level, optname, @intCast(usize, optval), @ptrToInt(optlen));
}
pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
return syscall5(SYS_getsockopt, usize(fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
return syscall5(SYS_getsockopt, @intCast(usize, fd), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
}
pub fn sendmsg(fd: i32, msg: *const msghdr, flags: u32) usize {
return syscall3(SYS_sendmsg, usize(fd), @ptrToInt(msg), flags);
return syscall3(SYS_sendmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
}
pub fn connect(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_connect, usize(fd), @ptrToInt(addr), usize(len));
return syscall3(SYS_connect, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
}
pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
return syscall3(SYS_recvmsg, usize(fd), @ptrToInt(msg), flags);
return syscall3(SYS_recvmsg, @intCast(usize, fd), @ptrToInt(msg), flags);
}
pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
return syscall6(SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
return syscall6(SYS_recvfrom, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
}
pub fn shutdown(fd: i32, how: i32) usize {
return syscall2(SYS_shutdown, usize(fd), usize(how));
return syscall2(SYS_shutdown, @intCast(usize, fd), @intCast(usize, how));
}
pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
return syscall3(SYS_bind, usize(fd), @ptrToInt(addr), usize(len));
return syscall3(SYS_bind, @intCast(usize, fd), @ptrToInt(addr), @intCast(usize, len));
}
pub fn listen(fd: i32, backlog: u32) usize {
return syscall2(SYS_listen, usize(fd), backlog);
return syscall2(SYS_listen, @intCast(usize, fd), backlog);
}
pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
return syscall6(SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen));
return syscall6(SYS_sendto, @intCast(usize, fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
}
pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
return syscall4(SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(*fd[0]));
return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(*fd[0]));
}
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 {
}
pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
return syscall4(SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags);
return syscall4(SYS_accept4, @intCast(usize, fd), @ptrToInt(addr), @ptrToInt(len), flags);
}
pub fn fstat(fd: i32, stat_buf: *Stat) usize {
return syscall2(SYS_fstat, usize(fd), @ptrToInt(stat_buf));
return syscall2(SYS_fstat, @intCast(usize, fd), @ptrToInt(stat_buf));
}
// TODO https://github.com/ziglang/zig/issues/265
@ -1214,15 +1214,15 @@ pub fn epoll_create1(flags: usize) usize {
}
pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
return syscall4(SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev));
return syscall4(SYS_epoll_ctl, @intCast(usize, epoll_fd), @intCast(usize, op), @intCast(usize, fd), @ptrToInt(ev));
}
pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
return syscall4(SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout));
return syscall4(SYS_epoll_wait, @intCast(usize, epoll_fd), @ptrToInt(events), @intCast(usize, maxevents), @intCast(usize, timeout));
}
pub fn timerfd_create(clockid: i32, flags: u32) usize {
return syscall2(SYS_timerfd_create, usize(clockid), usize(flags));
return syscall2(SYS_timerfd_create, @intCast(usize, clockid), @intCast(usize, flags));
}
pub const itimerspec = extern struct {
@ -1231,11 +1231,11 @@ pub const itimerspec = extern struct {
};
pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
return syscall2(SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value));
return syscall2(SYS_timerfd_gettime, @intCast(usize, fd), @ptrToInt(curr_value));
}
pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
return syscall4(SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value));
return syscall4(SYS_timerfd_settime, @intCast(usize, fd), @intCast(usize, flags), @ptrToInt(new_value), @ptrToInt(old_value));
}
pub const _LINUX_CAPABILITY_VERSION_1 = 0x19980330;
@ -1345,7 +1345,7 @@ pub const cap_user_data_t = extern struct {
};
pub fn unshare(flags: usize) usize {
return syscall1(SYS_unshare, usize(flags));
return syscall1(SYS_unshare, @intCast(usize, flags));
}
pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {

View File

@ -21,7 +21,7 @@ test "timer" {
.it_value = time_interval,
};
err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
err = linux.timerfd_settime(@intCast(i32, timer_fd), 0, &new_time, null);
assert(err == 0);
var event = linux.epoll_event{
@ -29,12 +29,12 @@ test "timer" {
.data = linux.epoll_data{ .ptr = 0 },
};
err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);
err = linux.epoll_ctl(@intCast(i32, epoll_fd), linux.EPOLL_CTL_ADD, @intCast(i32, timer_fd), &event);
assert(err == 0);
const events_one: linux.epoll_event = undefined;
var events = []linux.epoll_event{events_one} ** 8;
// TODO implicit cast from *[N]T to [*]T
err = linux.epoll_wait(i32(epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
err = linux.epoll_wait(@intCast(i32, epoll_fd), @ptrCast([*]linux.epoll_event, &events), 8, -1);
}

View File

@ -62,8 +62,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
var i: usize = 0;
while (i < hashtab[1]) : (i += 1) {
if (0 == (u32(1) << u5(syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << u5(syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == (u32(1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == syms[i].st_shndx) continue;
if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue;
if (maybe_versym) |versym| {

View File

@ -14,12 +14,12 @@ pub const epoch = @import("epoch.zig");
pub fn sleep(seconds: usize, nanoseconds: usize) void {
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios => {
posixSleep(u63(seconds), u63(nanoseconds));
posixSleep(@intCast(u63, seconds), @intCast(u63, nanoseconds));
},
Os.windows => {
const ns_per_ms = ns_per_s / ms_per_s;
const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
windows.Sleep(windows.DWORD(milliseconds));
windows.Sleep(@intCast(windows.DWORD, milliseconds));
},
else => @compileError("Unsupported OS"),
}
@ -83,8 +83,8 @@ fn milliTimestampDarwin() u64 {
var tv: darwin.timeval = undefined;
var err = darwin.gettimeofday(&tv, null);
debug.assert(err == 0);
const sec_ms = u64(tv.tv_sec) * ms_per_s;
const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
const sec_ms = @intCast(u64, tv.tv_sec) * ms_per_s;
const usec_ms = @divFloor(@intCast(u64, tv.tv_usec), us_per_s / ms_per_s);
return u64(sec_ms) + u64(usec_ms);
}
@ -95,8 +95,8 @@ fn milliTimestampPosix() u64 {
var ts: posix.timespec = undefined;
const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
debug.assert(err == 0);
const sec_ms = u64(ts.tv_sec) * ms_per_s;
const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s);
const sec_ms = @intCast(u64, ts.tv_sec) * ms_per_s;
const nsec_ms = @divFloor(@intCast(u64, ts.tv_nsec), ns_per_s / ms_per_s);
return sec_ms + nsec_ms;
}
@ -162,13 +162,13 @@ pub const Timer = struct {
var freq: i64 = undefined;
var err = windows.QueryPerformanceFrequency(&freq);
if (err == windows.FALSE) return error.TimerUnsupported;
self.frequency = u64(freq);
self.frequency = @intCast(u64, freq);
self.resolution = @divFloor(ns_per_s, self.frequency);
var start_time: i64 = undefined;
err = windows.QueryPerformanceCounter(&start_time);
debug.assert(err != windows.FALSE);
self.start_time = u64(start_time);
self.start_time = @intCast(u64, start_time);
},
Os.linux => {
//On Linux, seccomp can do arbitrary things to our ability to call
@ -184,12 +184,12 @@ pub const Timer = struct {
posix.EINVAL => return error.TimerUnsupported,
else => return std.os.unexpectedErrorPosix(errno),
}
self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
self.resolution = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
result = posix.clock_gettime(monotonic_clock_id, &ts);
errno = posix.getErrno(result);
if (errno != 0) return std.os.unexpectedErrorPosix(errno);
self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
self.start_time = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
},
Os.macosx, Os.ios => {
darwin.mach_timebase_info(&self.frequency);
@ -236,7 +236,7 @@ pub const Timer = struct {
var result: i64 = undefined;
var err = windows.QueryPerformanceCounter(&result);
debug.assert(err != windows.FALSE);
return u64(result);
return @intCast(u64, result);
}
fn clockDarwin() u64 {
@ -247,7 +247,7 @@ pub const Timer = struct {
var ts: posix.timespec = undefined;
var result = posix.clock_gettime(monotonic_clock_id, &ts);
debug.assert(posix.getErrno(result) == 0);
return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
return @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec);
}
};

View File

@ -42,7 +42,7 @@ pub const WriteError = error{
};
pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) WriteError!void {
if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
if (windows.WriteFile(handle, @ptrCast(*const c_void, bytes.ptr), @intCast(u32, bytes.len), null, null) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.INVALID_USER_BUFFER => WriteError.SystemResources,
@ -68,7 +68,12 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
const size = @sizeOf(windows.FILE_NAME_INFO);
var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH);
if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(*c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
if (windows.GetFileInformationByHandleEx(
handle,
windows.FileNameInfo,
@ptrCast(*c_void, &name_info_bytes[0]),
@intCast(u32, name_info_bytes.len),
) == 0) {
return true;
}

View File

@ -55,16 +55,16 @@ pub const Random = struct {
if (T.is_signed) {
const uint = @IntType(false, T.bit_count);
if (start >= 0 and end >= 0) {
return T(r.range(uint, uint(start), uint(end)));
return @intCast(T, r.range(uint, @intCast(uint, start), @intCast(uint, end)));
} else if (start < 0 and end < 0) {
// Can't overflow because the range is over signed ints
return math.negateCast(r.range(uint, math.absCast(end), math.absCast(start)) + 1) catch unreachable;
} else if (start < 0 and end >= 0) {
const end_uint = uint(end);
const end_uint = @intCast(uint, end);
const total_range = math.absCast(start) + end_uint;
const value = r.range(uint, 0, total_range);
const result = if (value < end_uint) x: {
break :x T(value);
break :x @intCast(T, value);
} else if (value == end_uint) x: {
break :x start;
} else x: {
@ -213,9 +213,9 @@ pub const Pcg = struct {
self.s = l *% default_multiplier +% (self.i | 1);
const xor_s = @truncate(u32, ((l >> 18) ^ l) >> 27);
const rot = u32(l >> 59);
const rot = @intCast(u32, l >> 59);
return (xor_s >> u5(rot)) | (xor_s << u5((0 -% rot) & 31));
return (xor_s >> @intCast(u5, rot)) | (xor_s << @intCast(u5, (0 -% rot) & 31));
}
fn seed(self: *Pcg, init_s: u64) void {
@ -322,7 +322,7 @@ pub const Xoroshiro128 = struct {
inline for (table) |entry| {
var b: usize = 0;
while (b < 64) : (b += 1) {
if ((entry & (u64(1) << u6(b))) != 0) {
if ((entry & (u64(1) << @intCast(u6, b))) != 0) {
s0 ^= self.s[0];
s1 ^= self.s[1];
}
@ -667,13 +667,13 @@ test "Random range" {
}
fn testRange(r: *Random, start: i32, end: i32) void {
const count = usize(end - start);
const count = @intCast(usize, end - start);
var values_buffer = []bool{false} ** 20;
const values = values_buffer[0..count];
var i: usize = 0;
while (i < count) {
const value = r.range(i32, start, end);
const index = usize(value - start);
const index = @intCast(usize, value - start);
if (!values[index]) {
i += 1;
values[index] = true;

View File

@ -104,7 +104,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
pub fn deinit(self: *Self) void {
self.freeShelves(ShelfIndex(self.dynamic_segments.len), 0);
self.freeShelves(@intCast(ShelfIndex, self.dynamic_segments.len), 0);
self.allocator.free(self.dynamic_segments);
self.* = undefined;
}
@ -158,7 +158,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
/// Only grows capacity, or retains current capacity
pub fn growCapacity(self: *Self, new_capacity: usize) !void {
const new_cap_shelf_count = shelfCount(new_capacity);
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
if (new_cap_shelf_count > old_shelf_count) {
self.dynamic_segments = try self.allocator.realloc([*]T, self.dynamic_segments, new_cap_shelf_count);
var i = old_shelf_count;
@ -175,7 +175,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
/// Only shrinks capacity or retains current capacity
pub fn shrinkCapacity(self: *Self, new_capacity: usize) void {
if (new_capacity <= prealloc_item_count) {
const len = ShelfIndex(self.dynamic_segments.len);
const len = @intCast(ShelfIndex, self.dynamic_segments.len);
self.freeShelves(len, 0);
self.allocator.free(self.dynamic_segments);
self.dynamic_segments = [][*]T{};
@ -183,7 +183,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
}
const new_cap_shelf_count = shelfCount(new_capacity);
const old_shelf_count = ShelfIndex(self.dynamic_segments.len);
const old_shelf_count = @intCast(ShelfIndex, self.dynamic_segments.len);
assert(new_cap_shelf_count <= old_shelf_count);
if (new_cap_shelf_count == old_shelf_count) {
return;
@ -338,7 +338,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
{
var i: usize = 0;
while (i < 100) : (i += 1) {
try list.push(i32(i + 1));
try list.push(@intCast(i32, i + 1));
assert(list.len == i + 1);
}
}
@ -346,7 +346,7 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void {
{
var i: usize = 0;
while (i < 100) : (i += 1) {
assert(list.at(i).* == i32(i + 1));
assert(list.at(i).* == @intCast(i32, i + 1));
}
}

View File

@ -80,7 +80,7 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count];
return callMainWithArgs(usize(c_argc), c_argv, envp);
return callMainWithArgs(@intCast(usize, c_argc), c_argv, envp);
}
fn callMain() u8 {

View File

@ -135,9 +135,9 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
const mask = if (T == f32) 0xff else 0x7ff;
var ux = @bitCast(uint, x);
var uy = @bitCast(uint, y);
var ex = i32((ux >> digits) & mask);
var ey = i32((uy >> digits) & mask);
const sx = if (T == f32) u32(ux & 0x80000000) else i32(ux >> bits_minus_1);
var ex = @intCast(i32, (ux >> digits) & mask);
var ey = @intCast(i32, (uy >> digits) & mask);
const sx = if (T == f32) @intCast(u32, ux & 0x80000000) else @intCast(i32, ux >> bits_minus_1);
var i: uint = undefined;
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 {
ex -= 1;
i <<= 1;
}) {}
ux <<= log2uint(@bitCast(u32, -ex + 1));
ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1));
} else {
ux &= @maxValue(uint) >> exp_bits;
ux |= 1 << digits;
@ -167,7 +167,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
ey -= 1;
i <<= 1;
}) {}
uy <<= log2uint(@bitCast(u32, -ey + 1));
uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1));
} else {
uy &= @maxValue(uint) >> exp_bits;
uy |= 1 << digits;
@ -199,12 +199,12 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
ux -%= 1 << digits;
ux |= uint(@bitCast(u32, ex)) << digits;
} else {
ux >>= log2uint(@bitCast(u32, -ex + 1));
ux >>= @intCast(log2uint, @bitCast(u32, -ex + 1));
}
if (T == f32) {
ux |= sx;
} else {
ux |= uint(sx) << bits_minus_1;
ux |= @intCast(uint, sx) << bits_minus_1;
}
return @bitCast(T, ux);
}
@ -227,8 +227,8 @@ export fn sqrt(x: f64) f64 {
const sign: u32 = 0x80000000;
const u = @bitCast(u64, x);
var ix0 = u32(u >> 32);
var ix1 = u32(u & 0xFFFFFFFF);
var ix0 = @intCast(u32, u >> 32);
var ix1 = @intCast(u32, u & 0xFFFFFFFF);
// sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan
if (ix0 & 0x7FF00000 == 0x7FF00000) {
@ -245,7 +245,7 @@ export fn sqrt(x: f64) f64 {
}
// normalize x
var m = i32(ix0 >> 20);
var m = @intCast(i32, ix0 >> 20);
if (m == 0) {
// subnormal
while (ix0 == 0) {
@ -259,9 +259,9 @@ export fn sqrt(x: f64) f64 {
while (ix0 & 0x00100000 == 0) : (i += 1) {
ix0 <<= 1;
}
m -= i32(i) - 1;
ix0 |= ix1 >> u5(32 - i);
ix1 <<= u5(i);
m -= @intCast(i32, i) - 1;
ix0 |= ix1 >> @intCast(u5, 32 - i);
ix1 <<= @intCast(u5, i);
}
// unbias exponent
@ -345,10 +345,10 @@ export fn sqrt(x: f64) f64 {
// NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same
// behaviour at least.
var iix0 = i32(ix0);
var iix0 = @intCast(i32, ix0);
iix0 = iix0 +% (m << 20);
const uz = (u64(iix0) << 32) | ix1;
const uz = (@intCast(u64, iix0) << 32) | ix1;
return @bitCast(f64, uz);
}

View File

@ -13,7 +13,7 @@ pub extern fn __divti3(a: i128, b: i128) i128 {
const r = udivmod(u128, @bitCast(u128, an), @bitCast(u128, bn), null);
const s = s_a ^ s_b;
return (i128(r) ^ s) -% s;
return (@bitCast(i128, r) ^ s) -% s;
}
pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {

View File

@ -32,14 +32,14 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
const aAbs: rep_t = aRep & absMask;
const sign = if ((aRep & signBit) != 0) i32(-1) else i32(1);
const exponent = i32(aAbs >> significandBits) - exponentBias;
const exponent = @intCast(i32, aAbs >> significandBits) - exponentBias;
const significand: rep_t = (aAbs & significandMask) | implicitBit;
// If either the value or the exponent is negative, the result is zero.
if (sign == -1 or exponent < 0) return 0;
// If the value is too large for the integer type, saturate.
if (c_uint(exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
if (@intCast(c_uint, exponent) >= fixuint_t.bit_count) return ~fixuint_t(0);
// If 0 <= exponent < significandBits, right shift to get the result.
// Otherwise, shift left.
@ -47,11 +47,11 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
// TODO this is a workaround for the mysterious "integer cast truncated bits"
// happening on the next line
@setRuntimeSafety(false);
return fixuint_t(significand >> Log2Int(rep_t)(significandBits - exponent));
return @intCast(fixuint_t, significand >> @intCast(Log2Int(rep_t), significandBits - exponent));
} else {
// TODO this is a workaround for the mysterious "integer cast truncated bits"
// happening on the next line
@setRuntimeSafety(false);
return fixuint_t(significand) << Log2Int(fixuint_t)(exponent - significandBits);
return @intCast(fixuint_t, significand) << @intCast(Log2Int(fixuint_t), exponent - significandBits);
}
}

View File

@ -292,7 +292,7 @@ extern fn __udivmodsi4(a: u32, b: u32, rem: *u32) u32 {
@setRuntimeSafety(is_test);
const d = __udivsi3(a, b);
rem.* = u32(i32(a) -% (i32(d) * i32(b)));
rem.* = @bitCast(u32, @bitCast(i32, a) -% (@bitCast(i32, d) * @bitCast(i32, b)));
return d;
}
@ -316,12 +316,12 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
sr += 1;
// 1 <= sr <= n_uword_bits - 1
// Not a special case
var q: u32 = n << u5(n_uword_bits - sr);
var r: u32 = n >> u5(sr);
var q: u32 = n << @intCast(u5, n_uword_bits - sr);
var r: u32 = n >> @intCast(u5, sr);
var carry: u32 = 0;
while (sr > 0) : (sr -= 1) {
// r:q = ((r:q) << 1) | carry
r = (r << 1) | (q >> u5(n_uword_bits - 1));
r = (r << 1) | (q >> @intCast(u5, n_uword_bits - 1));
q = (q << 1) | carry;
// carry = 0;
// if (r.all >= d.all)
@ -329,8 +329,8 @@ extern fn __udivsi3(n: u32, d: u32) u32 {
// r.all -= d.all;
// carry = 1;
// }
const s = i32(d -% r -% 1) >> u5(n_uword_bits - 1);
carry = u32(s & 1);
const s = @intCast(i32, d -% r -% 1) >> @intCast(u5, n_uword_bits - 1);
carry = @intCast(u32, s & 1);
r -= d & @bitCast(u32, s);
}
q = (q << 1) | carry;

View File

@ -71,7 +71,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
r[high] = n[high] & (d[high] - 1);
rem.* = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
}
return n[high] >> Log2SingleInt(@ctz(d[high]));
return n[high] >> @intCast(Log2SingleInt, @ctz(d[high]));
}
// K K
// ---
@ -88,10 +88,10 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// 1 <= sr <= SingleInt.bit_count - 1
// q.all = a << (DoubleInt.bit_count - sr);
q[low] = 0;
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
// r.all = a >> sr;
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
} else {
// d[low] != 0
if (d[high] == 0) {
@ -107,8 +107,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
return a;
}
sr = @ctz(d[low]);
q[high] = n[high] >> Log2SingleInt(sr);
q[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[high] >> @intCast(Log2SingleInt, sr);
q[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
}
// K X
@ -126,15 +126,15 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
} else if (sr < SingleInt.bit_count) {
// 2 <= sr <= SingleInt.bit_count - 1
q[low] = 0;
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
} else {
// SingleInt.bit_count + 1 <= sr <= DoubleInt.bit_count - 1
q[low] = n[low] << Log2SingleInt(DoubleInt.bit_count - sr);
q[high] = (n[high] << Log2SingleInt(DoubleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr - SingleInt.bit_count));
q[low] = n[low] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr);
q[high] = (n[high] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count));
r[high] = 0;
r[low] = n[high] >> Log2SingleInt(sr - SingleInt.bit_count);
r[low] = n[high] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count);
}
} else {
// K X
@ -158,9 +158,9 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
r[high] = 0;
r[low] = n[high];
} else {
r[high] = n[high] >> Log2SingleInt(sr);
r[low] = (n[high] << Log2SingleInt(SingleInt.bit_count - sr)) | (n[low] >> Log2SingleInt(sr));
q[high] = n[low] << Log2SingleInt(SingleInt.bit_count - sr);
r[high] = n[high] >> @intCast(Log2SingleInt, sr);
r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
}
}
}
@ -184,8 +184,8 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
// carry = 1;
// }
r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
const s: SignedDoubleInt = SignedDoubleInt(b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
carry = u32(s & 1);
const s: SignedDoubleInt = @intCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
carry = @intCast(u32, s & 1);
r_all -= b & @bitCast(DoubleInt, s);
r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
}

View File

@ -35,22 +35,22 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
// - Increasing the initial shift by 6 each time
// - Each time after the first shorten the shifted
// value to a max of 0b111111 (63)
1 => out[0] = u8(c), // Can just do 0 + codepoint for initial range
1 => out[0] = @intCast(u8, c), // Can just do 0 + codepoint for initial range
2 => {
out[0] = u8(0b11000000 | (c >> 6));
out[1] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11000000 | (c >> 6));
out[1] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
3 => {
if (0xd800 <= c and c <= 0xdfff) return error.Utf8CannotEncodeSurrogateHalf;
out[0] = u8(0b11100000 | (c >> 12));
out[1] = u8(0b10000000 | ((c >> 6) & 0b111111));
out[2] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11100000 | (c >> 12));
out[1] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
out[2] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
4 => {
out[0] = u8(0b11110000 | (c >> 18));
out[1] = u8(0b10000000 | ((c >> 12) & 0b111111));
out[2] = u8(0b10000000 | ((c >> 6) & 0b111111));
out[3] = u8(0b10000000 | (c & 0b111111));
out[0] = @intCast(u8, 0b11110000 | (c >> 18));
out[1] = @intCast(u8, 0b10000000 | ((c >> 12) & 0b111111));
out[2] = @intCast(u8, 0b10000000 | ((c >> 6) & 0b111111));
out[3] = @intCast(u8, 0b10000000 | (c & 0b111111));
},
else => unreachable,
}

View File

@ -1128,7 +1128,7 @@ pub const Tokenizer = struct {
// check utf8-encoded character.
const length = std.unicode.utf8ByteSequenceLength(c0) catch return 1;
if (self.index + length > self.buffer.len) {
return u3(self.buffer.len - self.index);
return @intCast(u3, self.buffer.len - self.index);
}
const bytes = self.buffer[self.index .. self.index + length];
switch (length) {

View File

@ -343,7 +343,7 @@ fn testPeerErrorAndArray2(x: u8) error![]const u8 {
test "explicit cast float number literal to integer if no fraction component" {
const x = i32(1e4);
assert(x == 10000);
const y = i32(f32(1e4));
const y = @floatToInt(i32, f32(1e4));
assert(y == 10000);
}
@ -398,3 +398,19 @@ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
const x: [*]const ?[*]const u8 = &window_name;
assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name"));
}
test "@intCast comptime_int" {
const result = @intCast(i32, 1234);
assert(@typeOf(result) == i32);
assert(result == 1234);
}
test "@floatCast comptime_int and comptime_float" {
const result = @floatCast(f32, 1234);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
const result2 = @floatCast(f32, 1234.0);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
}

View File

@ -99,7 +99,7 @@ test "int to enum" {
testIntToEnumEval(3);
}
fn testIntToEnumEval(x: i32) void {
assert(IntToEnumNumber(u3(x)) == IntToEnumNumber.Three);
assert(IntToEnumNumber(@intCast(u3, x)) == IntToEnumNumber.Three);
}
const IntToEnumNumber = enum {
Zero,

View File

@ -5,7 +5,7 @@ const builtin = @import("builtin");
test "compile time recursion" {
assert(some_data.len == 21);
}
var some_data: [usize(fibonacci(7))]u8 = undefined;
var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined;
fn fibonacci(x: i32) i32 {
if (x <= 1) return 1;
return fibonacci(x - 1) + fibonacci(x - 2);
@ -356,7 +356,7 @@ const global_array = x: {
test "compile-time downcast when the bits fit" {
comptime {
const spartan_count: u16 = 255;
const byte = u8(spartan_count);
const byte = @intCast(u8, spartan_count);
assert(byte == 255);
}
}
@ -440,7 +440,7 @@ test "binary math operator in partially inlined function" {
var b: [16]u8 = undefined;
for (b) |*r, i|
r.* = u8(i + 1);
r.* = @intCast(u8, i + 1);
copyWithPartialInline(s[0..], b[0..]);
assert(s[0] == 0x1020304);
@ -480,7 +480,7 @@ fn generateTable(comptime T: type) [1010]T {
var res: [1010]T = undefined;
var i: usize = 0;
while (i < 1010) : (i += 1) {
res[i] = T(i);
res[i] = @intCast(T, i);
}
return res;
}

View File

@ -80,7 +80,7 @@ test "function pointers" {
fn4,
};
for (fns) |f, i| {
assert(f() == u32(i) + 5);
assert(f() == @intCast(u32, i) + 5);
}
}
fn fn1() u32 {

View File

@ -46,7 +46,7 @@ test "basic for loop" {
buf_index += 1;
}
for (array) |item, index| {
buffer[buf_index] = u8(index);
buffer[buf_index] = @intCast(u8, index);
buf_index += 1;
}
const unknown_size: []const u8 = array;
@ -55,7 +55,7 @@ test "basic for loop" {
buf_index += 1;
}
for (unknown_size) |item, index| {
buffer[buf_index] = u8(index);
buffer[buf_index] = @intCast(u8, index);
buf_index += 1;
}

View File

@ -365,14 +365,14 @@ test "runtime struct initialization of bitfield" {
.y = x1,
};
const s2 = Nibbles{
.x = u4(x2),
.y = u4(x2),
.x = @intCast(u4, x2),
.y = @intCast(u4, x2),
};
assert(s1.x == x1);
assert(s1.y == x1);
assert(s2.x == u4(x2));
assert(s2.y == u4(x2));
assert(s2.x == @intCast(u4, x2));
assert(s2.y == @intCast(u4, x2));
}
var x1 = u4(1);

View File

@ -299,7 +299,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\export fn main() c_int {
\\ var array = []u32{ 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
\\
\\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), c_ulong(array.len), @sizeOf(i32), compare_fn);
\\ c.qsort(@ptrCast(?*c_void, array[0..].ptr), @intCast(c_ulong, array.len), @sizeOf(i32), compare_fn);
\\
\\ for (array) |item, i| {
\\ if (item != i) {
@ -331,8 +331,8 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ }
\\ const small: f32 = 3.25;
\\ const x: f64 = small;
\\ const y = i32(x);
\\ const z = f64(y);
\\ const y = @floatToInt(i32, x);
\\ const z = @intToFloat(f64, y);
\\ _ = c.printf(c"%.2f\n%d\n%.2f\n%.2f\n", x, y, z, f64(-0.4));
\\ return 0;
\\}

View File

@ -2931,10 +2931,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"cast negative value to unsigned integer",
\\comptime {
\\ const value: i32 = -1;
\\ const unsigned = u32(value);
\\ const unsigned = @intCast(u32, value);
\\}
,
".tmp_source.zig:3:25: error: attempt to cast negative value to unsigned integer",
".tmp_source.zig:3:22: error: attempt to cast negative value to unsigned integer",
);
cases.add(
@ -2963,10 +2963,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"compile-time integer cast truncates bits",
\\comptime {
\\ const spartan_count: u16 = 300;
\\ const byte = u8(spartan_count);
\\ const byte = @intCast(u8, spartan_count);
\\}
,
".tmp_source.zig:3:20: error: cast from 'u16' to 'u8' truncates bits",
".tmp_source.zig:3:18: error: cast from 'u16' to 'u8' truncates bits",
);
cases.add(

View File

@ -188,7 +188,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ if (x == 0) return error.Whatever;
\\}
\\fn shorten_cast(x: i32) i8 {
\\ return i8(x);
\\ return @intCast(i8, x);
\\}
);
@ -201,7 +201,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ if (x == 0) return error.Whatever;
\\}
\\fn unsigned_cast(x: i32) u32 {
\\ return u32(x);
\\ return @intCast(u32, x);
\\}
);