authorgravatar for git@e4m2.come4m2 <git@e4m2.com> 2023-08-15 12:09:28+02:00
committergravatar for git@e4m2.come4m2 <git@e4m2.com> 2023-08-15 12:09:28+02:00
log2b4c5d990cc7ec22da037b37e68b5ca48da958de
tree3d9849552b7dd998b1a5cef83f2f84e2b48934ae
parentc0baed4a3e8cf710a90f9909fdf383e3c1c52682

std.rand: Cleanup `@as` builtins


1 files changed, 21 insertions(+), 21 deletions(-)

lib/std/rand.zig+21-21
......@@ -113,8 +113,8 @@ pub const Random = struct {
113113 // TODO: endian portability is pointless if the underlying prng isn't endian portable.
114114 // TODO: document the endian portability of this library.
115115 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);
118118 }
119119
120120 /// Constant-time implementation off `uintLessThan`.
......@@ -193,10 +193,10 @@ pub const Random = struct {
193193 if (info.signedness == .signed) {
194194 // Two's complement makes this math pretty easy.
195195 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);
198198 const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo);
199 return @as(T, @bitCast(result));
199 return @bitCast(result);
200200 } else {
201201 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
202202 return at_least + r.uintLessThanBiased(T, less_than - at_least);
......@@ -212,10 +212,10 @@ pub const Random = struct {
212212 if (info.signedness == .signed) {
213213 // Two's complement makes this math pretty easy.
214214 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);
217217 const result = lo +% r.uintLessThan(UnsignedT, hi -% lo);
218 return @as(T, @bitCast(result));
218 return @bitCast(result);
219219 } else {
220220 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
221221 return at_least + r.uintLessThan(T, less_than - at_least);
......@@ -230,10 +230,10 @@ pub const Random = struct {
230230 if (info.signedness == .signed) {
231231 // Two's complement makes this math pretty easy.
232232 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);
235235 const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo);
236 return @as(T, @bitCast(result));
236 return @bitCast(result);
237237 } else {
238238 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
239239 return at_least + r.uintAtMostBiased(T, at_most - at_least);
......@@ -249,10 +249,10 @@ pub const Random = struct {
249249 if (info.signedness == .signed) {
250250 // Two's complement makes this math pretty easy.
251251 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);
254254 const result = lo +% r.uintAtMost(UnsignedT, hi -% lo);
255 return @as(T, @bitCast(result));
255 return @bitCast(result);
256256 } else {
257257 // The signed implementation would work fine, but we can use stricter arithmetic operators here.
258258 return at_least + r.uintAtMost(T, at_most - at_least);
......@@ -281,9 +281,9 @@ pub const Random = struct {
281281 rand_lz += @clz(r.int(u32) | 0x7FF);
282282 }
283283 }
284 const mantissa = @as(u23, @truncate(rand));
284 const mantissa: u23 = @truncate(rand);
285285 const exponent = @as(u32, 126 - rand_lz) << 23;
286 return @as(f32, @bitCast(exponent | mantissa));
286 return @bitCast(exponent | mantissa);
287287 },
288288 f64 => {
289289 // Use 52 random bits for the mantissa, and the rest for the exponent.
......@@ -308,7 +308,7 @@ pub const Random = struct {
308308 }
309309 const mantissa = rand & 0xFFFFFFFFFFFFF;
310310 const exponent = (1022 - rand_lz) << 52;
311 return @as(f64, @bitCast(exponent | mantissa));
311 return @bitCast(exponent | mantissa);
312312 },
313313 else => @compileError("unknown floating point type"),
314314 }
......@@ -320,7 +320,7 @@ pub const Random = struct {
320320 pub fn floatNorm(r: Random, comptime T: type) T {
321321 const value = ziggurat.next_f64(r, ziggurat.NormDist);
322322 switch (T) {
323 f32 => return @as(f32, @floatCast(value)),
323 f32 => return @floatCast(value),
324324 f64 => return value,
325325 else => @compileError("unknown floating point type"),
326326 }
......@@ -332,7 +332,7 @@ pub const Random = struct {
332332 pub fn floatExp(r: Random, comptime T: type) T {
333333 const value = ziggurat.next_f64(r, ziggurat.ExpDist);
334334 switch (T) {
335 f32 => return @as(f32, @floatCast(value)),
335 f32 => return @floatCast(value),
336336 f64 => return value,
337337 else => @compileError("unknown floating point type"),
338338 }
......@@ -366,10 +366,10 @@ pub const Random = struct {
366366 }
367367
368368 // `i <= j < max <= maxInt(MinInt)`
369 const max = @as(MinInt, @intCast(buf.len));
369 const max: MinInt = @intCast(buf.len);
370370 var i: MinInt = 0;
371371 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));
373373 mem.swap(T, &buf[i], &buf[j]);
374374 }
375375 }