| ... | @@ -113,8 +113,8 @@ pub const Random = struct { | ... | @@ -113,8 +113,8 @@ pub const Random = struct { |
| 113 | // TODO: endian portability is pointless if the underlying prng isn't endian portable. | 113 | // TODO: endian portability is pointless if the underlying prng isn't endian portable. |
| 114 | // TODO: document the endian portability of this library. | 114 | // TODO: document the endian portability of this library. |
| 115 | const byte_aligned_result = mem.readIntSliceLittle(ByteAlignedT, &rand_bytes); | 115 | const byte_aligned_result = mem.readIntSliceLittle(ByteAlignedT, &rand_bytes); |
| 116 | const unsigned_result = @as(UnsignedT, @truncate(byte_aligned_result)); | 116 | const unsigned_result: UnsignedT = @truncate(byte_aligned_result); |
| 117 | return @as(T, @bitCast(unsigned_result)); | 117 | return @bitCast(unsigned_result); |
| 118 | } | 118 | } |
| 119 | | 119 | |
| 120 | /// Constant-time implementation off `uintLessThan`. | 120 | /// Constant-time implementation off `uintLessThan`. |
| ... | @@ -193,10 +193,10 @@ pub const Random = struct { | ... | @@ -193,10 +193,10 @@ pub const Random = struct { |
| 193 | if (info.signedness == .signed) { | 193 | if (info.signedness == .signed) { |
| 194 | // Two's complement makes this math pretty easy. | 194 | // Two's complement makes this math pretty easy. |
| 195 | const UnsignedT = std.meta.Int(.unsigned, info.bits); | 195 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 196 | const lo = @as(UnsignedT, @bitCast(at_least)); | 196 | const lo: UnsignedT = @bitCast(at_least); |
| 197 | const hi = @as(UnsignedT, @bitCast(less_than)); | 197 | const hi: UnsignedT = @bitCast(less_than); |
| 198 | const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo); | 198 | const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo); |
| 199 | return @as(T, @bitCast(result)); | 199 | return @bitCast(result); |
| 200 | } else { | 200 | } else { |
| 201 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | 201 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. |
| 202 | return at_least + r.uintLessThanBiased(T, less_than - at_least); | 202 | return at_least + r.uintLessThanBiased(T, less_than - at_least); |
| ... | @@ -212,10 +212,10 @@ pub const Random = struct { | ... | @@ -212,10 +212,10 @@ pub const Random = struct { |
| 212 | if (info.signedness == .signed) { | 212 | if (info.signedness == .signed) { |
| 213 | // Two's complement makes this math pretty easy. | 213 | // Two's complement makes this math pretty easy. |
| 214 | const UnsignedT = std.meta.Int(.unsigned, info.bits); | 214 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 215 | const lo = @as(UnsignedT, @bitCast(at_least)); | 215 | const lo: UnsignedT = @bitCast(at_least); |
| 216 | const hi = @as(UnsignedT, @bitCast(less_than)); | 216 | const hi: UnsignedT = @bitCast(less_than); |
| 217 | const result = lo +% r.uintLessThan(UnsignedT, hi -% lo); | 217 | const result = lo +% r.uintLessThan(UnsignedT, hi -% lo); |
| 218 | return @as(T, @bitCast(result)); | 218 | return @bitCast(result); |
| 219 | } else { | 219 | } else { |
| 220 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | 220 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. |
| 221 | return at_least + r.uintLessThan(T, less_than - at_least); | 221 | return at_least + r.uintLessThan(T, less_than - at_least); |
| ... | @@ -230,10 +230,10 @@ pub const Random = struct { | ... | @@ -230,10 +230,10 @@ pub const Random = struct { |
| 230 | if (info.signedness == .signed) { | 230 | if (info.signedness == .signed) { |
| 231 | // Two's complement makes this math pretty easy. | 231 | // Two's complement makes this math pretty easy. |
| 232 | const UnsignedT = std.meta.Int(.unsigned, info.bits); | 232 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 233 | const lo = @as(UnsignedT, @bitCast(at_least)); | 233 | const lo: UnsignedT = @bitCast(at_least); |
| 234 | const hi = @as(UnsignedT, @bitCast(at_most)); | 234 | const hi: UnsignedT = @bitCast(at_most); |
| 235 | const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo); | 235 | const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo); |
| 236 | return @as(T, @bitCast(result)); | 236 | return @bitCast(result); |
| 237 | } else { | 237 | } else { |
| 238 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | 238 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. |
| 239 | return at_least + r.uintAtMostBiased(T, at_most - at_least); | 239 | return at_least + r.uintAtMostBiased(T, at_most - at_least); |
| ... | @@ -249,10 +249,10 @@ pub const Random = struct { | ... | @@ -249,10 +249,10 @@ pub const Random = struct { |
| 249 | if (info.signedness == .signed) { | 249 | if (info.signedness == .signed) { |
| 250 | // Two's complement makes this math pretty easy. | 250 | // Two's complement makes this math pretty easy. |
| 251 | const UnsignedT = std.meta.Int(.unsigned, info.bits); | 251 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 252 | const lo = @as(UnsignedT, @bitCast(at_least)); | 252 | const lo: UnsignedT = @bitCast(at_least); |
| 253 | const hi = @as(UnsignedT, @bitCast(at_most)); | 253 | const hi: UnsignedT = @bitCast(at_most); |
| 254 | const result = lo +% r.uintAtMost(UnsignedT, hi -% lo); | 254 | const result = lo +% r.uintAtMost(UnsignedT, hi -% lo); |
| 255 | return @as(T, @bitCast(result)); | 255 | return @bitCast(result); |
| 256 | } else { | 256 | } else { |
| 257 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. | 257 | // The signed implementation would work fine, but we can use stricter arithmetic operators here. |
| 258 | return at_least + r.uintAtMost(T, at_most - at_least); | 258 | return at_least + r.uintAtMost(T, at_most - at_least); |
| ... | @@ -281,9 +281,9 @@ pub const Random = struct { | ... | @@ -281,9 +281,9 @@ pub const Random = struct { |
| 281 | rand_lz += @clz(r.int(u32) | 0x7FF); | 281 | rand_lz += @clz(r.int(u32) | 0x7FF); |
| 282 | } | 282 | } |
| 283 | } | 283 | } |
| 284 | const mantissa = @as(u23, @truncate(rand)); | 284 | const mantissa: u23 = @truncate(rand); |
| 285 | const exponent = @as(u32, 126 - rand_lz) << 23; | 285 | const exponent = @as(u32, 126 - rand_lz) << 23; |
| 286 | return @as(f32, @bitCast(exponent | mantissa)); | 286 | return @bitCast(exponent | mantissa); |
| 287 | }, | 287 | }, |
| 288 | f64 => { | 288 | f64 => { |
| 289 | // Use 52 random bits for the mantissa, and the rest for the exponent. | 289 | // Use 52 random bits for the mantissa, and the rest for the exponent. |
| ... | @@ -308,7 +308,7 @@ pub const Random = struct { | ... | @@ -308,7 +308,7 @@ pub const Random = struct { |
| 308 | } | 308 | } |
| 309 | const mantissa = rand & 0xFFFFFFFFFFFFF; | 309 | const mantissa = rand & 0xFFFFFFFFFFFFF; |
| 310 | const exponent = (1022 - rand_lz) << 52; | 310 | const exponent = (1022 - rand_lz) << 52; |
| 311 | return @as(f64, @bitCast(exponent | mantissa)); | 311 | return @bitCast(exponent | mantissa); |
| 312 | }, | 312 | }, |
| 313 | else => @compileError("unknown floating point type"), | 313 | else => @compileError("unknown floating point type"), |
| 314 | } | 314 | } |
| ... | @@ -320,7 +320,7 @@ pub const Random = struct { | ... | @@ -320,7 +320,7 @@ pub const Random = struct { |
| 320 | pub fn floatNorm(r: Random, comptime T: type) T { | 320 | pub fn floatNorm(r: Random, comptime T: type) T { |
| 321 | const value = ziggurat.next_f64(r, ziggurat.NormDist); | 321 | const value = ziggurat.next_f64(r, ziggurat.NormDist); |
| 322 | switch (T) { | 322 | switch (T) { |
| 323 | f32 => return @as(f32, @floatCast(value)), | 323 | f32 => return @floatCast(value), |
| 324 | f64 => return value, | 324 | f64 => return value, |
| 325 | else => @compileError("unknown floating point type"), | 325 | else => @compileError("unknown floating point type"), |
| 326 | } | 326 | } |
| ... | @@ -332,7 +332,7 @@ pub const Random = struct { | ... | @@ -332,7 +332,7 @@ pub const Random = struct { |
| 332 | pub fn floatExp(r: Random, comptime T: type) T { | 332 | pub fn floatExp(r: Random, comptime T: type) T { |
| 333 | const value = ziggurat.next_f64(r, ziggurat.ExpDist); | 333 | const value = ziggurat.next_f64(r, ziggurat.ExpDist); |
| 334 | switch (T) { | 334 | switch (T) { |
| 335 | f32 => return @as(f32, @floatCast(value)), | 335 | f32 => return @floatCast(value), |
| 336 | f64 => return value, | 336 | f64 => return value, |
| 337 | else => @compileError("unknown floating point type"), | 337 | else => @compileError("unknown floating point type"), |
| 338 | } | 338 | } |
| ... | @@ -366,10 +366,10 @@ pub const Random = struct { | ... | @@ -366,10 +366,10 @@ pub const Random = struct { |
| 366 | } | 366 | } |
| 367 | | 367 | |
| 368 | // `i <= j < max <= maxInt(MinInt)` | 368 | // `i <= j < max <= maxInt(MinInt)` |
| 369 | const max = @as(MinInt, @intCast(buf.len)); | 369 | const max: MinInt = @intCast(buf.len); |
| 370 | var i: MinInt = 0; | 370 | var i: MinInt = 0; |
| 371 | while (i < max - 1) : (i += 1) { | 371 | while (i < max - 1) : (i += 1) { |
| 372 | const j = @as(MinInt, @intCast(r.intRangeLessThan(Index, i, max))); | 372 | const j: MinInt = @intCast(r.intRangeLessThan(Index, i, max)); |
| 373 | mem.swap(T, &buf[i], &buf[j]); | 373 | mem.swap(T, &buf[i], &buf[j]); |
| 374 | } | 374 | } |
| 375 | } | 375 | } |