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