authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-10-26 14:59:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-10-26 15:01:51-04:00
log2b395d4ede2d8ef356c54e1c7c09da88c634be11
treeddb9806afc26bc55554e20066b29d076bec71f79
parent40b7652a6da135aed68a0067f2de60b8b276713c
signature Commit is signed but in an unrecognized format.

remove @minValue,@maxValue; add std.math.minInt,maxInt

closes #1466 closes #1476

55 files changed, 310 insertions(+), 398 deletions(-)

doc/langref.html.in+18-30
...@@ -827,7 +827,7 @@ a +%= b{#endsyntax#}</pre></td>...@@ -827,7 +827,7 @@ a +%= b{#endsyntax#}</pre></td>
827 </ul>827 </ul>
828 </td>828 </td>
829 <td>829 <td>
830 <pre>{#syntax#}u32(@maxValue(u32)) +% 1 == 0{#endsyntax#}</pre>830 <pre>{#syntax#}u32(std.math.maxInt(u32)) +% 1 == 0{#endsyntax#}</pre>
831 </td>831 </td>
832 </tr>832 </tr>
833 <tr>833 <tr>
...@@ -866,7 +866,7 @@ a -%= b{#endsyntax#}</pre></td>...@@ -866,7 +866,7 @@ a -%= b{#endsyntax#}</pre></td>
866 </ul>866 </ul>
867 </td>867 </td>
868 <td>868 <td>
869 <pre>{#syntax#}u32(0) -% 1 == @maxValue(u32){#endsyntax#}</pre>869 <pre>{#syntax#}u32(0) -% 1 == std.math.maxInt(u32){#endsyntax#}</pre>
870 </td>870 </td>
871 </tr>871 </tr>
872 <tr>872 <tr>
...@@ -901,7 +901,7 @@ a -%= b{#endsyntax#}</pre></td>...@@ -901,7 +901,7 @@ a -%= b{#endsyntax#}</pre></td>
901 </ul>901 </ul>
902 </td>902 </td>
903 <td>903 <td>
904 <pre>{#syntax#}-%i32(@minValue(i32)) == @minValue(i32){#endsyntax#}</pre>904 <pre>{#syntax#}-%i32(std.math.minInt(i32)) == std.math.minInt(i32){#endsyntax#}</pre>
905 </td>905 </td>
906 </tr>906 </tr>
907 <tr>907 <tr>
...@@ -3298,6 +3298,9 @@ const err = (error.{FileNotFound}).FileNotFound;...@@ -3298,6 +3298,9 @@ const err = (error.{FileNotFound}).FileNotFound;
3298 Here is a function to parse a string into a 64-bit integer:3298 Here is a function to parse a string into a 64-bit integer:
3299 </p>3299 </p>
3300 {#code_begin|test#}3300 {#code_begin|test#}
3301const std = @import("std");
3302const maxInt = std.math.maxInt;
3303
3301pub fn parseU64(buf: []const u8, radix: u8) !u64 {3304pub fn parseU64(buf: []const u8, radix: u8) !u64 {
3302 var x: u64 = 0;3305 var x: u64 = 0;
33033306
...@@ -3327,13 +3330,13 @@ fn charToDigit(c: u8) u8 {...@@ -3327,13 +3330,13 @@ fn charToDigit(c: u8) u8 {
3327 '0' ... '9' => c - '0',3330 '0' ... '9' => c - '0',
3328 'A' ... 'Z' => c - 'A' + 10,3331 'A' ... 'Z' => c - 'A' + 10,
3329 'a' ... 'z' => c - 'a' + 10,3332 'a' ... 'z' => c - 'a' + 10,
3330 else => @maxValue(u8),3333 else => maxInt(u8),
3331 };3334 };
3332}3335}
33333336
3334test "parse u64" {3337test "parse u64" {
3335 const result = try parseU64("1234", 10);3338 const result = try parseU64("1234", 10);
3336 @import("std").debug.assert(result == 1234);3339 std.debug.assert(result == 1234);
3337}3340}
3338 {#code_end#}3341 {#code_end#}
3339 <p>3342 <p>
...@@ -5539,7 +5542,7 @@ test "main" {...@@ -5539,7 +5542,7 @@ test "main" {
5539 <p>5542 <p>
5540 Floored division. Rounds toward negative infinity. For unsigned integers it is5543 Floored division. Rounds toward negative infinity. For unsigned integers it is
5541 the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and5544 the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
5542 {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}.5545 {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
5543 </p>5546 </p>
5544 <ul>5547 <ul>
5545 <li>{#syntax#}@divFloor(-5, 3) == -2{#endsyntax#}</li>5548 <li>{#syntax#}@divFloor(-5, 3) == -2{#endsyntax#}</li>
...@@ -5553,7 +5556,7 @@ test "main" {...@@ -5553,7 +5556,7 @@ test "main" {
5553 <p>5556 <p>
5554 Truncated division. Rounds toward zero. For unsigned integers it is5557 Truncated division. Rounds toward zero. For unsigned integers it is
5555 the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and5558 the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and
5556 {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1){#endsyntax#}.5559 {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.
5557 </p>5560 </p>
5558 <ul>5561 <ul>
5559 <li>{#syntax#}@divTrunc(-5, 3) == -1{#endsyntax#}</li>5562 <li>{#syntax#}@divTrunc(-5, 3) == -1{#endsyntax#}</li>
...@@ -5816,15 +5819,6 @@ fn add(a: i32, b: i32) i32 { return a + b; }...@@ -5816,15 +5819,6 @@ fn add(a: i32, b: i32) i32 { return a + b; }
5816 This function returns an integer type with the given signness and bit count.5819 This function returns an integer type with the given signness and bit count.
5817 </p>5820 </p>
5818 {#header_close#}5821 {#header_close#}
5819 {#header_open|@maxValue#}
5820 <pre>{#syntax#}@maxValue(comptime T: type) comptime_int{#endsyntax#}</pre>
5821 <p>
5822 This function returns the maximum value of the integer type {#syntax#}T{#endsyntax#}.
5823 </p>
5824 <p>
5825 The result is a compile time constant.
5826 </p>
5827 {#header_close#}
5828 {#header_open|@memberCount#}5822 {#header_open|@memberCount#}
5829 <pre>{#syntax#}@memberCount(comptime T: type) comptime_int{#endsyntax#}</pre>5823 <pre>{#syntax#}@memberCount(comptime T: type) comptime_int{#endsyntax#}</pre>
5830 <p>5824 <p>
...@@ -5885,15 +5879,6 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);{#endsyntax#}</pre>...@@ -5885,15 +5879,6 @@ mem.copy(u8, dest[0...byte_count], source[0...byte_count]);{#endsyntax#}</pre>
5885 <p>There is also a standard library function for this:</p>5879 <p>There is also a standard library function for this:</p>
5886 <pre>{#syntax#}const mem = @import("std").mem;5880 <pre>{#syntax#}const mem = @import("std").mem;
5887mem.set(u8, dest, c);{#endsyntax#}</pre>5881mem.set(u8, dest, c);{#endsyntax#}</pre>
5888 {#header_close#}
5889 {#header_open|@minValue#}
5890 <pre>{#syntax#}@minValue(comptime T: type) comptime_int{#endsyntax#}</pre>
5891 <p>
5892 This function returns the minimum value of the integer type T.
5893 </p>
5894 <p>
5895 The result is a compile time constant.
5896 </p>
5897 {#header_close#}5882 {#header_close#}
5898 {#header_open|@mod#}5883 {#header_open|@mod#}
5899 <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre>5884 <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre>
...@@ -6688,7 +6673,7 @@ pub fn main() void {...@@ -6688,7 +6673,7 @@ pub fn main() void {
6688}6673}
6689 {#code_end#}6674 {#code_end#}
6690 <p>6675 <p>
6691 To obtain the maximum value of an unsigned integer, use {#link|@maxValue#}.6676 To obtain the maximum value of an unsigned integer, use {#syntax#}std.math.maxInt{#endsyntax#}.
6692 </p>6677 </p>
6693 {#header_close#}6678 {#header_close#}
6694 {#header_open|Cast Truncates Data#}6679 {#header_open|Cast Truncates Data#}
...@@ -6810,14 +6795,17 @@ pub fn main() void {...@@ -6810,14 +6795,17 @@ pub fn main() void {
6810 <li>{#syntax#}*%{#endsyntax#} (wraparound multiplication)</li>6795 <li>{#syntax#}*%{#endsyntax#} (wraparound multiplication)</li>
6811 </ul>6796 </ul>
6812 {#code_begin|test#}6797 {#code_begin|test#}
6813const assert = @import("std").debug.assert;6798const std = @import("std");
6799const assert = std.debug.assert;
6800const minInt = std.math.minInt;
6801const maxInt = std.math.maxInt;
68146802
6815test "wraparound addition and subtraction" {6803test "wraparound addition and subtraction" {
6816 const x: i32 = @maxValue(i32);6804 const x: i32 = maxInt(i32);
6817 const min_val = x +% 1;6805 const min_val = x +% 1;
6818 assert(min_val == @minValue(i32));6806 assert(min_val == minInt(i32));
6819 const max_val = min_val -% 1;6807 const max_val = min_val -% 1;
6820 assert(max_val == @maxValue(i32));6808 assert(max_val == maxInt(i32));
6821}6809}
6822 {#code_end#}6810 {#code_end#}
6823 {#header_close#}6811 {#header_close#}
src-self-hosted/codegen.zig+4-3
...@@ -10,6 +10,7 @@ const Scope = @import("scope.zig").Scope;...@@ -10,6 +10,7 @@ const Scope = @import("scope.zig").Scope;
10const event = std.event;10const event = std.event;
11const assert = std.debug.assert;11const assert = std.debug.assert;
12const DW = std.dwarf;12const DW = std.dwarf;
13const maxInt = std.math.maxInt;
1314
14pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {15pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {
15 fn_val.base.ref();16 fn_val.base.ref();
...@@ -362,15 +363,15 @@ fn addLLVMAttrInt(...@@ -362,15 +363,15 @@ fn addLLVMAttrInt(
362}363}
363364
364fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8) !void {365fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8) !void {
365 return addLLVMAttr(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name);366 return addLLVMAttr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name);
366}367}
367368
368fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: []const u8) !void {369fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: []const u8) !void {
369 return addLLVMAttrStr(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name, attr_val);370 return addLLVMAttrStr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
370}371}
371372
372fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: u64) !void {373fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: u64) !void {
373 return addLLVMAttrInt(ofile, fn_val, @maxValue(llvm.AttributeIndex), attr_name, attr_val);374 return addLLVMAttrInt(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
374}375}
375376
376fn renderLoadUntyped(377fn renderLoadUntyped(
src/all_types.hpp-16
...@@ -1343,8 +1343,6 @@ enum BuiltinFnId {...@@ -1343,8 +1343,6 @@ enum BuiltinFnId {
1343 BuiltinFnIdMemset,1343 BuiltinFnIdMemset,
1344 BuiltinFnIdSizeof,1344 BuiltinFnIdSizeof,
1345 BuiltinFnIdAlignOf,1345 BuiltinFnIdAlignOf,
1346 BuiltinFnIdMaxValue,
1347 BuiltinFnIdMinValue,
1348 BuiltinFnIdMemberCount,1346 BuiltinFnIdMemberCount,
1349 BuiltinFnIdMemberType,1347 BuiltinFnIdMemberType,
1350 BuiltinFnIdMemberName,1348 BuiltinFnIdMemberName,
...@@ -2081,8 +2079,6 @@ enum IrInstructionId {...@@ -2081,8 +2079,6 @@ enum IrInstructionId {
2081 IrInstructionIdCUndef,2079 IrInstructionIdCUndef,
2082 IrInstructionIdArrayLen,2080 IrInstructionIdArrayLen,
2083 IrInstructionIdRef,2081 IrInstructionIdRef,
2084 IrInstructionIdMinValue,
2085 IrInstructionIdMaxValue,
2086 IrInstructionIdCompileErr,2082 IrInstructionIdCompileErr,
2087 IrInstructionIdCompileLog,2083 IrInstructionIdCompileLog,
2088 IrInstructionIdErrName,2084 IrInstructionIdErrName,
...@@ -2609,18 +2605,6 @@ struct IrInstructionRef {...@@ -2609,18 +2605,6 @@ struct IrInstructionRef {
2609 bool is_volatile;2605 bool is_volatile;
2610};2606};
26112607
2612struct IrInstructionMinValue {
2613 IrInstruction base;
2614
2615 IrInstruction *value;
2616};
2617
2618struct IrInstructionMaxValue {
2619 IrInstruction base;
2620
2621 IrInstruction *value;
2622};
2623
2624struct IrInstructionCompileErr {2608struct IrInstructionCompileErr {
2625 IrInstruction base;2609 IrInstruction base;
26262610
src/codegen.cpp-4
...@@ -5100,8 +5100,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -5100,8 +5100,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
5100 case IrInstructionIdSizeOf:5100 case IrInstructionIdSizeOf:
5101 case IrInstructionIdSwitchTarget:5101 case IrInstructionIdSwitchTarget:
5102 case IrInstructionIdContainerInitFields:5102 case IrInstructionIdContainerInitFields:
5103 case IrInstructionIdMinValue:
5104 case IrInstructionIdMaxValue:
5105 case IrInstructionIdCompileErr:5103 case IrInstructionIdCompileErr:
5106 case IrInstructionIdCompileLog:5104 case IrInstructionIdCompileLog:
5107 case IrInstructionIdArrayLen:5105 case IrInstructionIdArrayLen:
...@@ -6651,8 +6649,6 @@ static void define_builtin_fns(CodeGen *g) {...@@ -6651,8 +6649,6 @@ static void define_builtin_fns(CodeGen *g) {
6651 create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3);6649 create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3);
6652 create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1);6650 create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1);
6653 create_builtin_fn(g, BuiltinFnIdAlignOf, "alignOf", 1);6651 create_builtin_fn(g, BuiltinFnIdAlignOf, "alignOf", 1);
6654 create_builtin_fn(g, BuiltinFnIdMaxValue, "maxValue", 1);
6655 create_builtin_fn(g, BuiltinFnIdMinValue, "minValue", 1);
6656 create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1);6652 create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1);
6657 create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);6653 create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
6658 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);6654 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
src/ir.cpp-120
...@@ -495,14 +495,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionInit *) {...@@ -495,14 +495,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionUnionInit *) {
495 return IrInstructionIdUnionInit;495 return IrInstructionIdUnionInit;
496}496}
497497
498static constexpr IrInstructionId ir_instruction_id(IrInstructionMinValue *) {
499 return IrInstructionIdMinValue;
500}
501
502static constexpr IrInstructionId ir_instruction_id(IrInstructionMaxValue *) {
503 return IrInstructionIdMaxValue;
504}
505
506static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {498static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileErr *) {
507 return IrInstructionIdCompileErr;499 return IrInstructionIdCompileErr;
508}500}
...@@ -1693,24 +1685,6 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source...@@ -1693,24 +1685,6 @@ static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source
1693 return &instruction->base;1685 return &instruction->base;
1694}1686}
16951687
1696static IrInstruction *ir_build_min_value(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1697 IrInstructionMinValue *instruction = ir_build_instruction<IrInstructionMinValue>(irb, scope, source_node);
1698 instruction->value = value;
1699
1700 ir_ref_instruction(value, irb->current_basic_block);
1701
1702 return &instruction->base;
1703}
1704
1705static IrInstruction *ir_build_max_value(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1706 IrInstructionMaxValue *instruction = ir_build_instruction<IrInstructionMaxValue>(irb, scope, source_node);
1707 instruction->value = value;
1708
1709 ir_ref_instruction(value, irb->current_basic_block);
1710
1711 return &instruction->base;
1712}
1713
1714static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *msg) {1688static IrInstruction *ir_build_compile_err(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *msg) {
1715 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);1689 IrInstructionCompileErr *instruction = ir_build_instruction<IrInstructionCompileErr>(irb, scope, source_node);
1716 instruction->msg = msg;1690 instruction->msg = msg;
...@@ -3813,26 +3787,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -3813,26 +3787,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
3813 IrInstruction *c_undef = ir_build_c_undef(irb, scope, node, arg0_value);3787 IrInstruction *c_undef = ir_build_c_undef(irb, scope, node, arg0_value);
3814 return ir_lval_wrap(irb, scope, c_undef, lval);3788 return ir_lval_wrap(irb, scope, c_undef, lval);
3815 }3789 }
3816 case BuiltinFnIdMaxValue:
3817 {
3818 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
3819 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
3820 if (arg0_value == irb->codegen->invalid_instruction)
3821 return arg0_value;
3822
3823 IrInstruction *max_value = ir_build_max_value(irb, scope, node, arg0_value);
3824 return ir_lval_wrap(irb, scope, max_value, lval);
3825 }
3826 case BuiltinFnIdMinValue:
3827 {
3828 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
3829 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
3830 if (arg0_value == irb->codegen->invalid_instruction)
3831 return arg0_value;
3832
3833 IrInstruction *min_value = ir_build_min_value(irb, scope, node, arg0_value);
3834 return ir_lval_wrap(irb, scope, min_value, lval);
3835 }
3836 case BuiltinFnIdCompileErr:3790 case BuiltinFnIdCompileErr:
3837 {3791 {
3838 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);3792 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
...@@ -16407,74 +16361,6 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir...@@ -16407,74 +16361,6 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir
16407 instruction->field_count, instruction->fields);16361 instruction->field_count, instruction->fields);
16408}16362}
1640916363
16410static IrInstruction *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_instruction,
16411 IrInstruction *target_type_value, bool is_max)
16412{
16413 ZigType *target_type = ir_resolve_type(ira, target_type_value);
16414 if (type_is_invalid(target_type))
16415 return ira->codegen->invalid_instruction;
16416 switch (target_type->id) {
16417 case ZigTypeIdInvalid:
16418 zig_unreachable();
16419 case ZigTypeIdInt:
16420 {
16421 IrInstruction *result = ir_const(ira, source_instruction,
16422 ira->codegen->builtin_types.entry_num_lit_int);
16423 eval_min_max_value(ira->codegen, target_type, &result->value, is_max);
16424 return result;
16425 }
16426 case ZigTypeIdBool:
16427 case ZigTypeIdVoid:
16428 {
16429 IrInstruction *result = ir_const(ira, source_instruction, target_type);
16430 eval_min_max_value(ira->codegen, target_type, &result->value, is_max);
16431 return result;
16432 }
16433 case ZigTypeIdEnum:
16434 case ZigTypeIdFloat:
16435 case ZigTypeIdMetaType:
16436 case ZigTypeIdUnreachable:
16437 case ZigTypeIdPointer:
16438 case ZigTypeIdPromise:
16439 case ZigTypeIdArray:
16440 case ZigTypeIdStruct:
16441 case ZigTypeIdComptimeFloat:
16442 case ZigTypeIdComptimeInt:
16443 case ZigTypeIdUndefined:
16444 case ZigTypeIdNull:
16445 case ZigTypeIdOptional:
16446 case ZigTypeIdErrorUnion:
16447 case ZigTypeIdErrorSet:
16448 case ZigTypeIdUnion:
16449 case ZigTypeIdFn:
16450 case ZigTypeIdNamespace:
16451 case ZigTypeIdBoundFn:
16452 case ZigTypeIdArgTuple:
16453 case ZigTypeIdOpaque:
16454 {
16455 const char *err_format = is_max ?
16456 "no max value available for type '%s'" :
16457 "no min value available for type '%s'";
16458 ir_add_error(ira, source_instruction,
16459 buf_sprintf(err_format, buf_ptr(&target_type->name)));
16460 return ira->codegen->invalid_instruction;
16461 }
16462 }
16463 zig_unreachable();
16464}
16465
16466static IrInstruction *ir_analyze_instruction_min_value(IrAnalyze *ira,
16467 IrInstructionMinValue *instruction)
16468{
16469 return ir_analyze_min_max(ira, &instruction->base, instruction->value->child, false);
16470}
16471
16472static IrInstruction *ir_analyze_instruction_max_value(IrAnalyze *ira,
16473 IrInstructionMaxValue *instruction)
16474{
16475 return ir_analyze_min_max(ira, &instruction->base, instruction->value->child, true);
16476}
16477
16478static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,16364static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
16479 IrInstructionCompileErr *instruction)16365 IrInstructionCompileErr *instruction)
16480{16366{
...@@ -21052,10 +20938,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio...@@ -21052,10 +20938,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
21052 return ir_analyze_instruction_container_init_list(ira, (IrInstructionContainerInitList *)instruction);20938 return ir_analyze_instruction_container_init_list(ira, (IrInstructionContainerInitList *)instruction);
21053 case IrInstructionIdContainerInitFields:20939 case IrInstructionIdContainerInitFields:
21054 return ir_analyze_instruction_container_init_fields(ira, (IrInstructionContainerInitFields *)instruction);20940 return ir_analyze_instruction_container_init_fields(ira, (IrInstructionContainerInitFields *)instruction);
21055 case IrInstructionIdMinValue:
21056 return ir_analyze_instruction_min_value(ira, (IrInstructionMinValue *)instruction);
21057 case IrInstructionIdMaxValue:
21058 return ir_analyze_instruction_max_value(ira, (IrInstructionMaxValue *)instruction);
21059 case IrInstructionIdCompileErr:20941 case IrInstructionIdCompileErr:
21060 return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);20942 return ir_analyze_instruction_compile_err(ira, (IrInstructionCompileErr *)instruction);
21061 case IrInstructionIdCompileLog:20943 case IrInstructionIdCompileLog:
...@@ -21399,8 +21281,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -21399,8 +21281,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
21399 case IrInstructionIdSwitchTarget:21281 case IrInstructionIdSwitchTarget:
21400 case IrInstructionIdUnionTag:21282 case IrInstructionIdUnionTag:
21401 case IrInstructionIdRef:21283 case IrInstructionIdRef:
21402 case IrInstructionIdMinValue:
21403 case IrInstructionIdMaxValue:
21404 case IrInstructionIdEmbedFile:21284 case IrInstructionIdEmbedFile:
21405 case IrInstructionIdTruncate:21285 case IrInstructionIdTruncate:
21406 case IrInstructionIdIntType:21286 case IrInstructionIdIntType:
src/ir_print.cpp-18
...@@ -565,18 +565,6 @@ static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {...@@ -565,18 +565,6 @@ static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
565 ir_print_other_instruction(irp, instruction->value);565 ir_print_other_instruction(irp, instruction->value);
566}566}
567567
568static void ir_print_min_value(IrPrint *irp, IrInstructionMinValue *instruction) {
569 fprintf(irp->f, "@minValue(");
570 ir_print_other_instruction(irp, instruction->value);
571 fprintf(irp->f, ")");
572}
573
574static void ir_print_max_value(IrPrint *irp, IrInstructionMaxValue *instruction) {
575 fprintf(irp->f, "@maxValue(");
576 ir_print_other_instruction(irp, instruction->value);
577 fprintf(irp->f, ")");
578}
579
580static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {568static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {
581 fprintf(irp->f, "@compileError(");569 fprintf(irp->f, "@compileError(");
582 ir_print_other_instruction(irp, instruction->msg);570 ir_print_other_instruction(irp, instruction->msg);
...@@ -1480,12 +1468,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1480,12 +1468,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1480 case IrInstructionIdRef:1468 case IrInstructionIdRef:
1481 ir_print_ref(irp, (IrInstructionRef *)instruction);1469 ir_print_ref(irp, (IrInstructionRef *)instruction);
1482 break;1470 break;
1483 case IrInstructionIdMinValue:
1484 ir_print_min_value(irp, (IrInstructionMinValue *)instruction);
1485 break;
1486 case IrInstructionIdMaxValue:
1487 ir_print_max_value(irp, (IrInstructionMaxValue *)instruction);
1488 break;
1489 case IrInstructionIdCompileErr:1471 case IrInstructionIdCompileErr:
1490 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);1472 ir_print_compile_err(irp, (IrInstructionCompileErr *)instruction);
1491 break;1473 break;
std/crypto/chacha20.zig+3-2
...@@ -5,6 +5,7 @@ const mem = std.mem;...@@ -5,6 +5,7 @@ const mem = std.mem;
5const endian = std.endian;5const endian = std.endian;
6const assert = std.debug.assert;6const assert = std.debug.assert;
7const builtin = @import("builtin");7const builtin = @import("builtin");
8const maxInt = std.math.maxInt;
89
9const QuarterRound = struct.{10const QuarterRound = struct.{
10 a: usize,11 a: usize,
...@@ -111,7 +112,7 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo...@@ -111,7 +112,7 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo
111/// counter, nonce, and key.112/// counter, nonce, and key.
112pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce: [12]u8) void {113pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce: [12]u8) void {
113 assert(in.len >= out.len);114 assert(in.len >= out.len);
114 assert((in.len >> 6) + counter <= @maxValue(u32));115 assert((in.len >> 6) + counter <= maxInt(u32));
115116
116 var k: [8]u32 = undefined;117 var k: [8]u32 = undefined;
117 var c: [4]u32 = undefined;118 var c: [4]u32 = undefined;
...@@ -161,7 +162,7 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]...@@ -161,7 +162,7 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
161 const big_block = (block_size << 32);162 const big_block = (block_size << 32);
162163
163 // first partial big block164 // first partial big block
164 if (((@intCast(u64, @maxValue(u32) - @truncate(u32, counter)) + 1) << 6) < in.len) {165 if (((@intCast(u64, maxInt(u32) - @truncate(u32, counter)) + 1) << 6) < in.len) {
165 chaCha20_internal(out[cursor..big_block], in[cursor..big_block], k, c);166 chaCha20_internal(out[cursor..big_block], in[cursor..big_block], k, c);
166 cursor = big_block - cursor;167 cursor = big_block - cursor;
167 c[1] += 1;168 c[1] += 1;
std/debug/index.zig+3-2
...@@ -11,6 +11,7 @@ const pdb = std.pdb;...@@ -11,6 +11,7 @@ const pdb = std.pdb;
11const windows = os.windows;11const windows = os.windows;
12const ArrayList = std.ArrayList;12const ArrayList = std.ArrayList;
13const builtin = @import("builtin");13const builtin = @import("builtin");
14const maxInt = std.math.maxInt;
1415
15pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator;16pub const FailingAllocator = @import("failing_allocator.zig").FailingAllocator;
16pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator;17pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator;
...@@ -842,7 +843,7 @@ fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {...@@ -842,7 +843,7 @@ fn readSparseBitVector(stream: var, allocator: *mem.Allocator) ![]usize {
842 if (word & (u32(1) << bit_i) != 0) {843 if (word & (u32(1) << bit_i) != 0) {
843 try list.append(word_i * 32 + bit_i);844 try list.append(word_i * 32 + bit_i);
844 }845 }
845 if (bit_i == @maxValue(u5)) break;846 if (bit_i == maxInt(u5)) break;
846 }847 }
847 }848 }
848 return list.toOwnedSlice();849 return list.toOwnedSlice();
...@@ -1939,7 +1940,7 @@ fn findCompileUnit(st: *DebugInfo, target_address: u64) !*const CompileUnit {...@@ -1939,7 +1940,7 @@ fn findCompileUnit(st: *DebugInfo, target_address: u64) !*const CompileUnit {
1939 if (begin_addr == 0 and end_addr == 0) {1940 if (begin_addr == 0 and end_addr == 0) {
1940 break;1941 break;
1941 }1942 }
1942 if (begin_addr == @maxValue(usize)) {1943 if (begin_addr == maxInt(usize)) {
1943 base_address = begin_addr;1944 base_address = begin_addr;
1944 continue;1945 continue;
1945 }1946 }
std/dynamic_library.zig+3-2
...@@ -10,6 +10,7 @@ const elf = std.elf;...@@ -10,6 +10,7 @@ const elf = std.elf;
10const linux = os.linux;10const linux = os.linux;
11const windows = os.windows;11const windows = os.windows;
12const win_util = @import("os/windows/util.zig");12const win_util = @import("os/windows/util.zig");
13const maxInt = std.math.maxInt;
1314
14pub const DynLib = switch (builtin.os) {15pub const DynLib = switch (builtin.os) {
15 Os.linux => LinuxDynLib,16 Os.linux => LinuxDynLib,
...@@ -80,7 +81,7 @@ pub const ElfLib = struct.{...@@ -80,7 +81,7 @@ pub const ElfLib = struct.{
80 const elf_addr = @ptrToInt(bytes.ptr);81 const elf_addr = @ptrToInt(bytes.ptr);
81 var ph_addr: usize = elf_addr + eh.e_phoff;82 var ph_addr: usize = elf_addr + eh.e_phoff;
8283
83 var base: usize = @maxValue(usize);84 var base: usize = maxInt(usize);
84 var maybe_dynv: ?[*]usize = null;85 var maybe_dynv: ?[*]usize = null;
85 {86 {
86 var i: usize = 0;87 var i: usize = 0;
...@@ -97,7 +98,7 @@ pub const ElfLib = struct.{...@@ -97,7 +98,7 @@ pub const ElfLib = struct.{
97 }98 }
98 }99 }
99 const dynv = maybe_dynv orelse return error.MissingDynamicLinkingInformation;100 const dynv = maybe_dynv orelse return error.MissingDynamicLinkingInformation;
100 if (base == @maxValue(usize)) return error.BaseNotFound;101 if (base == maxInt(usize)) return error.BaseNotFound;
101102
102 var maybe_strings: ?[*]u8 = null;103 var maybe_strings: ?[*]u8 = null;
103 var maybe_syms: ?[*]elf.Sym = null;104 var maybe_syms: ?[*]elf.Sym = null;
std/event/loop.zig+2-1
...@@ -8,6 +8,7 @@ const fs = std.event.fs;...@@ -8,6 +8,7 @@ const fs = std.event.fs;
8const os = std.os;8const os = std.os;
9const posix = os.posix;9const posix = os.posix;
10const windows = os.windows;10const windows = os.windows;
11const maxInt = std.math.maxInt;
1112
12pub const Loop = struct.{13pub const Loop = struct.{
13 allocator: *mem.Allocator,14 allocator: *mem.Allocator,
...@@ -317,7 +318,7 @@ pub const Loop = struct.{...@@ -317,7 +318,7 @@ pub const Loop = struct.{
317 windows.INVALID_HANDLE_VALUE,318 windows.INVALID_HANDLE_VALUE,
318 null,319 null,
319 undefined,320 undefined,
320 @maxValue(windows.DWORD),321 maxInt(windows.DWORD),
321 );322 );
322 errdefer os.close(self.os_data.io_port);323 errdefer os.close(self.os_data.io_port);
323324
std/heap.zig+2-1
...@@ -6,6 +6,7 @@ const os = std.os;...@@ -6,6 +6,7 @@ const os = std.os;
6const builtin = @import("builtin");6const builtin = @import("builtin");
7const Os = builtin.Os;7const Os = builtin.Os;
8const c = std.c;8const c = std.c;
9const maxInt = std.math.maxInt;
910
10const Allocator = mem.Allocator;11const Allocator = mem.Allocator;
1112
...@@ -567,7 +568,7 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi...@@ -567,7 +568,7 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi
567fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {568fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
568 //Maybe a platform's page_size is actually the same as or569 //Maybe a platform's page_size is actually the same as or
569 // very near usize?570 // very near usize?
570 if (os.page_size << 2 > @maxValue(usize)) return;571 if (os.page_size << 2 > maxInt(usize)) return;
571572
572 const USizeShift = @IntType(false, std.math.log2(usize.bit_count));573 const USizeShift = @IntType(false, std.math.log2(usize.bit_count));
573 const large_align = u29(os.page_size << 2);574 const large_align = u29(os.page_size << 2);
std/json.zig+2-1
...@@ -5,6 +5,7 @@...@@ -5,6 +5,7 @@
5const std = @import("index.zig");5const std = @import("index.zig");
6const debug = std.debug;6const debug = std.debug;
7const mem = std.mem;7const mem = std.mem;
8const maxInt = std.math.maxInt;
89
9// A single token slice into the parent string.10// A single token slice into the parent string.
10//11//
...@@ -107,7 +108,7 @@ pub const StreamingParser = struct.{...@@ -107,7 +108,7 @@ pub const StreamingParser = struct.{
107108
108 const object_bit = 0;109 const object_bit = 0;
109 const array_bit = 1;110 const array_bit = 1;
110 const max_stack_size = @maxValue(u8);111 const max_stack_size = maxInt(u8);
111112
112 pub fn init() StreamingParser {113 pub fn init() StreamingParser {
113 var p: StreamingParser = undefined;114 var p: StreamingParser = undefined;
std/math/asinh.zig+2-1
...@@ -7,6 +7,7 @@...@@ -7,6 +7,7 @@
7const std = @import("../index.zig");7const std = @import("../index.zig");
8const math = std.math;8const math = std.math;
9const assert = std.debug.assert;9const assert = std.debug.assert;
10const maxInt = std.math.maxInt;
1011
11pub fn asinh(x: var) @typeOf(x) {12pub fn asinh(x: var) @typeOf(x) {
12 const T = @typeOf(x);13 const T = @typeOf(x);
...@@ -55,7 +56,7 @@ fn asinh64(x: f64) f64 {...@@ -55,7 +56,7 @@ fn asinh64(x: f64) f64 {
55 const e = (u >> 52) & 0x7FF;56 const e = (u >> 52) & 0x7FF;
56 const s = u >> 63;57 const s = u >> 63;
5758
58 var rx = @bitCast(f64, u & (@maxValue(u64) >> 1)); // |x|59 var rx = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x|
5960
60 if (math.isNegativeInf(x)) {61 if (math.isNegativeInf(x)) {
61 return x;62 return x;
std/math/atanh.zig+2-1
...@@ -7,6 +7,7 @@...@@ -7,6 +7,7 @@
7const std = @import("../index.zig");7const std = @import("../index.zig");
8const math = std.math;8const math = std.math;
9const assert = std.debug.assert;9const assert = std.debug.assert;
10const maxInt = std.math.maxInt;
1011
11pub fn atanh(x: var) @typeOf(x) {12pub fn atanh(x: var) @typeOf(x) {
12 const T = @typeOf(x);13 const T = @typeOf(x);
...@@ -52,7 +53,7 @@ fn atanh_64(x: f64) f64 {...@@ -52,7 +53,7 @@ fn atanh_64(x: f64) f64 {
52 const e = (u >> 52) & 0x7FF;53 const e = (u >> 52) & 0x7FF;
53 const s = u >> 63;54 const s = u >> 63;
5455
55 var y = @bitCast(f64, u & (@maxValue(u64) >> 1)); // |x|56 var y = @bitCast(f64, u & (maxInt(u64) >> 1)); // |x|
5657
57 if (y == 1.0) {58 if (y == 1.0) {
58 return math.copysign(f64, math.inf(f64), x);59 return math.copysign(f64, math.inf(f64), x);
std/math/big/int.zig+31-29
...@@ -5,6 +5,8 @@ const math = std.math;...@@ -5,6 +5,8 @@ const math = std.math;
5const mem = std.mem;5const mem = std.mem;
6const Allocator = mem.Allocator;6const Allocator = mem.Allocator;
7const ArrayList = std.ArrayList;7const ArrayList = std.ArrayList;
8const maxInt = std.math.maxInt;
9const minInt = std.math.minInt;
810
9const TypeId = builtin.TypeId;11const TypeId = builtin.TypeId;
1012
...@@ -212,7 +214,7 @@ pub const Int = struct.{...@@ -212,7 +214,7 @@ pub const Int = struct.{
212 self.positive = value >= 0;214 self.positive = value >= 0;
213 self.len = req_limbs;215 self.len = req_limbs;
214216
215 if (w_value <= @maxValue(Limb)) {217 if (w_value <= maxInt(Limb)) {
216 self.limbs[0] = w_value;218 self.limbs[0] = w_value;
217 } else {219 } else {
218 const mask = (1 << Limb.bit_count) - 1;220 const mask = (1 << Limb.bit_count) - 1;
...@@ -267,7 +269,7 @@ pub const Int = struct.{...@@ -267,7 +269,7 @@ pub const Int = struct.{
267 if (math.cast(T, r)) |ok| {269 if (math.cast(T, r)) |ok| {
268 return -ok;270 return -ok;
269 } else |_| {271 } else |_| {
270 return @minValue(T);272 return minInt(T);
271 }273 }
272 }274 }
273 }275 }
...@@ -371,7 +373,7 @@ pub const Int = struct.{...@@ -371,7 +373,7 @@ pub const Int = struct.{
371 }373 }
372 } // Non power-of-two: batch divisions per word size.374 } // Non power-of-two: batch divisions per word size.
373 else {375 else {
374 const digits_per_limb = math.log(Limb, base, @maxValue(Limb));376 const digits_per_limb = math.log(Limb, base, maxInt(Limb));
375 var limb_base: Limb = 1;377 var limb_base: Limb = 1;
376 var j: usize = 0;378 var j: usize = 0;
377 while (j < digits_per_limb) : (j += 1) {379 while (j < digits_per_limb) : (j += 1) {
...@@ -717,7 +719,7 @@ pub const Int = struct.{...@@ -717,7 +719,7 @@ pub const Int = struct.{
717 const c3: Limb = @boolToInt(@addWithOverflow(Limb, r1, r2, &r1));719 const c3: Limb = @boolToInt(@addWithOverflow(Limb, r1, r2, &r1));
718720
719 // This never overflows, c1, c3 are either 0 or 1 and if both are 1 then721 // This never overflows, c1, c3 are either 0 or 1 and if both are 1 then
720 // c2 is at least <= @maxValue(Limb) - 2.722 // c2 is at least <= maxInt(Limb) - 2.
721 carry.* = c1 + c2 + c3;723 carry.* = c1 + c2 + c3;
722724
723 return r1;725 return r1;
...@@ -873,11 +875,11 @@ pub const Int = struct.{...@@ -873,11 +875,11 @@ pub const Int = struct.{
873 while (i > t) : (i -= 1) {875 while (i > t) : (i -= 1) {
874 // 3.1876 // 3.1
875 if (x.limbs[i] == y.limbs[t]) {877 if (x.limbs[i] == y.limbs[t]) {
876 q.limbs[i - t - 1] = @maxValue(Limb);878 q.limbs[i - t - 1] = maxInt(Limb);
877 } else {879 } else {
878 const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]);880 const num = (DoubleLimb(x.limbs[i]) << Limb.bit_count) | DoubleLimb(x.limbs[i - 1]);
879 const z = @intCast(Limb, num / DoubleLimb(y.limbs[t]));881 const z = @intCast(Limb, num / DoubleLimb(y.limbs[t]));
880 q.limbs[i - t - 1] = if (z > @maxValue(Limb)) @maxValue(Limb) else Limb(z);882 q.limbs[i - t - 1] = if (z > maxInt(Limb)) maxInt(Limb) else Limb(z);
881 }883 }
882884
883 // 3.2885 // 3.2
...@@ -1081,7 +1083,7 @@ test "big.int comptime_int set" {...@@ -1081,7 +1083,7 @@ test "big.int comptime_int set" {
10811083
1082 comptime var i: usize = 0;1084 comptime var i: usize = 0;
1083 inline while (i < s_limb_count) : (i += 1) {1085 inline while (i < s_limb_count) : (i += 1) {
1084 const result = Limb(s & @maxValue(Limb));1086 const result = Limb(s & maxInt(Limb));
1085 s >>= Limb.bit_count / 2;1087 s >>= Limb.bit_count / 2;
1086 s >>= Limb.bit_count / 2;1088 s >>= Limb.bit_count / 2;
1087 debug.assert(a.limbs[i] == result);1089 debug.assert(a.limbs[i] == result);
...@@ -1403,7 +1405,7 @@ test "big.int compare similar" {...@@ -1403,7 +1405,7 @@ test "big.int compare similar" {
1403}1405}
14041406
1405test "big.int compare different limb size" {1407test "big.int compare different limb size" {
1406 var a = try Int.initSet(al, @maxValue(Limb) + 1);1408 var a = try Int.initSet(al, maxInt(Limb) + 1);
1407 var b = try Int.initSet(al, 1);1409 var b = try Int.initSet(al, 1);
14081410
1409 debug.assert(a.cmpAbs(b) == 1);1411 debug.assert(a.cmpAbs(b) == 1);
...@@ -1457,16 +1459,16 @@ test "big.int add single-single" {...@@ -1457,16 +1459,16 @@ test "big.int add single-single" {
1457}1459}
14581460
1459test "big.int add multi-single" {1461test "big.int add multi-single" {
1460 var a = try Int.initSet(al, @maxValue(Limb) + 1);1462 var a = try Int.initSet(al, maxInt(Limb) + 1);
1461 var b = try Int.initSet(al, 1);1463 var b = try Int.initSet(al, 1);
14621464
1463 var c = try Int.init(al);1465 var c = try Int.init(al);
14641466
1465 try c.add(a, b);1467 try c.add(a, b);
1466 debug.assert((try c.to(DoubleLimb)) == @maxValue(Limb) + 2);1468 debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
14671469
1468 try c.add(b, a);1470 try c.add(b, a);
1469 debug.assert((try c.to(DoubleLimb)) == @maxValue(Limb) + 2);1471 debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
1470}1472}
14711473
1472test "big.int add multi-multi" {1474test "big.int add multi-multi" {
...@@ -1533,13 +1535,13 @@ test "big.int sub single-single" {...@@ -1533,13 +1535,13 @@ test "big.int sub single-single" {
1533}1535}
15341536
1535test "big.int sub multi-single" {1537test "big.int sub multi-single" {
1536 var a = try Int.initSet(al, @maxValue(Limb) + 1);1538 var a = try Int.initSet(al, maxInt(Limb) + 1);
1537 var b = try Int.initSet(al, 1);1539 var b = try Int.initSet(al, 1);
15381540
1539 var c = try Int.init(al);1541 var c = try Int.init(al);
1540 try c.sub(a, b);1542 try c.sub(a, b);
15411543
1542 debug.assert((try c.to(Limb)) == @maxValue(Limb));1544 debug.assert((try c.to(Limb)) == maxInt(Limb));
1543}1545}
15441546
1545test "big.int sub multi-multi" {1547test "big.int sub multi-multi" {
...@@ -1600,13 +1602,13 @@ test "big.int mul single-single" {...@@ -1600,13 +1602,13 @@ test "big.int mul single-single" {
1600}1602}
16011603
1602test "big.int mul multi-single" {1604test "big.int mul multi-single" {
1603 var a = try Int.initSet(al, @maxValue(Limb));1605 var a = try Int.initSet(al, maxInt(Limb));
1604 var b = try Int.initSet(al, 2);1606 var b = try Int.initSet(al, 2);
16051607
1606 var c = try Int.init(al);1608 var c = try Int.init(al);
1607 try c.mul(a, b);1609 try c.mul(a, b);
16081610
1609 debug.assert((try c.to(DoubleLimb)) == 2 * @maxValue(Limb));1611 debug.assert((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
1610}1612}
16111613
1612test "big.int mul multi-multi" {1614test "big.int mul multi-multi" {
...@@ -1622,29 +1624,29 @@ test "big.int mul multi-multi" {...@@ -1622,29 +1624,29 @@ test "big.int mul multi-multi" {
1622}1624}
16231625
1624test "big.int mul alias r with a" {1626test "big.int mul alias r with a" {
1625 var a = try Int.initSet(al, @maxValue(Limb));1627 var a = try Int.initSet(al, maxInt(Limb));
1626 var b = try Int.initSet(al, 2);1628 var b = try Int.initSet(al, 2);
16271629
1628 try a.mul(a, b);1630 try a.mul(a, b);
16291631
1630 debug.assert((try a.to(DoubleLimb)) == 2 * @maxValue(Limb));1632 debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
1631}1633}
16321634
1633test "big.int mul alias r with b" {1635test "big.int mul alias r with b" {
1634 var a = try Int.initSet(al, @maxValue(Limb));1636 var a = try Int.initSet(al, maxInt(Limb));
1635 var b = try Int.initSet(al, 2);1637 var b = try Int.initSet(al, 2);
16361638
1637 try a.mul(b, a);1639 try a.mul(b, a);
16381640
1639 debug.assert((try a.to(DoubleLimb)) == 2 * @maxValue(Limb));1641 debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
1640}1642}
16411643
1642test "big.int mul alias r with a and b" {1644test "big.int mul alias r with a and b" {
1643 var a = try Int.initSet(al, @maxValue(Limb));1645 var a = try Int.initSet(al, maxInt(Limb));
16441646
1645 try a.mul(a, a);1647 try a.mul(a, a);
16461648
1647 debug.assert((try a.to(DoubleLimb)) == @maxValue(Limb) * @maxValue(Limb));1649 debug.assert((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));
1648}1650}
16491651
1650test "big.int mul a*0" {1652test "big.int mul a*0" {
...@@ -2047,8 +2049,8 @@ test "big.int bitwise and simple" {...@@ -2047,8 +2049,8 @@ test "big.int bitwise and simple" {
2047}2049}
20482050
2049test "big.int bitwise and multi-limb" {2051test "big.int bitwise and multi-limb" {
2050 var a = try Int.initSet(al, @maxValue(Limb) + 1);2052 var a = try Int.initSet(al, maxInt(Limb) + 1);
2051 var b = try Int.initSet(al, @maxValue(Limb));2053 var b = try Int.initSet(al, maxInt(Limb));
20522054
2053 try a.bitAnd(a, b);2055 try a.bitAnd(a, b);
20542056
...@@ -2065,12 +2067,12 @@ test "big.int bitwise xor simple" {...@@ -2065,12 +2067,12 @@ test "big.int bitwise xor simple" {
2065}2067}
20662068
2067test "big.int bitwise xor multi-limb" {2069test "big.int bitwise xor multi-limb" {
2068 var a = try Int.initSet(al, @maxValue(Limb) + 1);2070 var a = try Int.initSet(al, maxInt(Limb) + 1);
2069 var b = try Int.initSet(al, @maxValue(Limb));2071 var b = try Int.initSet(al, maxInt(Limb));
20702072
2071 try a.bitXor(a, b);2073 try a.bitXor(a, b);
20722074
2073 debug.assert((try a.to(DoubleLimb)) == (@maxValue(Limb) + 1) ^ @maxValue(Limb));2075 debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));
2074}2076}
20752077
2076test "big.int bitwise or simple" {2078test "big.int bitwise or simple" {
...@@ -2083,13 +2085,13 @@ test "big.int bitwise or simple" {...@@ -2083,13 +2085,13 @@ test "big.int bitwise or simple" {
2083}2085}
20842086
2085test "big.int bitwise or multi-limb" {2087test "big.int bitwise or multi-limb" {
2086 var a = try Int.initSet(al, @maxValue(Limb) + 1);2088 var a = try Int.initSet(al, maxInt(Limb) + 1);
2087 var b = try Int.initSet(al, @maxValue(Limb));2089 var b = try Int.initSet(al, maxInt(Limb));
20882090
2089 try a.bitOr(a, b);2091 try a.bitOr(a, b);
20902092
2091 // TODO: big.int.cpp or is wrong on multi-limb.2093 // TODO: big.int.cpp or is wrong on multi-limb.
2092 debug.assert((try a.to(DoubleLimb)) == (@maxValue(Limb) + 1) + @maxValue(Limb));2094 debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));
2093}2095}
20942096
2095test "big.int var args" {2097test "big.int var args" {
std/math/copysign.zig+4-3
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub fn copysign(comptime T: type, x: T, y: T) T {6pub fn copysign(comptime T: type, x: T, y: T) T {
6 return switch (T) {7 return switch (T) {
...@@ -15,7 +16,7 @@ fn copysign16(x: f16, y: f16) f16 {...@@ -15,7 +16,7 @@ fn copysign16(x: f16, y: f16) f16 {
15 const ux = @bitCast(u16, x);16 const ux = @bitCast(u16, x);
16 const uy = @bitCast(u16, y);17 const uy = @bitCast(u16, y);
1718
18 const h1 = ux & (@maxValue(u16) / 2);19 const h1 = ux & (maxInt(u16) / 2);
19 const h2 = uy & (u16(1) << 15);20 const h2 = uy & (u16(1) << 15);
20 return @bitCast(f16, h1 | h2);21 return @bitCast(f16, h1 | h2);
21}22}
...@@ -24,7 +25,7 @@ fn copysign32(x: f32, y: f32) f32 {...@@ -24,7 +25,7 @@ fn copysign32(x: f32, y: f32) f32 {
24 const ux = @bitCast(u32, x);25 const ux = @bitCast(u32, x);
25 const uy = @bitCast(u32, y);26 const uy = @bitCast(u32, y);
2627
27 const h1 = ux & (@maxValue(u32) / 2);28 const h1 = ux & (maxInt(u32) / 2);
28 const h2 = uy & (u32(1) << 31);29 const h2 = uy & (u32(1) << 31);
29 return @bitCast(f32, h1 | h2);30 return @bitCast(f32, h1 | h2);
30}31}
...@@ -33,7 +34,7 @@ fn copysign64(x: f64, y: f64) f64 {...@@ -33,7 +34,7 @@ fn copysign64(x: f64, y: f64) f64 {
33 const ux = @bitCast(u64, x);34 const ux = @bitCast(u64, x);
34 const uy = @bitCast(u64, y);35 const uy = @bitCast(u64, y);
3536
36 const h1 = ux & (@maxValue(u64) / 2);37 const h1 = ux & (maxInt(u64) / 2);
37 const h2 = uy & (u64(1) << 63);38 const h2 = uy & (u64(1) << 63);
38 return @bitCast(f64, h1 | h2);39 return @bitCast(f64, h1 | h2);
39}40}
std/math/cosh.zig+2-1
...@@ -9,6 +9,7 @@ const std = @import("../index.zig");...@@ -9,6 +9,7 @@ const std = @import("../index.zig");
9const math = std.math;9const math = std.math;
10const expo2 = @import("expo2.zig").expo2;10const expo2 = @import("expo2.zig").expo2;
11const assert = std.debug.assert;11const assert = std.debug.assert;
12const maxInt = std.math.maxInt;
1213
13pub fn cosh(x: var) @typeOf(x) {14pub fn cosh(x: var) @typeOf(x) {
14 const T = @typeOf(x);15 const T = @typeOf(x);
...@@ -50,7 +51,7 @@ fn cosh32(x: f32) f32 {...@@ -50,7 +51,7 @@ fn cosh32(x: f32) f32 {
50fn cosh64(x: f64) f64 {51fn cosh64(x: f64) f64 {
51 const u = @bitCast(u64, x);52 const u = @bitCast(u64, x);
52 const w = @intCast(u32, u >> 32);53 const w = @intCast(u32, u >> 32);
53 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));54 const ax = @bitCast(f64, u & (maxInt(u64) >> 1));
5455
55 // TODO: Shouldn't need this explicit check.56 // TODO: Shouldn't need this explicit check.
56 if (x == 0.0) {57 if (x == 0.0) {
std/math/fabs.zig+2-1
...@@ -6,6 +6,7 @@...@@ -6,6 +6,7 @@
6const std = @import("../index.zig");6const std = @import("../index.zig");
7const math = std.math;7const math = std.math;
8const assert = std.debug.assert;8const assert = std.debug.assert;
9const maxInt = std.math.maxInt;
910
10pub fn fabs(x: var) @typeOf(x) {11pub fn fabs(x: var) @typeOf(x) {
11 const T = @typeOf(x);12 const T = @typeOf(x);
...@@ -31,7 +32,7 @@ fn fabs32(x: f32) f32 {...@@ -31,7 +32,7 @@ fn fabs32(x: f32) f32 {
3132
32fn fabs64(x: f64) f64 {33fn fabs64(x: f64) f64 {
33 var u = @bitCast(u64, x);34 var u = @bitCast(u64, x);
34 u &= @maxValue(u64) >> 1;35 u &= maxInt(u64) >> 1;
35 return @bitCast(f64, u);36 return @bitCast(f64, u);
36}37}
3738
std/math/hypot.zig+5-4
...@@ -8,6 +8,7 @@...@@ -8,6 +8,7 @@
8const std = @import("../index.zig");8const std = @import("../index.zig");
9const math = std.math;9const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const maxInt = std.math.maxInt;
1112
12pub fn hypot(comptime T: type, x: T, y: T) T {13pub fn hypot(comptime T: type, x: T, y: T) T {
13 return switch (T) {14 return switch (T) {
...@@ -21,8 +22,8 @@ fn hypot32(x: f32, y: f32) f32 {...@@ -21,8 +22,8 @@ fn hypot32(x: f32, y: f32) f32 {
21 var ux = @bitCast(u32, x);22 var ux = @bitCast(u32, x);
22 var uy = @bitCast(u32, y);23 var uy = @bitCast(u32, y);
2324
24 ux &= @maxValue(u32) >> 1;25 ux &= maxInt(u32) >> 1;
25 uy &= @maxValue(u32) >> 1;26 uy &= maxInt(u32) >> 1;
26 if (ux < uy) {27 if (ux < uy) {
27 const tmp = ux;28 const tmp = ux;
28 ux = uy;29 ux = uy;
...@@ -65,8 +66,8 @@ fn hypot64(x: f64, y: f64) f64 {...@@ -65,8 +66,8 @@ fn hypot64(x: f64, y: f64) f64 {
65 var ux = @bitCast(u64, x);66 var ux = @bitCast(u64, x);
66 var uy = @bitCast(u64, y);67 var uy = @bitCast(u64, y);
6768
68 ux &= @maxValue(u64) >> 1;69 ux &= maxInt(u64) >> 1;
69 uy &= @maxValue(u64) >> 1;70 uy &= maxInt(u64) >> 1;
70 if (ux < uy) {71 if (ux < uy) {
71 const tmp = ux;72 const tmp = ux;
72 ux = uy;73 ux = uy;
std/math/ilogb.zig+18-16
...@@ -1,12 +1,14 @@...@@ -1,12 +1,14 @@
1// Special Cases:1// Special Cases:
2//2//
3// - ilogb(+-inf) = @maxValue(i32)3// - ilogb(+-inf) = maxInt(i32)
4// - ilogb(0) = @maxValue(i32)4// - ilogb(0) = maxInt(i32)
5// - ilogb(nan) = @maxValue(i32)5// - ilogb(nan) = maxInt(i32)
66
7const std = @import("../index.zig");7const std = @import("../index.zig");
8const math = std.math;8const math = std.math;
9const assert = std.debug.assert;9const assert = std.debug.assert;
10const maxInt = std.math.maxInt;
11const minInt = std.math.minInt;
1012
11pub fn ilogb(x: var) i32 {13pub fn ilogb(x: var) i32 {
12 const T = @typeOf(x);14 const T = @typeOf(x);
...@@ -18,7 +20,7 @@ pub fn ilogb(x: var) i32 {...@@ -18,7 +20,7 @@ pub fn ilogb(x: var) i32 {
18}20}
1921
20// NOTE: Should these be exposed publically?22// NOTE: Should these be exposed publically?
21const fp_ilogbnan = -1 - i32(@maxValue(u32) >> 1);23const fp_ilogbnan = -1 - i32(maxInt(u32) >> 1);
22const fp_ilogb0 = fp_ilogbnan;24const fp_ilogb0 = fp_ilogbnan;
2325
24fn ilogb32(x: f32) i32 {26fn ilogb32(x: f32) i32 {
...@@ -27,7 +29,7 @@ fn ilogb32(x: f32) i32 {...@@ -27,7 +29,7 @@ fn ilogb32(x: f32) i32 {
2729
28 // TODO: We should be able to merge this with the lower check.30 // TODO: We should be able to merge this with the lower check.
29 if (math.isNan(x)) {31 if (math.isNan(x)) {
30 return @maxValue(i32);32 return maxInt(i32);
31 }33 }
3234
33 if (e == 0) {35 if (e == 0) {
...@@ -50,7 +52,7 @@ fn ilogb32(x: f32) i32 {...@@ -50,7 +52,7 @@ fn ilogb32(x: f32) i32 {
50 if (u << 9 != 0) {52 if (u << 9 != 0) {
51 return fp_ilogbnan;53 return fp_ilogbnan;
52 } else {54 } else {
53 return @maxValue(i32);55 return maxInt(i32);
54 }56 }
55 }57 }
5658
...@@ -62,7 +64,7 @@ fn ilogb64(x: f64) i32 {...@@ -62,7 +64,7 @@ fn ilogb64(x: f64) i32 {
62 var e = @intCast(i32, (u >> 52) & 0x7FF);64 var e = @intCast(i32, (u >> 52) & 0x7FF);
6365
64 if (math.isNan(x)) {66 if (math.isNan(x)) {
65 return @maxValue(i32);67 return maxInt(i32);
66 }68 }
6769
68 if (e == 0) {70 if (e == 0) {
...@@ -85,7 +87,7 @@ fn ilogb64(x: f64) i32 {...@@ -85,7 +87,7 @@ fn ilogb64(x: f64) i32 {
85 if (u << 12 != 0) {87 if (u << 12 != 0) {
86 return fp_ilogbnan;88 return fp_ilogbnan;
87 } else {89 } else {
88 return @maxValue(i32);90 return maxInt(i32);
89 }91 }
90 }92 }
9193
...@@ -116,15 +118,15 @@ test "math.ilogb64" {...@@ -116,15 +118,15 @@ test "math.ilogb64" {
116}118}
117119
118test "math.ilogb32.special" {120test "math.ilogb32.special" {
119 assert(ilogb32(math.inf(f32)) == @maxValue(i32));121 assert(ilogb32(math.inf(f32)) == maxInt(i32));
120 assert(ilogb32(-math.inf(f32)) == @maxValue(i32));122 assert(ilogb32(-math.inf(f32)) == maxInt(i32));
121 assert(ilogb32(0.0) == @minValue(i32));123 assert(ilogb32(0.0) == minInt(i32));
122 assert(ilogb32(math.nan(f32)) == @maxValue(i32));124 assert(ilogb32(math.nan(f32)) == maxInt(i32));
123}125}
124126
125test "math.ilogb64.special" {127test "math.ilogb64.special" {
126 assert(ilogb64(math.inf(f64)) == @maxValue(i32));128 assert(ilogb64(math.inf(f64)) == maxInt(i32));
127 assert(ilogb64(-math.inf(f64)) == @maxValue(i32));129 assert(ilogb64(-math.inf(f64)) == maxInt(i32));
128 assert(ilogb64(0.0) == @minValue(i32));130 assert(ilogb64(0.0) == minInt(i32));
129 assert(ilogb64(math.nan(f64)) == @maxValue(i32));131 assert(ilogb64(math.nan(f64)) == maxInt(i32));
130}132}
std/math/index.zig+69-13
...@@ -382,7 +382,7 @@ pub fn absInt(x: var) !@typeOf(x) {...@@ -382,7 +382,7 @@ pub fn absInt(x: var) !@typeOf(x) {
382 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt382 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
383 comptime assert(T.is_signed); // must pass a signed integer to absInt383 comptime assert(T.is_signed); // must pass a signed integer to absInt
384384
385 if (x == @minValue(@typeOf(x))) {385 if (x == minInt(@typeOf(x))) {
386 return error.Overflow;386 return error.Overflow;
387 } else {387 } else {
388 @setRuntimeSafety(false);388 @setRuntimeSafety(false);
...@@ -404,7 +404,7 @@ pub const absFloat = @import("fabs.zig").fabs;...@@ -404,7 +404,7 @@ pub const absFloat = @import("fabs.zig").fabs;
404pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {404pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
405 @setRuntimeSafety(false);405 @setRuntimeSafety(false);
406 if (denominator == 0) return error.DivisionByZero;406 if (denominator == 0) return error.DivisionByZero;
407 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;407 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
408 return @divTrunc(numerator, denominator);408 return @divTrunc(numerator, denominator);
409}409}
410410
...@@ -425,7 +425,7 @@ fn testDivTrunc() void {...@@ -425,7 +425,7 @@ fn testDivTrunc() void {
425pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {425pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
426 @setRuntimeSafety(false);426 @setRuntimeSafety(false);
427 if (denominator == 0) return error.DivisionByZero;427 if (denominator == 0) return error.DivisionByZero;
428 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;428 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
429 return @divFloor(numerator, denominator);429 return @divFloor(numerator, denominator);
430}430}
431431
...@@ -446,7 +446,7 @@ fn testDivFloor() void {...@@ -446,7 +446,7 @@ fn testDivFloor() void {
446pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {446pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
447 @setRuntimeSafety(false);447 @setRuntimeSafety(false);
448 if (denominator == 0) return error.DivisionByZero;448 if (denominator == 0) return error.DivisionByZero;
449 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;449 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
450 const result = @divTrunc(numerator, denominator);450 const result = @divTrunc(numerator, denominator);
451 if (result * denominator != numerator) return error.UnexpectedRemainder;451 if (result * denominator != numerator) return error.UnexpectedRemainder;
452 return result;452 return result;
...@@ -530,8 +530,8 @@ test "math.absCast" {...@@ -530,8 +530,8 @@ test "math.absCast" {
530 assert(absCast(i32(999)) == 999);530 assert(absCast(i32(999)) == 999);
531 assert(@typeOf(absCast(i32(999))) == u32);531 assert(@typeOf(absCast(i32(999))) == u32);
532532
533 assert(absCast(i32(@minValue(i32))) == -@minValue(i32));533 assert(absCast(i32(minInt(i32))) == -minInt(i32));
534 assert(@typeOf(absCast(i32(@minValue(i32)))) == u32);534 assert(@typeOf(absCast(i32(minInt(i32)))) == u32);
535}535}
536536
537/// Returns the negation of the integer parameter.537/// Returns the negation of the integer parameter.
...@@ -540,9 +540,9 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {...@@ -540,9 +540,9 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
540 if (@typeOf(x).is_signed) return negate(x);540 if (@typeOf(x).is_signed) return negate(x);
541541
542 const int = @IntType(true, @typeOf(x).bit_count);542 const int = @IntType(true, @typeOf(x).bit_count);
543 if (x > -@minValue(int)) return error.Overflow;543 if (x > -minInt(int)) return error.Overflow;
544544
545 if (x == -@minValue(int)) return @minValue(int);545 if (x == -minInt(int)) return minInt(int);
546546
547 return -@intCast(int, x);547 return -@intCast(int, x);
548}548}
...@@ -551,10 +551,10 @@ test "math.negateCast" {...@@ -551,10 +551,10 @@ test "math.negateCast" {
551 assert((negateCast(u32(999)) catch unreachable) == -999);551 assert((negateCast(u32(999)) catch unreachable) == -999);
552 assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32);552 assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32);
553553
554 assert((negateCast(u32(-@minValue(i32))) catch unreachable) == @minValue(i32));554 assert((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32));
555 assert(@typeOf(negateCast(u32(-@minValue(i32))) catch unreachable) == i32);555 assert(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32);
556556
557 if (negateCast(u32(@maxValue(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow);557 if (negateCast(u32(maxInt(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow);
558}558}
559559
560/// Cast an integer to a different integer type. If the value doesn't fit,560/// Cast an integer to a different integer type. If the value doesn't fit,
...@@ -562,9 +562,9 @@ test "math.negateCast" {...@@ -562,9 +562,9 @@ test "math.negateCast" {
562pub fn cast(comptime T: type, x: var) (error.{Overflow}!T) {562pub fn cast(comptime T: type, x: var) (error.{Overflow}!T) {
563 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer563 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
564 comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer564 comptime assert(@typeId(@typeOf(x)) == builtin.TypeId.Int); // must pass an integer
565 if (@maxValue(@typeOf(x)) > @maxValue(T) and x > @maxValue(T)) {565 if (maxInt(@typeOf(x)) > maxInt(T) and x > maxInt(T)) {
566 return error.Overflow;566 return error.Overflow;
567 } else if (@minValue(@typeOf(x)) < @minValue(T) and x < @minValue(T)) {567 } else if (minInt(@typeOf(x)) < minInt(T) and x < minInt(T)) {
568 return error.Overflow;568 return error.Overflow;
569 } else {569 } else {
570 return @intCast(T, x);570 return @intCast(T, x);
...@@ -658,3 +658,59 @@ test "math.f64_min" {...@@ -658,3 +658,59 @@ test "math.f64_min" {
658 const fmin: f64 = f64_min;658 const fmin: f64 = f64_min;
659 assert(@bitCast(u64, fmin) == f64_min_u64);659 assert(@bitCast(u64, fmin) == f64_min_u64);
660}660}
661
662pub fn maxInt(comptime T: type) comptime_int {
663 const info = @typeInfo(T);
664 const bit_count = comptime_int(info.Int.bits); // TODO #1683
665 if (bit_count == 0) return 0;
666 return (1 << (bit_count - @boolToInt(info.Int.is_signed))) - 1;
667}
668
669pub fn minInt(comptime T: type) comptime_int {
670 const info = @typeInfo(T);
671 const bit_count = comptime_int(info.Int.bits); // TODO #1683
672 if (!info.Int.is_signed) return 0;
673 if (bit_count == 0) return 0;
674 return -(1 << (bit_count - 1));
675}
676
677test "minInt and maxInt" {
678 assert(maxInt(u0) == 0);
679 assert(maxInt(u1) == 1);
680 assert(maxInt(u8) == 255);
681 assert(maxInt(u16) == 65535);
682 assert(maxInt(u32) == 4294967295);
683 assert(maxInt(u64) == 18446744073709551615);
684
685 assert(maxInt(i0) == 0);
686 assert(maxInt(i1) == 0);
687 assert(maxInt(i8) == 127);
688 assert(maxInt(i16) == 32767);
689 assert(maxInt(i32) == 2147483647);
690 assert(maxInt(i63) == 4611686018427387903);
691 assert(maxInt(i64) == 9223372036854775807);
692
693 assert(minInt(u0) == 0);
694 assert(minInt(u1) == 0);
695 assert(minInt(u8) == 0);
696 assert(minInt(u16) == 0);
697 assert(minInt(u32) == 0);
698 assert(minInt(u63) == 0);
699 assert(minInt(u64) == 0);
700
701 assert(minInt(i0) == 0);
702 assert(minInt(i1) == -1);
703 assert(minInt(i8) == -128);
704 assert(minInt(i16) == -32768);
705 assert(minInt(i32) == -2147483648);
706 assert(minInt(i63) == -4611686018427387904);
707 assert(minInt(i64) == -9223372036854775808);
708}
709
710test "max value type" {
711 // If the type of maxInt(i32) was i32 then this implicit cast to
712 // u32 would not work. But since the value is a number literal,
713 // it works fine.
714 const x: u32 = maxInt(i32);
715 assert(x == 2147483647);
716}
std/math/isfinite.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub fn isFinite(x: var) bool {6pub fn isFinite(x: var) bool {
6 const T = @typeOf(x);7 const T = @typeOf(x);
...@@ -15,7 +16,7 @@ pub fn isFinite(x: var) bool {...@@ -15,7 +16,7 @@ pub fn isFinite(x: var) bool {
15 },16 },
16 f64 => {17 f64 => {
17 const bits = @bitCast(u64, x);18 const bits = @bitCast(u64, x);
18 return bits & (@maxValue(u64) >> 1) < (0x7FF << 52);19 return bits & (maxInt(u64) >> 1) < (0x7FF << 52);
19 },20 },
20 else => {21 else => {
21 @compileError("isFinite not implemented for " ++ @typeName(T));22 @compileError("isFinite not implemented for " ++ @typeName(T));
std/math/isinf.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub fn isInf(x: var) bool {6pub fn isInf(x: var) bool {
6 const T = @typeOf(x);7 const T = @typeOf(x);
...@@ -15,7 +16,7 @@ pub fn isInf(x: var) bool {...@@ -15,7 +16,7 @@ pub fn isInf(x: var) bool {
15 },16 },
16 f64 => {17 f64 => {
17 const bits = @bitCast(u64, x);18 const bits = @bitCast(u64, x);
18 return bits & (@maxValue(u64) >> 1) == (0x7FF << 52);19 return bits & (maxInt(u64) >> 1) == (0x7FF << 52);
19 },20 },
20 else => {21 else => {
21 @compileError("isInf not implemented for " ++ @typeName(T));22 @compileError("isInf not implemented for " ++ @typeName(T));
std/math/isnan.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub fn isNan(x: var) bool {6pub fn isNan(x: var) bool {
6 const T = @typeOf(x);7 const T = @typeOf(x);
...@@ -15,7 +16,7 @@ pub fn isNan(x: var) bool {...@@ -15,7 +16,7 @@ pub fn isNan(x: var) bool {
15 },16 },
16 f64 => {17 f64 => {
17 const bits = @bitCast(u64, x);18 const bits = @bitCast(u64, x);
18 return (bits & (@maxValue(u64) >> 1)) > (u64(0x7FF) << 52);19 return (bits & (maxInt(u64) >> 1)) > (u64(0x7FF) << 52);
19 },20 },
20 else => {21 else => {
21 @compileError("isNan not implemented for " ++ @typeName(T));22 @compileError("isNan not implemented for " ++ @typeName(T));
std/math/isnormal.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const math = std.math;2const math = std.math;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub fn isNormal(x: var) bool {6pub fn isNormal(x: var) bool {
6 const T = @typeOf(x);7 const T = @typeOf(x);
...@@ -15,7 +16,7 @@ pub fn isNormal(x: var) bool {...@@ -15,7 +16,7 @@ pub fn isNormal(x: var) bool {
15 },16 },
16 f64 => {17 f64 => {
17 const bits = @bitCast(u64, x);18 const bits = @bitCast(u64, x);
18 return (bits + (1 << 52)) & (@maxValue(u64) >> 1) >= (1 << 53);19 return (bits + (1 << 52)) & (maxInt(u64) >> 1) >= (1 << 53);
19 },20 },
20 else => {21 else => {
21 @compileError("isNormal not implemented for " ++ @typeName(T));22 @compileError("isNormal not implemented for " ++ @typeName(T));
std/math/log10.zig+2-1
...@@ -10,6 +10,7 @@ const math = std.math;...@@ -10,6 +10,7 @@ const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const builtin = @import("builtin");11const builtin = @import("builtin");
12const TypeId = builtin.TypeId;12const TypeId = builtin.TypeId;
13const maxInt = std.math.maxInt;
1314
14pub fn log10(x: var) @typeOf(x) {15pub fn log10(x: var) @typeOf(x) {
15 const T = @typeOf(x);16 const T = @typeOf(x);
...@@ -151,7 +152,7 @@ pub fn log10_64(x_: f64) f64 {...@@ -151,7 +152,7 @@ pub fn log10_64(x_: f64) f64 {
151 // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)152 // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)
152 var hi = f - hfsq;153 var hi = f - hfsq;
153 var hii = @bitCast(u64, hi);154 var hii = @bitCast(u64, hi);
154 hii &= u64(@maxValue(u64)) << 32;155 hii &= u64(maxInt(u64)) << 32;
155 hi = @bitCast(f64, hii);156 hi = @bitCast(f64, hii);
156 const lo = f - hi - hfsq + s * (hfsq + R);157 const lo = f - hi - hfsq + s * (hfsq + R);
157158
std/math/log2.zig+2-1
...@@ -10,6 +10,7 @@ const math = std.math;...@@ -10,6 +10,7 @@ const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const builtin = @import("builtin");11const builtin = @import("builtin");
12const TypeId = builtin.TypeId;12const TypeId = builtin.TypeId;
13const maxInt = std.math.maxInt;
1314
14pub fn log2(x: var) @typeOf(x) {15pub fn log2(x: var) @typeOf(x) {
15 const T = @typeOf(x);16 const T = @typeOf(x);
...@@ -151,7 +152,7 @@ pub fn log2_64(x_: f64) f64 {...@@ -151,7 +152,7 @@ pub fn log2_64(x_: f64) f64 {
151 // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)152 // hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)
152 var hi = f - hfsq;153 var hi = f - hfsq;
153 var hii = @bitCast(u64, hi);154 var hii = @bitCast(u64, hi);
154 hii &= u64(@maxValue(u64)) << 32;155 hii &= u64(maxInt(u64)) << 32;
155 hi = @bitCast(f64, hii);156 hi = @bitCast(f64, hii);
156 const lo = f - hi - hfsq + s * (hfsq + R);157 const lo = f - hi - hfsq + s * (hfsq + R);
157158
std/math/modf.zig+2-1
...@@ -6,6 +6,7 @@...@@ -6,6 +6,7 @@
6const std = @import("../index.zig");6const std = @import("../index.zig");
7const math = std.math;7const math = std.math;
8const assert = std.debug.assert;8const assert = std.debug.assert;
9const maxInt = std.math.maxInt;
910
10fn modf_result(comptime T: type) type {11fn modf_result(comptime T: type) type {
11 return struct.{12 return struct.{
...@@ -101,7 +102,7 @@ fn modf64(x: f64) modf64_result {...@@ -101,7 +102,7 @@ fn modf64(x: f64) modf64_result {
101 return result;102 return result;
102 }103 }
103104
104 const mask = u64(@maxValue(u64) >> 12) >> @intCast(u6, e);105 const mask = u64(maxInt(u64) >> 12) >> @intCast(u6, e);
105 if (u & mask == 0) {106 if (u & mask == 0) {
106 result.ipart = x;107 result.ipart = x;
107 result.fpart = @bitCast(f64, us);108 result.fpart = @bitCast(f64, us);
std/math/sinh.zig+2-1
...@@ -9,6 +9,7 @@ const std = @import("../index.zig");...@@ -9,6 +9,7 @@ const std = @import("../index.zig");
9const math = std.math;9const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const expo2 = @import("expo2.zig").expo2;11const expo2 = @import("expo2.zig").expo2;
12const maxInt = std.math.maxInt;
1213
13pub fn sinh(x: var) @typeOf(x) {14pub fn sinh(x: var) @typeOf(x) {
14 const T = @typeOf(x);15 const T = @typeOf(x);
...@@ -56,7 +57,7 @@ fn sinh32(x: f32) f32 {...@@ -56,7 +57,7 @@ fn sinh32(x: f32) f32 {
56fn sinh64(x: f64) f64 {57fn sinh64(x: f64) f64 {
57 const u = @bitCast(u64, x);58 const u = @bitCast(u64, x);
58 const w = @intCast(u32, u >> 32);59 const w = @intCast(u32, u >> 32);
59 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));60 const ax = @bitCast(f64, u & (maxInt(u64) >> 1));
6061
61 if (x == 0.0 or math.isNan(x)) {62 if (x == 0.0 or math.isNan(x)) {
62 return x;63 return x;
std/math/sqrt.zig+2-1
...@@ -10,6 +10,7 @@ const math = std.math;...@@ -10,6 +10,7 @@ const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const builtin = @import("builtin");11const builtin = @import("builtin");
12const TypeId = builtin.TypeId;12const TypeId = builtin.TypeId;
13const maxInt = std.math.maxInt;
1314
14pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) {15pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) {
15 const T = @typeOf(x);16 const T = @typeOf(x);
...@@ -17,7 +18,7 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ...@@ -17,7 +18,7 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ
17 TypeId.ComptimeFloat => return T(@sqrt(f64, x)), // TODO upgrade to f12818 TypeId.ComptimeFloat => return T(@sqrt(f64, x)), // TODO upgrade to f128
18 TypeId.Float => return @sqrt(T, x),19 TypeId.Float => return @sqrt(T, x),
19 TypeId.ComptimeInt => comptime {20 TypeId.ComptimeInt => comptime {
20 if (x > @maxValue(u128)) {21 if (x > maxInt(u128)) {
21 @compileError("sqrt not implemented for comptime_int greater than 128 bits");22 @compileError("sqrt not implemented for comptime_int greater than 128 bits");
22 }23 }
23 if (x < 0) {24 if (x < 0) {
std/math/tanh.zig+2-1
...@@ -9,6 +9,7 @@ const std = @import("../index.zig");...@@ -9,6 +9,7 @@ const std = @import("../index.zig");
9const math = std.math;9const math = std.math;
10const assert = std.debug.assert;10const assert = std.debug.assert;
11const expo2 = @import("expo2.zig").expo2;11const expo2 = @import("expo2.zig").expo2;
12const maxInt = std.math.maxInt;
1213
13pub fn tanh(x: var) @typeOf(x) {14pub fn tanh(x: var) @typeOf(x) {
14 const T = @typeOf(x);15 const T = @typeOf(x);
...@@ -69,7 +70,7 @@ fn tanh32(x: f32) f32 {...@@ -69,7 +70,7 @@ fn tanh32(x: f32) f32 {
69fn tanh64(x: f64) f64 {70fn tanh64(x: f64) f64 {
70 const u = @bitCast(u64, x);71 const u = @bitCast(u64, x);
71 const w = @intCast(u32, u >> 32);72 const w = @intCast(u32, u >> 32);
72 const ax = @bitCast(f64, u & (@maxValue(u64) >> 1));73 const ax = @bitCast(f64, u & (maxInt(u64) >> 1));
7374
74 var t: f64 = undefined;75 var t: f64 = undefined;
7576
std/math/trunc.zig+3-2
...@@ -7,6 +7,7 @@...@@ -7,6 +7,7 @@
7const std = @import("../index.zig");7const std = @import("../index.zig");
8const math = std.math;8const math = std.math;
9const assert = std.debug.assert;9const assert = std.debug.assert;
10const maxInt = std.math.maxInt;
1011
11pub fn trunc(x: var) @typeOf(x) {12pub fn trunc(x: var) @typeOf(x) {
12 const T = @typeOf(x);13 const T = @typeOf(x);
...@@ -29,7 +30,7 @@ fn trunc32(x: f32) f32 {...@@ -29,7 +30,7 @@ fn trunc32(x: f32) f32 {
29 e = 1;30 e = 1;
30 }31 }
3132
32 m = u32(@maxValue(u32)) >> @intCast(u5, e);33 m = u32(maxInt(u32)) >> @intCast(u5, e);
33 if (u & m == 0) {34 if (u & m == 0) {
34 return x;35 return x;
35 } else {36 } else {
...@@ -50,7 +51,7 @@ fn trunc64(x: f64) f64 {...@@ -50,7 +51,7 @@ fn trunc64(x: f64) f64 {
50 e = 1;51 e = 1;
51 }52 }
5253
53 m = u64(@maxValue(u64)) >> @intCast(u6, e);54 m = u64(maxInt(u64)) >> @intCast(u6, e);
54 if (u & m == 0) {55 if (u & m == 0) {
55 return x;56 return x;
56 } else {57 } else {
std/os/child_process.zig+5-4
...@@ -14,6 +14,7 @@ const builtin = @import("builtin");...@@ -14,6 +14,7 @@ const builtin = @import("builtin");
14const Os = builtin.Os;14const Os = builtin.Os;
15const LinkedList = std.LinkedList;15const LinkedList = std.LinkedList;
16const windows_util = @import("windows/util.zig");16const windows_util = @import("windows/util.zig");
17const maxInt = std.math.maxInt;
1718
18const is_windows = builtin.os == Os.windows;19const is_windows = builtin.os == Os.windows;
1920
...@@ -307,16 +308,16 @@ pub const ChildProcess = struct.{...@@ -307,16 +308,16 @@ pub const ChildProcess = struct.{
307 os.close(self.err_pipe[1]);308 os.close(self.err_pipe[1]);
308 }309 }
309310
310 // Write @maxValue(ErrInt) to the write end of the err_pipe. This is after311 // Write maxInt(ErrInt) to the write end of the err_pipe. This is after
311 // waitpid, so this write is guaranteed to be after the child312 // waitpid, so this write is guaranteed to be after the child
312 // pid potentially wrote an error. This way we can do a blocking313 // pid potentially wrote an error. This way we can do a blocking
313 // read on the error pipe and either get @maxValue(ErrInt) (no error) or314 // read on the error pipe and either get maxInt(ErrInt) (no error) or
314 // an error code.315 // an error code.
315 try writeIntFd(self.err_pipe[1], @maxValue(ErrInt));316 try writeIntFd(self.err_pipe[1], maxInt(ErrInt));
316 const err_int = try readIntFd(self.err_pipe[0]);317 const err_int = try readIntFd(self.err_pipe[0]);
317 // Here we potentially return the fork child's error318 // Here we potentially return the fork child's error
318 // from the parent pid.319 // from the parent pid.
319 if (err_int != @maxValue(ErrInt)) {320 if (err_int != maxInt(ErrInt)) {
320 return @errSetCast(SpawnError, @intToError(err_int));321 return @errSetCast(SpawnError, @intToError(err_int));
321 }322 }
322323
std/os/darwin.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const c = std.c;2const c = std.c;
3const assert = std.debug.assert;3const assert = std.debug.assert;
4const maxInt = std.math.maxInt;
45
5pub use @import("darwin/errno.zig");6pub use @import("darwin/errno.zig");
67
...@@ -45,7 +46,7 @@ pub const MAP_NOCACHE = 0x0400;...@@ -45,7 +46,7 @@ pub const MAP_NOCACHE = 0x0400;
4546
46/// don't reserve needed swap area47/// don't reserve needed swap area
47pub const MAP_NORESERVE = 0x0040;48pub const MAP_NORESERVE = 0x0040;
48pub const MAP_FAILED = @maxValue(usize);49pub const MAP_FAILED = maxInt(usize);
4950
50/// [XSI] no hang in wait/no child to reap51/// [XSI] no hang in wait/no child to reap
51pub const WNOHANG = 0x00000001;52pub const WNOHANG = 0x00000001;
std/os/file.zig+2-1
...@@ -9,6 +9,7 @@ const posix = os.posix;...@@ -9,6 +9,7 @@ const posix = os.posix;
9const windows = os.windows;9const windows = os.windows;
10const Os = builtin.Os;10const Os = builtin.Os;
11const windows_util = @import("windows/util.zig");11const windows_util = @import("windows/util.zig");
12const maxInt = std.math.maxInt;
1213
13const is_posix = builtin.os != builtin.Os.windows;14const is_posix = builtin.os != builtin.Os.windows;
14const is_windows = builtin.os == builtin.Os.windows;15const is_windows = builtin.os == builtin.Os.windows;
...@@ -385,7 +386,7 @@ pub const File = struct.{...@@ -385,7 +386,7 @@ pub const File = struct.{
385 } else if (is_windows) {386 } else if (is_windows) {
386 var index: usize = 0;387 var index: usize = 0;
387 while (index < buffer.len) {388 while (index < buffer.len) {
388 const want_read_count = @intCast(windows.DWORD, math.min(windows.DWORD(@maxValue(windows.DWORD)), buffer.len - index));389 const want_read_count = @intCast(windows.DWORD, math.min(windows.DWORD(maxInt(windows.DWORD)), buffer.len - index));
389 var amt_read: windows.DWORD = undefined;390 var amt_read: windows.DWORD = undefined;
390 if (windows.ReadFile(self.handle, buffer.ptr + index, want_read_count, &amt_read, null) == 0) {391 if (windows.ReadFile(self.handle, buffer.ptr + index, want_read_count, &amt_read, null) == 0) {
391 const err = windows.GetLastError();392 const err = windows.GetLastError();
std/os/linux/index.zig+4-3
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("../../index.zig");1const std = @import("../../index.zig");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const builtin = @import("builtin");3const builtin = @import("builtin");
4const maxInt = std.math.maxInt;
4const vdso = @import("vdso.zig");5const vdso = @import("vdso.zig");
5pub use switch (builtin.arch) {6pub use switch (builtin.arch) {
6 builtin.Arch.x86_64 => @import("x86_64.zig"),7 builtin.Arch.x86_64 => @import("x86_64.zig"),
...@@ -38,7 +39,7 @@ pub const PROT_EXEC = 4;...@@ -38,7 +39,7 @@ pub const PROT_EXEC = 4;
38pub const PROT_GROWSDOWN = 0x01000000;39pub const PROT_GROWSDOWN = 0x01000000;
39pub const PROT_GROWSUP = 0x02000000;40pub const PROT_GROWSUP = 0x02000000;
4041
41pub const MAP_FAILED = @maxValue(usize);42pub const MAP_FAILED = maxInt(usize);
42pub const MAP_SHARED = 0x01;43pub const MAP_SHARED = 0x01;
43pub const MAP_PRIVATE = 0x02;44pub const MAP_PRIVATE = 0x02;
44pub const MAP_TYPE = 0x0f;45pub const MAP_TYPE = 0x0f;
...@@ -1094,7 +1095,7 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti...@@ -1094,7 +1095,7 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti
10941095
1095const NSIG = 65;1096const NSIG = 65;
1096const sigset_t = [128 / @sizeOf(usize)]usize;1097const sigset_t = [128 / @sizeOf(usize)]usize;
1097const all_mask = []usize.{@maxValue(usize)};1098const all_mask = []usize.{maxInt(usize)};
1098const app_mask = []usize.{0xfffffffc7fffffff};1099const app_mask = []usize.{0xfffffffc7fffffff};
10991100
1100const k_sigaction = extern struct.{1101const k_sigaction = extern struct.{
...@@ -1111,7 +1112,7 @@ pub const Sigaction = struct.{...@@ -1111,7 +1112,7 @@ pub const Sigaction = struct.{
1111 flags: u32,1112 flags: u32,
1112};1113};
11131114
1114pub const SIG_ERR = @intToPtr(extern fn (i32) void, @maxValue(usize));1115pub const SIG_ERR = @intToPtr(extern fn (i32) void, maxInt(usize));
1115pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);1116pub const SIG_DFL = @intToPtr(extern fn (i32) void, 0);
1116pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);1117pub const SIG_IGN = @intToPtr(extern fn (i32) void, 1);
1117pub const empty_sigset = []usize.{0} ** sigset_t.len;1118pub const empty_sigset = []usize.{0} ** sigset_t.len;
std/os/linux/vdso.zig+3-2
...@@ -3,6 +3,7 @@ const elf = std.elf;...@@ -3,6 +3,7 @@ const elf = std.elf;
3const linux = std.os.linux;3const linux = std.os.linux;
4const cstr = std.cstr;4const cstr = std.cstr;
5const mem = std.mem;5const mem = std.mem;
6const maxInt = std.math.maxInt;
67
7pub fn lookup(vername: []const u8, name: []const u8) usize {8pub fn lookup(vername: []const u8, name: []const u8) usize {
8 const vdso_addr = std.os.linuxGetAuxVal(std.elf.AT_SYSINFO_EHDR);9 const vdso_addr = std.os.linuxGetAuxVal(std.elf.AT_SYSINFO_EHDR);
...@@ -13,7 +14,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {...@@ -13,7 +14,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
13 const ph = @intToPtr(*elf.Phdr, ph_addr);14 const ph = @intToPtr(*elf.Phdr, ph_addr);
1415
15 var maybe_dynv: ?[*]usize = null;16 var maybe_dynv: ?[*]usize = null;
16 var base: usize = @maxValue(usize);17 var base: usize = maxInt(usize);
17 {18 {
18 var i: usize = 0;19 var i: usize = 0;
19 while (i < eh.e_phnum) : ({20 while (i < eh.e_phnum) : ({
...@@ -29,7 +30,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {...@@ -29,7 +30,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
29 }30 }
30 }31 }
31 const dynv = maybe_dynv orelse return 0;32 const dynv = maybe_dynv orelse return 0;
32 if (base == @maxValue(usize)) return 0;33 if (base == maxInt(usize)) return 0;
3334
34 var maybe_strings: ?[*]u8 = null;35 var maybe_strings: ?[*]u8 = null;
35 var maybe_syms: ?[*]elf.Sym = null;36 var maybe_syms: ?[*]elf.Sym = null;
std/os/windows/index.zig+6-5
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("../../index.zig");1const std = @import("../../index.zig");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const maxInt = std.math.maxInt;
34
4pub use @import("advapi32.zig");5pub use @import("advapi32.zig");
5pub use @import("kernel32.zig");6pub use @import("kernel32.zig");
...@@ -53,17 +54,17 @@ pub const TRUE = 1;...@@ -53,17 +54,17 @@ pub const TRUE = 1;
53pub const FALSE = 0;54pub const FALSE = 0;
5455
55/// The standard input device. Initially, this is the console input buffer, CONIN$.56/// The standard input device. Initially, this is the console input buffer, CONIN$.
56pub const STD_INPUT_HANDLE = @maxValue(DWORD) - 10 + 1;57pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1;
5758
58/// The standard output device. Initially, this is the active console screen buffer, CONOUT$.59/// The standard output device. Initially, this is the active console screen buffer, CONOUT$.
59pub const STD_OUTPUT_HANDLE = @maxValue(DWORD) - 11 + 1;60pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1;
6061
61/// The standard error device. Initially, this is the active console screen buffer, CONOUT$.62/// The standard error device. Initially, this is the active console screen buffer, CONOUT$.
62pub const STD_ERROR_HANDLE = @maxValue(DWORD) - 12 + 1;63pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1;
6364
64pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, @maxValue(usize));65pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize));
6566
66pub const INVALID_FILE_ATTRIBUTES = DWORD(@maxValue(DWORD));67pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD));
6768
68pub const OVERLAPPED = extern struct.{69pub const OVERLAPPED = extern struct.{
69 Internal: ULONG_PTR,70 Internal: ULONG_PTR,
std/rand/index.zig+6-5
...@@ -20,6 +20,7 @@ const assert = std.debug.assert;...@@ -20,6 +20,7 @@ const assert = std.debug.assert;
20const mem = std.mem;20const mem = std.mem;
21const math = std.math;21const math = std.math;
22const ziggurat = @import("ziggurat.zig");22const ziggurat = @import("ziggurat.zig");
23const maxInt = std.math.maxInt;
2324
24// When you need fast unbiased random numbers25// When you need fast unbiased random numbers
25pub const DefaultPrng = Xoroshiro128;26pub const DefaultPrng = Xoroshiro128;
...@@ -39,7 +40,7 @@ pub const Random = struct.{...@@ -39,7 +40,7 @@ pub const Random = struct.{
39 return r.int(u1) != 0;40 return r.int(u1) != 0;
40 }41 }
4142
42 /// Returns a random int `i` such that `0 <= i <= @maxValue(T)`.43 /// Returns a random int `i` such that `0 <= i <= maxInt(T)`.
43 /// `i` is evenly distributed.44 /// `i` is evenly distributed.
44 pub fn int(r: *Random, comptime T: type) T {45 pub fn int(r: *Random, comptime T: type) T {
45 const UnsignedT = @IntType(false, T.bit_count);46 const UnsignedT = @IntType(false, T.bit_count);
...@@ -69,14 +70,14 @@ pub const Random = struct.{...@@ -69,14 +70,14 @@ pub const Random = struct.{
69 assert(T.is_signed == false);70 assert(T.is_signed == false);
70 assert(0 < less_than);71 assert(0 < less_than);
7172
72 const last_group_size_minus_one: T = @maxValue(T) % less_than;73 const last_group_size_minus_one: T = maxInt(T) % less_than;
73 if (last_group_size_minus_one == less_than - 1) {74 if (last_group_size_minus_one == less_than - 1) {
74 // less_than is a power of two.75 // less_than is a power of two.
75 assert(math.floorPowerOfTwo(T, less_than) == less_than);76 assert(math.floorPowerOfTwo(T, less_than) == less_than);
76 // There is no retry zone. The optimal retry_zone_start would be @maxValue(T) + 1.77 // There is no retry zone. The optimal retry_zone_start would be maxInt(T) + 1.
77 return r.int(T) % less_than;78 return r.int(T) % less_than;
78 }79 }
79 const retry_zone_start = @maxValue(T) - last_group_size_minus_one;80 const retry_zone_start = maxInt(T) - last_group_size_minus_one;
8081
81 while (true) {82 while (true) {
82 const rand_val = r.int(T);83 const rand_val = r.int(T);
...@@ -91,7 +92,7 @@ pub const Random = struct.{...@@ -91,7 +92,7 @@ pub const Random = struct.{
91 /// for commentary on the runtime of this function.92 /// for commentary on the runtime of this function.
92 pub fn uintAtMost(r: *Random, comptime T: type, at_most: T) T {93 pub fn uintAtMost(r: *Random, comptime T: type, at_most: T) T {
93 assert(T.is_signed == false);94 assert(T.is_signed == false);
94 if (at_most == @maxValue(T)) {95 if (at_most == maxInt(T)) {
95 // have the full range96 // have the full range
96 return r.int(T);97 return r.int(T);
97 }98 }
std/special/builtin.zig+6-4
...@@ -1,14 +1,16 @@...@@ -1,14 +1,16 @@
1// These functions are provided when not linking against libc because LLVM1// These functions are provided when not linking against libc because LLVM
2// sometimes generates code that calls them.2// sometimes generates code that calls them.
33
4const std = @import("std");
4const builtin = @import("builtin");5const builtin = @import("builtin");
6const maxInt = std.math.maxInt;
57
6// Avoid dragging in the runtime safety mechanisms into this .o file,8// Avoid dragging in the runtime safety mechanisms into this .o file,
7// unless we're trying to test this file.9// unless we're trying to test this file.
8pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {10pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
9 if (builtin.is_test) {11 if (builtin.is_test) {
10 @setCold(true);12 @setCold(true);
11 @import("std").debug.panic("{}", msg);13 std.debug.panic("{}", msg);
12 } else {14 } else {
13 unreachable;15 unreachable;
14 }16 }
...@@ -190,7 +192,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {...@@ -190,7 +192,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
190 }) {}192 }) {}
191 ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1));193 ux <<= @intCast(log2uint, @bitCast(u32, -ex + 1));
192 } else {194 } else {
193 ux &= @maxValue(uint) >> exp_bits;195 ux &= maxInt(uint) >> exp_bits;
194 ux |= 1 << digits;196 ux |= 1 << digits;
195 }197 }
196 if (ey == 0) {198 if (ey == 0) {
...@@ -201,7 +203,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {...@@ -201,7 +203,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
201 }) {}203 }) {}
202 uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1));204 uy <<= @intCast(log2uint, @bitCast(u32, -ey + 1));
203 } else {205 } else {
204 uy &= @maxValue(uint) >> exp_bits;206 uy &= maxInt(uint) >> exp_bits;
205 uy |= 1 << digits;207 uy |= 1 << digits;
206 }208 }
207209
...@@ -247,7 +249,7 @@ fn isNan(comptime T: type, bits: T) bool {...@@ -247,7 +249,7 @@ fn isNan(comptime T: type, bits: T) bool {
247 } else if (T == u32) {249 } else if (T == u32) {
248 return (bits & 0x7fffffff) > 0x7f800000;250 return (bits & 0x7fffffff) > 0x7f800000;
249 } else if (T == u64) {251 } else if (T == u64) {
250 return (bits & (@maxValue(u64) >> 1)) > (u64(0x7ff) << 52);252 return (bits & (maxInt(u64) >> 1)) > (u64(0x7ff) << 52);
251 } else {253 } else {
252 unreachable;254 unreachable;
253 }255 }
std/special/compiler_rt/floattidf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const DBL_MANT_DIG = 53;6const DBL_MANT_DIG = 53;
57
...@@ -37,7 +39,7 @@ pub extern fn __floattidf(arg: i128) f64 {...@@ -37,7 +39,7 @@ pub extern fn __floattidf(arg: i128) f64 {
37 const shift2_amt = @intCast(i32, N + (DBL_MANT_DIG + 2)) - sd;39 const shift2_amt = @intCast(i32, N + (DBL_MANT_DIG + 2)) - sd;
38 const shift2_amt_u7 = @intCast(u7, shift2_amt);40 const shift2_amt_u7 = @intCast(u7, shift2_amt);
3941
40 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, @maxValue(u128)) >> shift2_amt_u7)) != 0);42 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, maxInt(u128)) >> shift2_amt_u7)) != 0);
41 },43 },
42 }44 }
43 // finish45 // finish
std/special/compiler_rt/floattisf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const FLT_MANT_DIG = 24;6const FLT_MANT_DIG = 24;
57
...@@ -38,7 +40,7 @@ pub extern fn __floattisf(arg: i128) f32 {...@@ -38,7 +40,7 @@ pub extern fn __floattisf(arg: i128) f32 {
38 const shift2_amt = @intCast(i32, N + (FLT_MANT_DIG + 2)) - sd;40 const shift2_amt = @intCast(i32, N + (FLT_MANT_DIG + 2)) - sd;
39 const shift2_amt_u7 = @intCast(u7, shift2_amt);41 const shift2_amt_u7 = @intCast(u7, shift2_amt);
4042
41 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, @maxValue(u128)) >> shift2_amt_u7)) != 0);43 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, maxInt(u128)) >> shift2_amt_u7)) != 0);
42 },44 },
43 }45 }
44 // finish46 // finish
std/special/compiler_rt/floattitf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const LDBL_MANT_DIG = 113;6const LDBL_MANT_DIG = 113;
57
...@@ -37,7 +39,7 @@ pub extern fn __floattitf(arg: i128) f128 {...@@ -37,7 +39,7 @@ pub extern fn __floattitf(arg: i128) f128 {
37 const shift2_amt = @intCast(i32, N + (LDBL_MANT_DIG + 2)) - sd;39 const shift2_amt = @intCast(i32, N + (LDBL_MANT_DIG + 2)) - sd;
38 const shift2_amt_u7 = @intCast(u7, shift2_amt);40 const shift2_amt_u7 = @intCast(u7, shift2_amt);
3941
40 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, @maxValue(u128)) >> shift2_amt_u7)) != 0);42 a = (a >> shift1_amt_u7) | @boolToInt((a & (@intCast(u128, maxInt(u128)) >> shift2_amt_u7)) != 0);
41 },43 },
42 }44 }
43 // finish45 // finish
std/special/compiler_rt/floatuntidf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const DBL_MANT_DIG = 53;6const DBL_MANT_DIG = 53;
57
...@@ -30,7 +32,7 @@ pub extern fn __floatuntidf(arg: u128) f64 {...@@ -30,7 +32,7 @@ pub extern fn __floatuntidf(arg: u128) f64 {
30 const shift_amt = @bitCast(i32, N + (DBL_MANT_DIG + 2)) - sd;32 const shift_amt = @bitCast(i32, N + (DBL_MANT_DIG + 2)) - sd;
31 const shift_amt_u7 = @intCast(u7, shift_amt);33 const shift_amt_u7 = @intCast(u7, shift_amt);
32 a = (a >> @intCast(u7, sd - (DBL_MANT_DIG + 2))) |34 a = (a >> @intCast(u7, sd - (DBL_MANT_DIG + 2))) |
33 @boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);35 @boolToInt((a & (u128(maxInt(u128)) >> shift_amt_u7)) != 0);
34 },36 },
35 }37 }
36 // finish38 // finish
std/special/compiler_rt/floatuntisf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const FLT_MANT_DIG = 24;6const FLT_MANT_DIG = 24;
57
...@@ -30,7 +32,7 @@ pub extern fn __floatuntisf(arg: u128) f32 {...@@ -30,7 +32,7 @@ pub extern fn __floatuntisf(arg: u128) f32 {
30 const shift_amt = @bitCast(i32, N + (FLT_MANT_DIG + 2)) - sd;32 const shift_amt = @bitCast(i32, N + (FLT_MANT_DIG + 2)) - sd;
31 const shift_amt_u7 = @intCast(u7, shift_amt);33 const shift_amt_u7 = @intCast(u7, shift_amt);
32 a = (a >> @intCast(u7, sd - (FLT_MANT_DIG + 2))) |34 a = (a >> @intCast(u7, sd - (FLT_MANT_DIG + 2))) |
33 @boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);35 @boolToInt((a & (u128(maxInt(u128)) >> shift_amt_u7)) != 0);
34 },36 },
35 }37 }
36 // finish38 // finish
std/special/compiler_rt/floatuntitf.zig+3-1
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const is_test = builtin.is_test;2const is_test = builtin.is_test;
3const std = @import("std");
4const maxInt = std.math.maxInt;
35
4const LDBL_MANT_DIG = 113;6const LDBL_MANT_DIG = 113;
57
...@@ -30,7 +32,7 @@ pub extern fn __floatuntitf(arg: u128) f128 {...@@ -30,7 +32,7 @@ pub extern fn __floatuntitf(arg: u128) f128 {
30 const shift_amt = @bitCast(i32, N + (LDBL_MANT_DIG + 2)) - sd;32 const shift_amt = @bitCast(i32, N + (LDBL_MANT_DIG + 2)) - sd;
31 const shift_amt_u7 = @intCast(u7, shift_amt);33 const shift_amt_u7 = @intCast(u7, shift_amt);
32 a = (a >> @intCast(u7, sd - (LDBL_MANT_DIG + 2))) |34 a = (a >> @intCast(u7, sd - (LDBL_MANT_DIG + 2))) |
33 @boolToInt((a & (u128(@maxValue(u128)) >> shift_amt_u7)) != 0);35 @boolToInt((a & (u128(maxInt(u128)) >> shift_amt_u7)) != 0);
34 },36 },
35 }37 }
36 // finish38 // finish
std/zig/parser_test.zig+2-1
...@@ -1868,6 +1868,7 @@ const std = @import("std");...@@ -1868,6 +1868,7 @@ const std = @import("std");
1868const mem = std.mem;1868const mem = std.mem;
1869const warn = std.debug.warn;1869const warn = std.debug.warn;
1870const io = std.io;1870const io = std.io;
1871const maxInt = std.math.maxInt;
18711872
1872var fixed_buffer_mem: [100 * 1024]u8 = undefined;1873var fixed_buffer_mem: [100 * 1024]u8 = undefined;
18731874
...@@ -1916,7 +1917,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {...@@ -1916,7 +1917,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
1916 const needed_alloc_count = x: {1917 const needed_alloc_count = x: {
1917 // Try it once with unlimited memory, make sure it works1918 // Try it once with unlimited memory, make sure it works
1918 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);1919 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
1919 var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize));1920 var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
1920 var anything_changed: bool = undefined;1921 var anything_changed: bool = undefined;
1921 const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed);1922 const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed);
1922 if (!mem.eql(u8, result_source, expected_source)) {1923 if (!mem.eql(u8, result_source, expected_source)) {
test/cases/alignof.zig+4-2
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const assert = @import("std").debug.assert;1const std = @import("std");
2const assert = std.debug.assert;
2const builtin = @import("builtin");3const builtin = @import("builtin");
4const maxInt = std.math.maxInt;
35
4const Foo = struct.{6const Foo = struct.{
5 x: u32,7 x: u32,
...@@ -8,7 +10,7 @@ const Foo = struct.{...@@ -8,7 +10,7 @@ const Foo = struct.{
8};10};
911
10test "@alignOf(T) before referencing T" {12test "@alignOf(T) before referencing T" {
11 comptime assert(@alignOf(Foo) != @maxValue(usize));13 comptime assert(@alignOf(Foo) != maxInt(usize));
12 if (builtin.arch == builtin.Arch.x86_64) {14 if (builtin.arch == builtin.Arch.x86_64) {
13 comptime assert(@alignOf(Foo) == 4);15 comptime assert(@alignOf(Foo) == 4);
14 }16 }
test/cases/bitcast.zig+5-3
...@@ -1,4 +1,6 @@...@@ -1,4 +1,6 @@
1const assert = @import("std").debug.assert;1const std = @import("std");
2const assert = std.debug.assert;
3const maxInt = std.math.maxInt;
24
3test "@bitCast i32 -> u32" {5test "@bitCast i32 -> u32" {
4 testBitCast_i32_u32();6 testBitCast_i32_u32();
...@@ -6,8 +8,8 @@ test "@bitCast i32 -> u32" {...@@ -6,8 +8,8 @@ test "@bitCast i32 -> u32" {
6}8}
79
8fn testBitCast_i32_u32() void {10fn testBitCast_i32_u32() void {
9 assert(conv(-1) == @maxValue(u32));11 assert(conv(-1) == maxInt(u32));
10 assert(conv2(@maxValue(u32)) == -1);12 assert(conv2(maxInt(u32)) == -1);
11}13}
1214
13fn conv(x: i32) u32 {15fn conv(x: i32) u32 {
test/cases/cast.zig+4-3
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const mem = std.mem;3const mem = std.mem;
4const maxInt = std.math.maxInt;
45
5test "int to ptr cast" {6test "int to ptr cast" {
6 const x = usize(13);7 const x = usize(13);
...@@ -368,7 +369,7 @@ test "@bytesToSlice keeps pointer alignment" {...@@ -368,7 +369,7 @@ test "@bytesToSlice keeps pointer alignment" {
368}369}
369370
370test "@intCast i32 to u7" {371test "@intCast i32 to u7" {
371 var x: u128 = @maxValue(u128);372 var x: u128 = maxInt(u128);
372 var y: i32 = 120;373 var y: i32 = 120;
373 var z = x >> @intCast(u7, y);374 var z = x >> @intCast(u7, y);
374 assert(z == 0xff);375 assert(z == 0xff);
...@@ -435,11 +436,11 @@ test "compile time int to ptr of function" {...@@ -435,11 +436,11 @@ test "compile time int to ptr of function" {
435 foobar(FUNCTION_CONSTANT);436 foobar(FUNCTION_CONSTANT);
436}437}
437438
438pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, @maxValue(usize));439pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize));
439pub const PFN_void = extern fn (*c_void) void;440pub const PFN_void = extern fn (*c_void) void;
440441
441fn foobar(func: PFN_void) void {442fn foobar(func: PFN_void) void {
442 std.debug.assert(@ptrToInt(func) == @maxValue(usize));443 std.debug.assert(@ptrToInt(func) == maxInt(usize));
443}444}
444445
445test "implicit ptr to *c_void" {446test "implicit ptr to *c_void" {
test/cases/math.zig+17-14
...@@ -1,4 +1,7 @@...@@ -1,4 +1,7 @@
1const assert = @import("std").debug.assert;1const std = @import("std");
2const assert = std.debug.assert;
3const maxInt = std.math.maxInt;
4const minInt = std.math.minInt;
25
3test "division" {6test "division" {
4 testDivision();7 testDivision();
...@@ -179,30 +182,30 @@ test "const number literal" {...@@ -179,30 +182,30 @@ test "const number literal" {
179const ten = 10;182const ten = 10;
180183
181test "unsigned wrapping" {184test "unsigned wrapping" {
182 testUnsignedWrappingEval(@maxValue(u32));185 testUnsignedWrappingEval(maxInt(u32));
183 comptime testUnsignedWrappingEval(@maxValue(u32));186 comptime testUnsignedWrappingEval(maxInt(u32));
184}187}
185fn testUnsignedWrappingEval(x: u32) void {188fn testUnsignedWrappingEval(x: u32) void {
186 const zero = x +% 1;189 const zero = x +% 1;
187 assert(zero == 0);190 assert(zero == 0);
188 const orig = zero -% 1;191 const orig = zero -% 1;
189 assert(orig == @maxValue(u32));192 assert(orig == maxInt(u32));
190}193}
191194
192test "signed wrapping" {195test "signed wrapping" {
193 testSignedWrappingEval(@maxValue(i32));196 testSignedWrappingEval(maxInt(i32));
194 comptime testSignedWrappingEval(@maxValue(i32));197 comptime testSignedWrappingEval(maxInt(i32));
195}198}
196fn testSignedWrappingEval(x: i32) void {199fn testSignedWrappingEval(x: i32) void {
197 const min_val = x +% 1;200 const min_val = x +% 1;
198 assert(min_val == @minValue(i32));201 assert(min_val == minInt(i32));
199 const max_val = min_val -% 1;202 const max_val = min_val -% 1;
200 assert(max_val == @maxValue(i32));203 assert(max_val == maxInt(i32));
201}204}
202205
203test "negation wrapping" {206test "negation wrapping" {
204 testNegationWrappingEval(@minValue(i16));207 testNegationWrappingEval(minInt(i16));
205 comptime testNegationWrappingEval(@minValue(i16));208 comptime testNegationWrappingEval(minInt(i16));
206}209}
207fn testNegationWrappingEval(x: i16) void {210fn testNegationWrappingEval(x: i16) void {
208 assert(x == -32768);211 assert(x == -32768);
...@@ -311,8 +314,8 @@ test "hex float literal within range" {...@@ -311,8 +314,8 @@ test "hex float literal within range" {
311}314}
312315
313test "truncating shift left" {316test "truncating shift left" {
314 testShlTrunc(@maxValue(u16));317 testShlTrunc(maxInt(u16));
315 comptime testShlTrunc(@maxValue(u16));318 comptime testShlTrunc(maxInt(u16));
316}319}
317fn testShlTrunc(x: u16) void {320fn testShlTrunc(x: u16) void {
318 const shifted = x << 1;321 const shifted = x << 1;
...@@ -320,8 +323,8 @@ fn testShlTrunc(x: u16) void {...@@ -320,8 +323,8 @@ fn testShlTrunc(x: u16) void {
320}323}
321324
322test "truncating shift right" {325test "truncating shift right" {
323 testShrTrunc(@maxValue(u16));326 testShrTrunc(maxInt(u16));
324 comptime testShrTrunc(@maxValue(u16));327 comptime testShrTrunc(maxInt(u16));
325}328}
326fn testShrTrunc(x: u16) void {329fn testShrTrunc(x: u16) void {
327 const shifted = x >> 1;330 const shifted = x >> 1;
test/cases/misc.zig+9-44
...@@ -1,7 +1,9 @@...@@ -1,7 +1,9 @@
1const assert = @import("std").debug.assert;1const std = @import("std");
2const mem = @import("std").mem;2const assert = std.debug.assert;
3const cstr = @import("std").cstr;3const mem = std.mem;
4const cstr = std.cstr;
4const builtin = @import("builtin");5const builtin = @import("builtin");
6const maxInt = std.math.maxInt;
57
6// normal comment8// normal comment
79
...@@ -58,43 +60,6 @@ test "floating point primitive bit counts" {...@@ -58,43 +60,6 @@ test "floating point primitive bit counts" {
58 assert(f64.bit_count == 64);60 assert(f64.bit_count == 64);
59}61}
6062
61test "@minValue and @maxValue" {
62 assert(@maxValue(u1) == 1);
63 assert(@maxValue(u8) == 255);
64 assert(@maxValue(u16) == 65535);
65 assert(@maxValue(u32) == 4294967295);
66 assert(@maxValue(u64) == 18446744073709551615);
67
68 assert(@maxValue(i1) == 0);
69 assert(@maxValue(i8) == 127);
70 assert(@maxValue(i16) == 32767);
71 assert(@maxValue(i32) == 2147483647);
72 assert(@maxValue(i63) == 4611686018427387903);
73 assert(@maxValue(i64) == 9223372036854775807);
74
75 assert(@minValue(u1) == 0);
76 assert(@minValue(u8) == 0);
77 assert(@minValue(u16) == 0);
78 assert(@minValue(u32) == 0);
79 assert(@minValue(u63) == 0);
80 assert(@minValue(u64) == 0);
81
82 assert(@minValue(i1) == -1);
83 assert(@minValue(i8) == -128);
84 assert(@minValue(i16) == -32768);
85 assert(@minValue(i32) == -2147483648);
86 assert(@minValue(i63) == -4611686018427387904);
87 assert(@minValue(i64) == -9223372036854775808);
88}
89
90test "max value type" {
91 // If the type of @maxValue(i32) was i32 then this implicit cast to
92 // u32 would not work. But since the value is a number literal,
93 // it works fine.
94 const x: u32 = @maxValue(i32);
95 assert(x == 2147483647);
96}
97
98test "short circuit" {63test "short circuit" {
99 testShortCircuit(false, true);64 testShortCircuit(false, true);
100 comptime testShortCircuit(false, true);65 comptime testShortCircuit(false, true);
...@@ -428,10 +393,10 @@ test "cast slice to u8 slice" {...@@ -428,10 +393,10 @@ test "cast slice to u8 slice" {
428 const big_thing_again = @bytesToSlice(i32, bytes);393 const big_thing_again = @bytesToSlice(i32, bytes);
429 assert(big_thing_again[2] == 3);394 assert(big_thing_again[2] == 3);
430 big_thing_again[2] = -1;395 big_thing_again[2] = -1;
431 assert(bytes[8] == @maxValue(u8));396 assert(bytes[8] == maxInt(u8));
432 assert(bytes[9] == @maxValue(u8));397 assert(bytes[9] == maxInt(u8));
433 assert(bytes[10] == @maxValue(u8));398 assert(bytes[10] == maxInt(u8));
434 assert(bytes[11] == @maxValue(u8));399 assert(bytes[11] == maxInt(u8));
435}400}
436401
437test "pointer to void return type" {402test "pointer to void return type" {
test/cases/struct.zig+17-15
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const assert = @import("std").debug.assert;1const std = @import("std");
2const assert = std.debug.assert;
2const builtin = @import("builtin");3const builtin = @import("builtin");
4const maxInt = std.math.maxInt;
35
4const StructWithNoFields = struct.{6const StructWithNoFields = struct.{
5 fn add(a: i32, b: i32) i32 {7 fn add(a: i32, b: i32) i32 {
...@@ -307,29 +309,29 @@ test "packed array 24bits" {...@@ -307,29 +309,29 @@ test "packed array 24bits" {
307 assert(ptr.b[1].field == 0);309 assert(ptr.b[1].field == 0);
308 assert(ptr.c == 0);310 assert(ptr.c == 0);
309311
310 ptr.a = @maxValue(u16);312 ptr.a = maxInt(u16);
311 assert(ptr.a == @maxValue(u16));313 assert(ptr.a == maxInt(u16));
312 assert(ptr.b[0].field == 0);314 assert(ptr.b[0].field == 0);
313 assert(ptr.b[1].field == 0);315 assert(ptr.b[1].field == 0);
314 assert(ptr.c == 0);316 assert(ptr.c == 0);
315317
316 ptr.b[0].field = @maxValue(u24);318 ptr.b[0].field = maxInt(u24);
317 assert(ptr.a == @maxValue(u16));319 assert(ptr.a == maxInt(u16));
318 assert(ptr.b[0].field == @maxValue(u24));320 assert(ptr.b[0].field == maxInt(u24));
319 assert(ptr.b[1].field == 0);321 assert(ptr.b[1].field == 0);
320 assert(ptr.c == 0);322 assert(ptr.c == 0);
321323
322 ptr.b[1].field = @maxValue(u24);324 ptr.b[1].field = maxInt(u24);
323 assert(ptr.a == @maxValue(u16));325 assert(ptr.a == maxInt(u16));
324 assert(ptr.b[0].field == @maxValue(u24));326 assert(ptr.b[0].field == maxInt(u24));
325 assert(ptr.b[1].field == @maxValue(u24));327 assert(ptr.b[1].field == maxInt(u24));
326 assert(ptr.c == 0);328 assert(ptr.c == 0);
327329
328 ptr.c = @maxValue(u16);330 ptr.c = maxInt(u16);
329 assert(ptr.a == @maxValue(u16));331 assert(ptr.a == maxInt(u16));
330 assert(ptr.b[0].field == @maxValue(u24));332 assert(ptr.b[0].field == maxInt(u24));
331 assert(ptr.b[1].field == @maxValue(u24));333 assert(ptr.b[1].field == maxInt(u24));
332 assert(ptr.c == @maxValue(u16));334 assert(ptr.c == maxInt(u16));
333335
334 assert(bytes[bytes.len - 1] == 0xaa);336 assert(bytes[bytes.len - 1] == 0xaa);
335}337}
test/compile_errors.zig+3-3
...@@ -535,12 +535,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -535,12 +535,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
535 );535 );
536536
537 cases.add(537 cases.add(
538 "optional pointer to void in extern struct",538 "bit count of @IntType too large",
539 \\comptime {539 \\comptime {
540 \\ _ = @IntType(false, @maxValue(u32) + 1);540 \\ _ = @IntType(false, @import("std").math.maxInt(u32) + 1);
541 \\}541 \\}
542 ,542 ,
543 ".tmp_source.zig:2:40: error: integer value 4294967296 cannot be implicitly casted to type 'u32'",543 ".tmp_source.zig:2:57: error: integer value 4294967296 cannot be implicitly casted to type 'u32'",
544 );544 );
545545
546 cases.add(546 cases.add(
test/standalone/brace_expansion/main.zig+2-1
...@@ -5,6 +5,7 @@ const debug = std.debug;...@@ -5,6 +5,7 @@ const debug = std.debug;
5const assert = debug.assert;5const assert = debug.assert;
6const Buffer = std.Buffer;6const Buffer = std.Buffer;
7const ArrayList = std.ArrayList;7const ArrayList = std.ArrayList;
8const maxInt = std.math.maxInt;
89
9const Token = union(enum).{10const Token = union(enum).{
10 Word: []const u8,11 Word: []const u8,
...@@ -192,7 +193,7 @@ pub fn main() !void {...@@ -192,7 +193,7 @@ pub fn main() !void {
192 defer stdin_buf.deinit();193 defer stdin_buf.deinit();
193194
194 var stdin_adapter = stdin_file.inStream();195 var stdin_adapter = stdin_file.inStream();
195 try stdin_adapter.stream.readAllBuffer(&stdin_buf, @maxValue(usize));196 try stdin_adapter.stream.readAllBuffer(&stdin_buf, maxInt(usize));
196197
197 var result_buf = try Buffer.initSize(global_allocator, 0);198 var result_buf = try Buffer.initSize(global_allocator, 0);
198 defer result_buf.deinit();199 defer result_buf.deinit();