-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
remove builtin @minValue and @maxValue and replace with stdlib implementation; #1476
Conversation
std/math/index.zig
Outdated
i64 => -9223372036854775808, | ||
i128 => -170141183460469231731687303715884105728, | ||
else => { | ||
//Calculate for Integers that we do not have cached |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there is any reason for this explicit caching since the compiler already does this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "the compiler already does this" mean here? we are in comptime
, so obviously we are not caching at runtime, but I thought that even at comptime
, having a switch
for common types could speed-up compile times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, each instantiation of min/maxInt
only gets its body comptime evaluated once and then cached. This is how types such as ArrayList(u8)
is the same type each time it is called. Isn't the point of comptime
stuff that we can avoid hardcoding our comptime generated tables and such? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std/math/index.zig
Outdated
return switch (@typeId(T)) { | ||
// - (1 << (T.bit_count - 1)) | ||
TypeId.Int, TypeId.ComptimeInt => if (T.is_signed) -(1 << (T.bit_count - 1)) else 0, | ||
else => @compileError("minValue not implemented for " ++ @typeName(T)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this function also find the max value of an enum?
Use case: User made enum arrays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking it should probably just be std.maxInt
and std.minInt
. max/min values of enums can be separate functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will change the function name.
std/math/index.zig
Outdated
@@ -380,7 +456,7 @@ pub fn absInt(x: var) !@typeOf(x) { | |||
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt | |||
comptime assert(T.is_signed); // must pass a signed integer to absInt | |||
|
|||
if (x == @minValue(@typeOf(x))) { | |||
if (x == std.math.minValue(@typeOf(x))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are already in the math
namespace here, so no need for std.math
test/cases/misc.zig
Outdated
assert(@maxValue(u16) == 65535); | ||
assert(@maxValue(u32) == 4294967295); | ||
assert(@maxValue(u64) == 18446744073709551615); | ||
test "std.math.minValue and std.math.maxValue" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably exist in std.math
. I'm pretty sure these tests test language features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so too, but I am glad that everyone agrees -- I will move it over to std.math
test/compile_errors.zig
Outdated
@@ -1,6 +1,24 @@ | |||
const tests = @import("tests.zig"); | |||
|
|||
pub fn addCases(cases: *tests.CompileErrorContext) void { | |||
cases.add( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure this tests compiler errors for compiler features, so these should not be here anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std.math.minValue
and friends emits a @compileError
for using types that are not an Integer -- this was supposed to test that, but where would be the better place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std/math/index.zig
Outdated
}; | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
test "std.math.minInt and std.math.maxInt" { | ||
assert(std.math.maxInt(u1) == 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You missed removing std.math
here when copy-pasting :)
b5e4a9d
to
01f5133
Compare
Removed cached values and rebased. |
Could you rebase all the renames across files into one commit? It helps when going through commit logs where I think its better to see that the commit intent is highlighted rather than the individual files, which we have the diff for anyway. Thanks. |
01f5133
to
ee28327
Compare
@tiehuis collapsed into 640105a |
std/math/index.zig
Outdated
@@ -246,30 +246,10 @@ test "math.max" { | |||
/// The result is a compile time constant. | |||
pub fn minInt(comptime T: type) @typeOf(42) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@typeOf(42)
can just be comptime_int
now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
be25229
to
b40ebc4
Compare
Going to rebase this after 0.3.0 lands. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Squash commits please
… or max value for a non-integer;
b40ebc4
to
5302839
Compare
fixes #1466
@minValue
and@maxValue
from stage1@minValue
and@maxValue
withstd.math.minInt
andstd.math.maxInt