blob ac28cbb4 (55049B) - Raw
1 // SPDX-License-Identifier: MIT 2 // Copyright (c) 2015-2021 Zig Contributors 3 // This file is part of [zig](https://ziglang.org/), which is MIT licensed. 4 // The MIT license requires this copyright notice to be included in all copies 5 // and substantial portions of the software. 6 const std = @import("std.zig"); 7 const assert = std.debug.assert; 8 const mem = std.mem; 9 const testing = std.testing; 10 11 /// Euler's number (e) 12 pub const e = 2.71828182845904523536028747135266249775724709369995; 13 14 /// Archimedes' constant (π) 15 pub const pi = 3.14159265358979323846264338327950288419716939937510; 16 17 /// Circle constant (τ) 18 pub const tau = 2 * pi; 19 20 /// log2(e) 21 pub const log2e = 1.442695040888963407359924681001892137; 22 23 /// log10(e) 24 pub const log10e = 0.434294481903251827651128918916605082; 25 26 /// ln(2) 27 pub const ln2 = 0.693147180559945309417232121458176568; 28 29 /// ln(10) 30 pub const ln10 = 2.302585092994045684017991454684364208; 31 32 /// 2/sqrt(π) 33 pub const two_sqrtpi = 1.128379167095512573896158903121545172; 34 35 /// sqrt(2) 36 pub const sqrt2 = 1.414213562373095048801688724209698079; 37 38 /// 1/sqrt(2) 39 pub const sqrt1_2 = 0.707106781186547524400844362104849039; 40 41 // From a small c++ [program using boost float128](https://github.com/winksaville/cpp_boost_float128) 42 pub const f128_true_min = @bitCast(f128, @as(u128, 0x00000000000000000000000000000001)); 43 pub const f128_min = @bitCast(f128, @as(u128, 0x00010000000000000000000000000000)); 44 pub const f128_max = @bitCast(f128, @as(u128, 0x7FFEFFFFFFFFFFFFFFFFFFFFFFFFFFFF)); 45 pub const f128_epsilon = @bitCast(f128, @as(u128, 0x3F8F0000000000000000000000000000)); 46 pub const f128_toint = 1.0 / f128_epsilon; 47 48 // float.h details 49 pub const f64_true_min = 4.94065645841246544177e-324; 50 pub const f64_min = 2.2250738585072014e-308; 51 pub const f64_max = 1.79769313486231570815e+308; 52 pub const f64_epsilon = 2.22044604925031308085e-16; 53 pub const f64_toint = 1.0 / f64_epsilon; 54 55 pub const f32_true_min = 1.40129846432481707092e-45; 56 pub const f32_min = 1.17549435082228750797e-38; 57 pub const f32_max = 3.40282346638528859812e+38; 58 pub const f32_epsilon = 1.1920928955078125e-07; 59 pub const f32_toint = 1.0 / f32_epsilon; 60 61 pub const f16_true_min = 0.000000059604644775390625; // 2**-24 62 pub const f16_min = 0.00006103515625; // 2**-14 63 pub const f16_max = 65504; 64 pub const f16_epsilon = 0.0009765625; // 2**-10 65 pub const f16_toint = 1.0 / f16_epsilon; 66 67 pub const epsilon = @import("math/epsilon.zig").epsilon; 68 69 pub const nan_u16 = @as(u16, 0x7C01); 70 pub const nan_f16 = @bitCast(f16, nan_u16); 71 72 pub const qnan_u16 = @as(u16, 0x7E00); 73 pub const qnan_f16 = @bitCast(f16, qnan_u16); 74 75 pub const inf_u16 = @as(u16, 0x7C00); 76 pub const inf_f16 = @bitCast(f16, inf_u16); 77 78 pub const nan_u32 = @as(u32, 0x7F800001); 79 pub const nan_f32 = @bitCast(f32, nan_u32); 80 81 pub const qnan_u32 = @as(u32, 0x7FC00000); 82 pub const qnan_f32 = @bitCast(f32, qnan_u32); 83 84 pub const inf_u32 = @as(u32, 0x7F800000); 85 pub const inf_f32 = @bitCast(f32, inf_u32); 86 87 pub const nan_u64 = @as(u64, 0x7FF << 52) | 1; 88 pub const nan_f64 = @bitCast(f64, nan_u64); 89 90 pub const qnan_u64 = @as(u64, 0x7ff8000000000000); 91 pub const qnan_f64 = @bitCast(f64, qnan_u64); 92 93 pub const inf_u64 = @as(u64, 0x7FF << 52); 94 pub const inf_f64 = @bitCast(f64, inf_u64); 95 96 pub const nan_u128 = @as(u128, 0x7fff0000000000000000000000000001); 97 pub const nan_f128 = @bitCast(f128, nan_u128); 98 99 pub const qnan_u128 = @as(u128, 0x7fff8000000000000000000000000000); 100 pub const qnan_f128 = @bitCast(f128, qnan_u128); 101 102 pub const inf_u128 = @as(u128, 0x7fff0000000000000000000000000000); 103 pub const inf_f128 = @bitCast(f128, inf_u128); 104 105 pub const nan = @import("math/nan.zig").nan; 106 pub const snan = @import("math/nan.zig").snan; 107 pub const inf = @import("math/inf.zig").inf; 108 109 /// Performs an approximate comparison of two floating point values `x` and `y`. 110 /// Returns true if the absolute difference between them is less or equal than 111 /// the specified tolerance. 112 /// 113 /// The `tolerance` parameter is the absolute tolerance used when determining if 114 /// the two numbers are close enough, a good value for this parameter is a small 115 /// multiple of `epsilon(T)`. 116 /// 117 /// Note that this function is recommended for for comparing small numbers 118 /// around zero, using `approxEqRel` is suggested otherwise. 119 /// 120 /// NaN values are never considered equal to any value. 121 pub fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool { 122 assert(@typeInfo(T) == .Float); 123 assert(tolerance >= 0); 124 125 // Fast path for equal values (and signed zeros and infinites). 126 if (x == y) 127 return true; 128 129 if (isNan(x) or isNan(y)) 130 return false; 131 132 return fabs(x - y) <= tolerance; 133 } 134 135 /// Performs an approximate comparison of two floating point values `x` and `y`. 136 /// Returns true if the absolute difference between them is less or equal than 137 /// `max(|x|, |y|) * tolerance`, where `tolerance` is a positive number greater 138 /// than zero. 139 /// 140 /// The `tolerance` parameter is the relative tolerance used when determining if 141 /// the two numbers are close enough, a good value for this parameter is usually 142 /// `sqrt(epsilon(T))`, meaning that the two numbers are considered equal if at 143 /// least half of the digits are equal. 144 /// 145 /// Note that for comparisons of small numbers around zero this function won't 146 /// give meaningful results, use `approxEqAbs` instead. 147 /// 148 /// NaN values are never considered equal to any value. 149 pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool { 150 assert(@typeInfo(T) == .Float); 151 assert(tolerance > 0); 152 153 // Fast path for equal values (and signed zeros and infinites). 154 if (x == y) 155 return true; 156 157 if (isNan(x) or isNan(y)) 158 return false; 159 160 return fabs(x - y) <= max(fabs(x), fabs(y)) * tolerance; 161 } 162 163 /// Deprecated, use `approxEqAbs` or `approxEqRel`. 164 pub const approxEq = approxEqAbs; 165 166 test "approxEqAbs and approxEqRel" { 167 inline for ([_]type{ f16, f32, f64, f128 }) |T| { 168 const eps_value = comptime epsilon(T); 169 const sqrt_eps_value = comptime sqrt(eps_value); 170 const nan_value = comptime nan(T); 171 const inf_value = comptime inf(T); 172 const min_value: T = switch (T) { 173 f16 => f16_min, 174 f32 => f32_min, 175 f64 => f64_min, 176 f128 => f128_min, 177 else => unreachable, 178 }; 179 180 try testing.expect(approxEqAbs(T, 0.0, 0.0, eps_value)); 181 try testing.expect(approxEqAbs(T, -0.0, -0.0, eps_value)); 182 try testing.expect(approxEqAbs(T, 0.0, -0.0, eps_value)); 183 try testing.expect(approxEqRel(T, 1.0, 1.0, sqrt_eps_value)); 184 try testing.expect(!approxEqRel(T, 1.0, 0.0, sqrt_eps_value)); 185 try testing.expect(!approxEqAbs(T, 1.0 + 2 * epsilon(T), 1.0, eps_value)); 186 try testing.expect(approxEqAbs(T, 1.0 + 1 * epsilon(T), 1.0, eps_value)); 187 try testing.expect(!approxEqRel(T, 1.0, nan_value, sqrt_eps_value)); 188 try testing.expect(!approxEqRel(T, nan_value, nan_value, sqrt_eps_value)); 189 try testing.expect(approxEqRel(T, inf_value, inf_value, sqrt_eps_value)); 190 try testing.expect(approxEqRel(T, min_value, min_value, sqrt_eps_value)); 191 try testing.expect(approxEqRel(T, -min_value, -min_value, sqrt_eps_value)); 192 try testing.expect(approxEqAbs(T, min_value, 0.0, eps_value * 2)); 193 try testing.expect(approxEqAbs(T, -min_value, 0.0, eps_value * 2)); 194 } 195 } 196 197 pub fn doNotOptimizeAway(value: anytype) void { 198 // TODO: use @declareSideEffect() when it is available. 199 // https://github.com/ziglang/zig/issues/6168 200 const T = @TypeOf(value); 201 var x: T = undefined; 202 const p = @ptrCast(*volatile T, &x); 203 p.* = x; 204 } 205 206 pub fn raiseInvalid() void { 207 // Raise INVALID fpu exception 208 } 209 210 pub fn raiseUnderflow() void { 211 // Raise UNDERFLOW fpu exception 212 } 213 214 pub fn raiseOverflow() void { 215 // Raise OVERFLOW fpu exception 216 } 217 218 pub fn raiseInexact() void { 219 // Raise INEXACT fpu exception 220 } 221 222 pub fn raiseDivByZero() void { 223 // Raise INEXACT fpu exception 224 } 225 226 pub const isNan = @import("math/isnan.zig").isNan; 227 pub const isSignalNan = @import("math/isnan.zig").isSignalNan; 228 pub const fabs = @import("math/fabs.zig").fabs; 229 pub const ceil = @import("math/ceil.zig").ceil; 230 pub const floor = @import("math/floor.zig").floor; 231 pub const trunc = @import("math/trunc.zig").trunc; 232 pub const round = @import("math/round.zig").round; 233 pub const frexp = @import("math/frexp.zig").frexp; 234 pub const frexp32_result = @import("math/frexp.zig").frexp32_result; 235 pub const frexp64_result = @import("math/frexp.zig").frexp64_result; 236 pub const modf = @import("math/modf.zig").modf; 237 pub const modf32_result = @import("math/modf.zig").modf32_result; 238 pub const modf64_result = @import("math/modf.zig").modf64_result; 239 pub const copysign = @import("math/copysign.zig").copysign; 240 pub const isFinite = @import("math/isfinite.zig").isFinite; 241 pub const isInf = @import("math/isinf.zig").isInf; 242 pub const isPositiveInf = @import("math/isinf.zig").isPositiveInf; 243 pub const isNegativeInf = @import("math/isinf.zig").isNegativeInf; 244 pub const isNormal = @import("math/isnormal.zig").isNormal; 245 pub const signbit = @import("math/signbit.zig").signbit; 246 pub const scalbn = @import("math/scalbn.zig").scalbn; 247 pub const pow = @import("math/pow.zig").pow; 248 pub const powi = @import("math/powi.zig").powi; 249 pub const sqrt = @import("math/sqrt.zig").sqrt; 250 pub const cbrt = @import("math/cbrt.zig").cbrt; 251 pub const acos = @import("math/acos.zig").acos; 252 pub const asin = @import("math/asin.zig").asin; 253 pub const atan = @import("math/atan.zig").atan; 254 pub const atan2 = @import("math/atan2.zig").atan2; 255 pub const hypot = @import("math/hypot.zig").hypot; 256 pub const exp = @import("math/exp.zig").exp; 257 pub const exp2 = @import("math/exp2.zig").exp2; 258 pub const expm1 = @import("math/expm1.zig").expm1; 259 pub const ilogb = @import("math/ilogb.zig").ilogb; 260 pub const ln = @import("math/ln.zig").ln; 261 pub const log = @import("math/log.zig").log; 262 pub const log2 = @import("math/log2.zig").log2; 263 pub const log10 = @import("math/log10.zig").log10; 264 pub const log1p = @import("math/log1p.zig").log1p; 265 pub const fma = @import("math/fma.zig").fma; 266 pub const asinh = @import("math/asinh.zig").asinh; 267 pub const acosh = @import("math/acosh.zig").acosh; 268 pub const atanh = @import("math/atanh.zig").atanh; 269 pub const sinh = @import("math/sinh.zig").sinh; 270 pub const cosh = @import("math/cosh.zig").cosh; 271 pub const tanh = @import("math/tanh.zig").tanh; 272 pub const cos = @import("math/cos.zig").cos; 273 pub const sin = @import("math/sin.zig").sin; 274 pub const tan = @import("math/tan.zig").tan; 275 276 pub const complex = @import("math/complex.zig"); 277 pub const Complex = complex.Complex; 278 279 pub const big = @import("math/big.zig"); 280 281 test { 282 std.testing.refAllDecls(@This()); 283 } 284 285 pub fn floatMantissaBits(comptime T: type) comptime_int { 286 assert(@typeInfo(T) == .Float); 287 288 return switch (@typeInfo(T).Float.bits) { 289 16 => 10, 290 32 => 23, 291 64 => 52, 292 80 => 64, 293 128 => 112, 294 else => @compileError("unknown floating point type " ++ @typeName(T)), 295 }; 296 } 297 298 pub fn floatExponentBits(comptime T: type) comptime_int { 299 assert(@typeInfo(T) == .Float); 300 301 return switch (@typeInfo(T).Float.bits) { 302 16 => 5, 303 32 => 8, 304 64 => 11, 305 80 => 15, 306 128 => 15, 307 else => @compileError("unknown floating point type " ++ @typeName(T)), 308 }; 309 } 310 311 /// Given two types, returns the smallest one which is capable of holding the 312 /// full range of the minimum value. 313 pub fn Min(comptime A: type, comptime B: type) type { 314 switch (@typeInfo(A)) { 315 .Int => |a_info| switch (@typeInfo(B)) { 316 .Int => |b_info| if (a_info.signedness == .unsigned and b_info.signedness == .unsigned) { 317 if (a_info.bits < b_info.bits) { 318 return A; 319 } else { 320 return B; 321 } 322 }, 323 else => {}, 324 }, 325 else => {}, 326 } 327 return @TypeOf(@as(A, 0) + @as(B, 0)); 328 } 329 330 /// Returns the smaller number. When one of the parameter's type's full range fits in the other, 331 /// the return type is the smaller type. 332 pub fn min(x: anytype, y: anytype) Min(@TypeOf(x), @TypeOf(y)) { 333 const Result = Min(@TypeOf(x), @TypeOf(y)); 334 if (x < y) { 335 // TODO Zig should allow this as an implicit cast because x is immutable and in this 336 // scope it is known to fit in the return type. 337 switch (@typeInfo(Result)) { 338 .Int => return @intCast(Result, x), 339 else => return x, 340 } 341 } else { 342 // TODO Zig should allow this as an implicit cast because y is immutable and in this 343 // scope it is known to fit in the return type. 344 switch (@typeInfo(Result)) { 345 .Int => return @intCast(Result, y), 346 else => return y, 347 } 348 } 349 } 350 351 test "math.min" { 352 try testing.expect(min(@as(i32, -1), @as(i32, 2)) == -1); 353 { 354 var a: u16 = 999; 355 var b: u32 = 10; 356 var result = min(a, b); 357 try testing.expect(@TypeOf(result) == u16); 358 try testing.expect(result == 10); 359 } 360 { 361 var a: f64 = 10.34; 362 var b: f32 = 999.12; 363 var result = min(a, b); 364 try testing.expect(@TypeOf(result) == f64); 365 try testing.expect(result == 10.34); 366 } 367 { 368 var a: i8 = -127; 369 var b: i16 = -200; 370 var result = min(a, b); 371 try testing.expect(@TypeOf(result) == i16); 372 try testing.expect(result == -200); 373 } 374 { 375 const a = 10.34; 376 var b: f32 = 999.12; 377 var result = min(a, b); 378 try testing.expect(@TypeOf(result) == f32); 379 try testing.expect(result == 10.34); 380 } 381 } 382 383 /// Finds the min of three numbers 384 pub fn min3(x: anytype, y: anytype, z: anytype) @TypeOf(x, y, z) { 385 return min(x, min(y, z)); 386 } 387 388 test "math.min3" { 389 try testing.expect(min3(@as(i32, 0), @as(i32, 1), @as(i32, 2)) == 0); 390 try testing.expect(min3(@as(i32, 0), @as(i32, 2), @as(i32, 1)) == 0); 391 try testing.expect(min3(@as(i32, 1), @as(i32, 0), @as(i32, 2)) == 0); 392 try testing.expect(min3(@as(i32, 1), @as(i32, 2), @as(i32, 0)) == 0); 393 try testing.expect(min3(@as(i32, 2), @as(i32, 0), @as(i32, 1)) == 0); 394 try testing.expect(min3(@as(i32, 2), @as(i32, 1), @as(i32, 0)) == 0); 395 } 396 397 pub fn max(x: anytype, y: anytype) @TypeOf(x, y) { 398 return if (x > y) x else y; 399 } 400 401 test "math.max" { 402 try testing.expect(max(@as(i32, -1), @as(i32, 2)) == 2); 403 try testing.expect(max(@as(i32, 2), @as(i32, -1)) == 2); 404 } 405 406 /// Finds the max of three numbers 407 pub fn max3(x: anytype, y: anytype, z: anytype) @TypeOf(x, y, z) { 408 return max(x, max(y, z)); 409 } 410 411 test "math.max3" { 412 try testing.expect(max3(@as(i32, 0), @as(i32, 1), @as(i32, 2)) == 2); 413 try testing.expect(max3(@as(i32, 0), @as(i32, 2), @as(i32, 1)) == 2); 414 try testing.expect(max3(@as(i32, 1), @as(i32, 0), @as(i32, 2)) == 2); 415 try testing.expect(max3(@as(i32, 1), @as(i32, 2), @as(i32, 0)) == 2); 416 try testing.expect(max3(@as(i32, 2), @as(i32, 0), @as(i32, 1)) == 2); 417 try testing.expect(max3(@as(i32, 2), @as(i32, 1), @as(i32, 0)) == 2); 418 } 419 420 pub fn clamp(val: anytype, lower: anytype, upper: anytype) @TypeOf(val, lower, upper) { 421 assert(lower <= upper); 422 return max(lower, min(val, upper)); 423 } 424 test "math.clamp" { 425 // Within range 426 try testing.expect(std.math.clamp(@as(i32, -1), @as(i32, -4), @as(i32, 7)) == -1); 427 // Below 428 try testing.expect(std.math.clamp(@as(i32, -5), @as(i32, -4), @as(i32, 7)) == -4); 429 // Above 430 try testing.expect(std.math.clamp(@as(i32, 8), @as(i32, -4), @as(i32, 7)) == 7); 431 432 // Floating point 433 try testing.expect(std.math.clamp(@as(f32, 1.1), @as(f32, 0.0), @as(f32, 1.0)) == 1.0); 434 try testing.expect(std.math.clamp(@as(f32, -127.5), @as(f32, -200), @as(f32, -100)) == -127.5); 435 436 // Mix of comptime and non-comptime 437 var i: i32 = 1; 438 try testing.expect(std.math.clamp(i, 0, 1) == 1); 439 } 440 441 pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) { 442 var answer: T = undefined; 443 return if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer; 444 } 445 446 pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) { 447 if (T == comptime_int) return a + b; 448 var answer: T = undefined; 449 return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; 450 } 451 452 pub fn sub(comptime T: type, a: T, b: T) (error{Overflow}!T) { 453 var answer: T = undefined; 454 return if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer; 455 } 456 457 pub fn negate(x: anytype) !@TypeOf(x) { 458 return sub(@TypeOf(x), 0, x); 459 } 460 461 pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T { 462 var answer: T = undefined; 463 return if (@shlWithOverflow(T, a, shift_amt, &answer)) error.Overflow else answer; 464 } 465 466 /// Shifts left. Overflowed bits are truncated. 467 /// A negative shift amount results in a right shift. 468 pub fn shl(comptime T: type, a: T, shift_amt: anytype) T { 469 const abs_shift_amt = absCast(shift_amt); 470 471 const casted_shift_amt = blk: { 472 if (@typeInfo(T) == .Vector) { 473 const C = @typeInfo(T).Vector.child; 474 const len = @typeInfo(T).Vector.len; 475 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0)); 476 break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt)); 477 } else { 478 if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0; 479 break :blk @intCast(Log2Int(T), abs_shift_amt); 480 } 481 }; 482 483 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.signedness == .signed) { 484 if (shift_amt < 0) { 485 return a >> casted_shift_amt; 486 } 487 } 488 489 return a << casted_shift_amt; 490 } 491 492 test "math.shl" { 493 try testing.expect(shl(u8, 0b11111111, @as(usize, 3)) == 0b11111000); 494 try testing.expect(shl(u8, 0b11111111, @as(usize, 8)) == 0); 495 try testing.expect(shl(u8, 0b11111111, @as(usize, 9)) == 0); 496 try testing.expect(shl(u8, 0b11111111, @as(isize, -2)) == 0b00111111); 497 try testing.expect(shl(u8, 0b11111111, 3) == 0b11111000); 498 try testing.expect(shl(u8, 0b11111111, 8) == 0); 499 try testing.expect(shl(u8, 0b11111111, 9) == 0); 500 try testing.expect(shl(u8, 0b11111111, -2) == 0b00111111); 501 try testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) << 1); 502 try testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) >> 1); 503 try testing.expect(shl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0); 504 } 505 506 /// Shifts right. Overflowed bits are truncated. 507 /// A negative shift amount results in a left shift. 508 pub fn shr(comptime T: type, a: T, shift_amt: anytype) T { 509 const abs_shift_amt = absCast(shift_amt); 510 511 const casted_shift_amt = blk: { 512 if (@typeInfo(T) == .Vector) { 513 const C = @typeInfo(T).Vector.child; 514 const len = @typeInfo(T).Vector.len; 515 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0)); 516 break :blk @splat(len, @intCast(Log2Int(C), abs_shift_amt)); 517 } else { 518 if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0; 519 break :blk @intCast(Log2Int(T), abs_shift_amt); 520 } 521 }; 522 523 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.signedness == .signed) { 524 if (shift_amt < 0) { 525 return a << casted_shift_amt; 526 } 527 } 528 529 return a >> casted_shift_amt; 530 } 531 532 test "math.shr" { 533 try testing.expect(shr(u8, 0b11111111, @as(usize, 3)) == 0b00011111); 534 try testing.expect(shr(u8, 0b11111111, @as(usize, 8)) == 0); 535 try testing.expect(shr(u8, 0b11111111, @as(usize, 9)) == 0); 536 try testing.expect(shr(u8, 0b11111111, @as(isize, -2)) == 0b11111100); 537 try testing.expect(shr(u8, 0b11111111, 3) == 0b00011111); 538 try testing.expect(shr(u8, 0b11111111, 8) == 0); 539 try testing.expect(shr(u8, 0b11111111, 9) == 0); 540 try testing.expect(shr(u8, 0b11111111, -2) == 0b11111100); 541 try testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(usize, 1))[0] == @as(u32, 42) >> 1); 542 try testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, @as(isize, -1))[0] == @as(u32, 42) << 1); 543 try testing.expect(shr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){42}, 33)[0] == 0); 544 } 545 546 /// Rotates right. Only unsigned values can be rotated. 547 /// Negative shift values results in shift modulo the bit count. 548 pub fn rotr(comptime T: type, x: T, r: anytype) T { 549 if (@typeInfo(T) == .Vector) { 550 const C = @typeInfo(T).Vector.child; 551 if (@typeInfo(C).Int.signedness == .signed) { 552 @compileError("cannot rotate signed integers"); 553 } 554 const ar = @intCast(Log2Int(C), @mod(r, @typeInfo(C).Int.bits)); 555 return (x >> @splat(@typeInfo(T).Vector.len, ar)) | (x << @splat(@typeInfo(T).Vector.len, 1 + ~ar)); 556 } else if (@typeInfo(T).Int.signedness == .signed) { 557 @compileError("cannot rotate signed integer"); 558 } else { 559 const ar = @mod(r, @typeInfo(T).Int.bits); 560 return shr(T, x, ar) | shl(T, x, @typeInfo(T).Int.bits - ar); 561 } 562 } 563 564 test "math.rotr" { 565 try testing.expect(rotr(u8, 0b00000001, @as(usize, 0)) == 0b00000001); 566 try testing.expect(rotr(u8, 0b00000001, @as(usize, 9)) == 0b10000000); 567 try testing.expect(rotr(u8, 0b00000001, @as(usize, 8)) == 0b00000001); 568 try testing.expect(rotr(u8, 0b00000001, @as(usize, 4)) == 0b00010000); 569 try testing.expect(rotr(u8, 0b00000001, @as(isize, -1)) == 0b00000010); 570 try testing.expect(rotr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){1}, @as(usize, 1))[0] == @as(u32, 1) << 31); 571 try testing.expect(rotr(std.meta.Vector(1, u32), std.meta.Vector(1, u32){1}, @as(isize, -1))[0] == @as(u32, 1) << 1); 572 } 573 574 /// Rotates left. Only unsigned values can be rotated. 575 /// Negative shift values results in shift modulo the bit count. 576 pub fn rotl(comptime T: type, x: T, r: anytype) T { 577 if (@typeInfo(T) == .Vector) { 578 const C = @typeInfo(T).Vector.child; 579 if (@typeInfo(C).Int.signedness == .signed) { 580 @compileError("cannot rotate signed integers"); 581 } 582 const ar = @intCast(Log2Int(C), @mod(r, @typeInfo(C).Int.bits)); 583 return (x << @splat(@typeInfo(T).Vector.len, ar)) | (x >> @splat(@typeInfo(T).Vector.len, 1 +% ~ar)); 584 } else if (@typeInfo(T).Int.signedness == .signed) { 585 @compileError("cannot rotate signed integer"); 586 } else { 587 const ar = @mod(r, @typeInfo(T).Int.bits); 588 return shl(T, x, ar) | shr(T, x, @typeInfo(T).Int.bits - ar); 589 } 590 } 591 592 test "math.rotl" { 593 try testing.expect(rotl(u8, 0b00000001, @as(usize, 0)) == 0b00000001); 594 try testing.expect(rotl(u8, 0b00000001, @as(usize, 9)) == 0b00000010); 595 try testing.expect(rotl(u8, 0b00000001, @as(usize, 8)) == 0b00000001); 596 try testing.expect(rotl(u8, 0b00000001, @as(usize, 4)) == 0b00010000); 597 try testing.expect(rotl(u8, 0b00000001, @as(isize, -1)) == 0b10000000); 598 try testing.expect(rotl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){1 << 31}, @as(usize, 1))[0] == 1); 599 try testing.expect(rotl(std.meta.Vector(1, u32), std.meta.Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30); 600 } 601 602 pub fn Log2Int(comptime T: type) type { 603 // comptime ceil log2 604 comptime var count = 0; 605 comptime var s = @typeInfo(T).Int.bits - 1; 606 inline while (s != 0) : (s >>= 1) { 607 count += 1; 608 } 609 610 return std.meta.Int(.unsigned, count); 611 } 612 613 pub fn Log2IntCeil(comptime T: type) type { 614 // comptime ceil log2 615 comptime var count = 0; 616 comptime var s = @typeInfo(T).Int.bits; 617 inline while (s != 0) : (s >>= 1) { 618 count += 1; 619 } 620 621 return std.meta.Int(.unsigned, count); 622 } 623 624 pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) type { 625 assert(from <= to); 626 if (from == 0 and to == 0) { 627 return u0; 628 } 629 const sign: std.builtin.Signedness = if (from < 0) .signed else .unsigned; 630 const largest_positive_integer = max(if (from < 0) (-from) - 1 else from, to); // two's complement 631 const base = log2(largest_positive_integer); 632 const upper = (1 << base) - 1; 633 var magnitude_bits = if (upper >= largest_positive_integer) base else base + 1; 634 if (sign == .signed) { 635 magnitude_bits += 1; 636 } 637 return std.meta.Int(sign, magnitude_bits); 638 } 639 640 test "math.IntFittingRange" { 641 try testing.expect(IntFittingRange(0, 0) == u0); 642 try testing.expect(IntFittingRange(0, 1) == u1); 643 try testing.expect(IntFittingRange(0, 2) == u2); 644 try testing.expect(IntFittingRange(0, 3) == u2); 645 try testing.expect(IntFittingRange(0, 4) == u3); 646 try testing.expect(IntFittingRange(0, 7) == u3); 647 try testing.expect(IntFittingRange(0, 8) == u4); 648 try testing.expect(IntFittingRange(0, 9) == u4); 649 try testing.expect(IntFittingRange(0, 15) == u4); 650 try testing.expect(IntFittingRange(0, 16) == u5); 651 try testing.expect(IntFittingRange(0, 17) == u5); 652 try testing.expect(IntFittingRange(0, 4095) == u12); 653 try testing.expect(IntFittingRange(2000, 4095) == u12); 654 try testing.expect(IntFittingRange(0, 4096) == u13); 655 try testing.expect(IntFittingRange(2000, 4096) == u13); 656 try testing.expect(IntFittingRange(0, 4097) == u13); 657 try testing.expect(IntFittingRange(2000, 4097) == u13); 658 try testing.expect(IntFittingRange(0, 123456789123456798123456789) == u87); 659 try testing.expect(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177); 660 661 try testing.expect(IntFittingRange(-1, -1) == i1); 662 try testing.expect(IntFittingRange(-1, 0) == i1); 663 try testing.expect(IntFittingRange(-1, 1) == i2); 664 try testing.expect(IntFittingRange(-2, -2) == i2); 665 try testing.expect(IntFittingRange(-2, -1) == i2); 666 try testing.expect(IntFittingRange(-2, 0) == i2); 667 try testing.expect(IntFittingRange(-2, 1) == i2); 668 try testing.expect(IntFittingRange(-2, 2) == i3); 669 try testing.expect(IntFittingRange(-1, 2) == i3); 670 try testing.expect(IntFittingRange(-1, 3) == i3); 671 try testing.expect(IntFittingRange(-1, 4) == i4); 672 try testing.expect(IntFittingRange(-1, 7) == i4); 673 try testing.expect(IntFittingRange(-1, 8) == i5); 674 try testing.expect(IntFittingRange(-1, 9) == i5); 675 try testing.expect(IntFittingRange(-1, 15) == i5); 676 try testing.expect(IntFittingRange(-1, 16) == i6); 677 try testing.expect(IntFittingRange(-1, 17) == i6); 678 try testing.expect(IntFittingRange(-1, 4095) == i13); 679 try testing.expect(IntFittingRange(-4096, 4095) == i13); 680 try testing.expect(IntFittingRange(-1, 4096) == i14); 681 try testing.expect(IntFittingRange(-4097, 4095) == i14); 682 try testing.expect(IntFittingRange(-1, 4097) == i14); 683 try testing.expect(IntFittingRange(-1, 123456789123456798123456789) == i88); 684 try testing.expect(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178); 685 } 686 687 test "math overflow functions" { 688 try testOverflow(); 689 comptime try testOverflow(); 690 } 691 692 fn testOverflow() !void { 693 try testing.expect((mul(i32, 3, 4) catch unreachable) == 12); 694 try testing.expect((add(i32, 3, 4) catch unreachable) == 7); 695 try testing.expect((sub(i32, 3, 4) catch unreachable) == -1); 696 try testing.expect((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000); 697 } 698 699 pub fn absInt(x: anytype) !@TypeOf(x) { 700 const T = @TypeOf(x); 701 comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt 702 comptime assert(@typeInfo(T).Int.signedness == .signed); // must pass a signed integer to absInt 703 704 if (x == minInt(@TypeOf(x))) { 705 return error.Overflow; 706 } else { 707 @setRuntimeSafety(false); 708 return if (x < 0) -x else x; 709 } 710 } 711 712 test "math.absInt" { 713 try testAbsInt(); 714 comptime try testAbsInt(); 715 } 716 fn testAbsInt() !void { 717 try testing.expect((absInt(@as(i32, -10)) catch unreachable) == 10); 718 try testing.expect((absInt(@as(i32, 10)) catch unreachable) == 10); 719 } 720 721 pub const absFloat = fabs; 722 723 test "math.absFloat" { 724 try testAbsFloat(); 725 comptime try testAbsFloat(); 726 } 727 fn testAbsFloat() !void { 728 try testing.expect(absFloat(@as(f32, -10.05)) == 10.05); 729 try testing.expect(absFloat(@as(f32, 10.05)) == 10.05); 730 } 731 732 pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T { 733 @setRuntimeSafety(false); 734 if (denominator == 0) return error.DivisionByZero; 735 if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; 736 return @divTrunc(numerator, denominator); 737 } 738 739 test "math.divTrunc" { 740 try testDivTrunc(); 741 comptime try testDivTrunc(); 742 } 743 fn testDivTrunc() !void { 744 try testing.expect((divTrunc(i32, 5, 3) catch unreachable) == 1); 745 try testing.expect((divTrunc(i32, -5, 3) catch unreachable) == -1); 746 try testing.expectError(error.DivisionByZero, divTrunc(i8, -5, 0)); 747 try testing.expectError(error.Overflow, divTrunc(i8, -128, -1)); 748 749 try testing.expect((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0); 750 try testing.expect((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0); 751 } 752 753 pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T { 754 @setRuntimeSafety(false); 755 if (denominator == 0) return error.DivisionByZero; 756 if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; 757 return @divFloor(numerator, denominator); 758 } 759 760 test "math.divFloor" { 761 try testDivFloor(); 762 comptime try testDivFloor(); 763 } 764 fn testDivFloor() !void { 765 try testing.expect((divFloor(i32, 5, 3) catch unreachable) == 1); 766 try testing.expect((divFloor(i32, -5, 3) catch unreachable) == -2); 767 try testing.expectError(error.DivisionByZero, divFloor(i8, -5, 0)); 768 try testing.expectError(error.Overflow, divFloor(i8, -128, -1)); 769 770 try testing.expect((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0); 771 try testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0); 772 } 773 774 pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T { 775 @setRuntimeSafety(false); 776 if (comptime std.meta.trait.isNumber(T) and denominator == 0) return error.DivisionByZero; 777 const info = @typeInfo(T); 778 switch (info) { 779 .ComptimeFloat, .Float => return @ceil(numerator / denominator), 780 .ComptimeInt, .Int => { 781 if (numerator < 0 and denominator < 0) { 782 if (info == .Int and numerator == minInt(T) and denominator == -1) 783 return error.Overflow; 784 return @divFloor(numerator + 1, denominator) + 1; 785 } 786 if (numerator > 0 and denominator > 0) 787 return @divFloor(numerator - 1, denominator) + 1; 788 return @divTrunc(numerator, denominator); 789 }, 790 else => @compileError("divCeil unsupported on " ++ @typeName(T)), 791 } 792 } 793 794 test "math.divCeil" { 795 try testDivCeil(); 796 comptime try testDivCeil(); 797 } 798 fn testDivCeil() !void { 799 try testing.expectEqual(@as(i32, 2), divCeil(i32, 5, 3) catch unreachable); 800 try testing.expectEqual(@as(i32, -1), divCeil(i32, -5, 3) catch unreachable); 801 try testing.expectEqual(@as(i32, -1), divCeil(i32, 5, -3) catch unreachable); 802 try testing.expectEqual(@as(i32, 2), divCeil(i32, -5, -3) catch unreachable); 803 try testing.expectEqual(@as(i32, 0), divCeil(i32, 0, 5) catch unreachable); 804 try testing.expectEqual(@as(u32, 0), divCeil(u32, 0, 5) catch unreachable); 805 try testing.expectError(error.DivisionByZero, divCeil(i8, -5, 0)); 806 try testing.expectError(error.Overflow, divCeil(i8, -128, -1)); 807 808 try testing.expectEqual(@as(f32, 0.0), divCeil(f32, 0.0, 5.0) catch unreachable); 809 try testing.expectEqual(@as(f32, 2.0), divCeil(f32, 5.0, 3.0) catch unreachable); 810 try testing.expectEqual(@as(f32, -1.0), divCeil(f32, -5.0, 3.0) catch unreachable); 811 try testing.expectEqual(@as(f32, -1.0), divCeil(f32, 5.0, -3.0) catch unreachable); 812 try testing.expectEqual(@as(f32, 2.0), divCeil(f32, -5.0, -3.0) catch unreachable); 813 814 try testing.expectEqual(6, divCeil(comptime_int, 23, 4) catch unreachable); 815 try testing.expectEqual(-5, divCeil(comptime_int, -23, 4) catch unreachable); 816 try testing.expectEqual(-5, divCeil(comptime_int, 23, -4) catch unreachable); 817 try testing.expectEqual(6, divCeil(comptime_int, -23, -4) catch unreachable); 818 try testing.expectError(error.DivisionByZero, divCeil(comptime_int, 23, 0)); 819 820 try testing.expectEqual(6.0, divCeil(comptime_float, 23.0, 4.0) catch unreachable); 821 try testing.expectEqual(-5.0, divCeil(comptime_float, -23.0, 4.0) catch unreachable); 822 try testing.expectEqual(-5.0, divCeil(comptime_float, 23.0, -4.0) catch unreachable); 823 try testing.expectEqual(6.0, divCeil(comptime_float, -23.0, -4.0) catch unreachable); 824 try testing.expectError(error.DivisionByZero, divCeil(comptime_float, 23.0, 0.0)); 825 } 826 827 pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { 828 @setRuntimeSafety(false); 829 if (denominator == 0) return error.DivisionByZero; 830 if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; 831 const result = @divTrunc(numerator, denominator); 832 if (result * denominator != numerator) return error.UnexpectedRemainder; 833 return result; 834 } 835 836 test "math.divExact" { 837 try testDivExact(); 838 comptime try testDivExact(); 839 } 840 fn testDivExact() !void { 841 try testing.expect((divExact(i32, 10, 5) catch unreachable) == 2); 842 try testing.expect((divExact(i32, -10, 5) catch unreachable) == -2); 843 try testing.expectError(error.DivisionByZero, divExact(i8, -5, 0)); 844 try testing.expectError(error.Overflow, divExact(i8, -128, -1)); 845 try testing.expectError(error.UnexpectedRemainder, divExact(i32, 5, 2)); 846 847 try testing.expect((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0); 848 try testing.expect((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0); 849 try testing.expectError(error.UnexpectedRemainder, divExact(f32, 5.0, 2.0)); 850 } 851 852 pub fn mod(comptime T: type, numerator: T, denominator: T) !T { 853 @setRuntimeSafety(false); 854 if (denominator == 0) return error.DivisionByZero; 855 if (denominator < 0) return error.NegativeDenominator; 856 return @mod(numerator, denominator); 857 } 858 859 test "math.mod" { 860 try testMod(); 861 comptime try testMod(); 862 } 863 fn testMod() !void { 864 try testing.expect((mod(i32, -5, 3) catch unreachable) == 1); 865 try testing.expect((mod(i32, 5, 3) catch unreachable) == 2); 866 try testing.expectError(error.NegativeDenominator, mod(i32, 10, -1)); 867 try testing.expectError(error.DivisionByZero, mod(i32, 10, 0)); 868 869 try testing.expect((mod(f32, -5, 3) catch unreachable) == 1); 870 try testing.expect((mod(f32, 5, 3) catch unreachable) == 2); 871 try testing.expectError(error.NegativeDenominator, mod(f32, 10, -1)); 872 try testing.expectError(error.DivisionByZero, mod(f32, 10, 0)); 873 } 874 875 pub fn rem(comptime T: type, numerator: T, denominator: T) !T { 876 @setRuntimeSafety(false); 877 if (denominator == 0) return error.DivisionByZero; 878 if (denominator < 0) return error.NegativeDenominator; 879 return @rem(numerator, denominator); 880 } 881 882 test "math.rem" { 883 try testRem(); 884 comptime try testRem(); 885 } 886 fn testRem() !void { 887 try testing.expect((rem(i32, -5, 3) catch unreachable) == -2); 888 try testing.expect((rem(i32, 5, 3) catch unreachable) == 2); 889 try testing.expectError(error.NegativeDenominator, rem(i32, 10, -1)); 890 try testing.expectError(error.DivisionByZero, rem(i32, 10, 0)); 891 892 try testing.expect((rem(f32, -5, 3) catch unreachable) == -2); 893 try testing.expect((rem(f32, 5, 3) catch unreachable) == 2); 894 try testing.expectError(error.NegativeDenominator, rem(f32, 10, -1)); 895 try testing.expectError(error.DivisionByZero, rem(f32, 10, 0)); 896 } 897 898 /// Returns the absolute value of the integer parameter. 899 /// Result is an unsigned integer. 900 pub fn absCast(x: anytype) switch (@typeInfo(@TypeOf(x))) { 901 .ComptimeInt => comptime_int, 902 .Int => |intInfo| std.meta.Int(.unsigned, intInfo.bits), 903 else => @compileError("absCast only accepts integers"), 904 } { 905 switch (@typeInfo(@TypeOf(x))) { 906 .ComptimeInt => { 907 if (x < 0) { 908 return -x; 909 } else { 910 return x; 911 } 912 }, 913 .Int => |intInfo| { 914 const Uint = std.meta.Int(.unsigned, intInfo.bits); 915 if (x < 0) { 916 return ~@bitCast(Uint, x +% -1); 917 } else { 918 return @intCast(Uint, x); 919 } 920 }, 921 else => unreachable, 922 } 923 } 924 925 test "math.absCast" { 926 try testing.expectEqual(@as(u1, 1), absCast(@as(i1, -1))); 927 try testing.expectEqual(@as(u32, 999), absCast(@as(i32, -999))); 928 try testing.expectEqual(@as(u32, 999), absCast(@as(i32, 999))); 929 try testing.expectEqual(@as(u32, -minInt(i32)), absCast(@as(i32, minInt(i32)))); 930 try testing.expectEqual(999, absCast(-999)); 931 } 932 933 /// Returns the negation of the integer parameter. 934 /// Result is a signed integer. 935 pub fn negateCast(x: anytype) !std.meta.Int(.signed, std.meta.bitCount(@TypeOf(x))) { 936 if (@typeInfo(@TypeOf(x)).Int.signedness == .signed) return negate(x); 937 938 const int = std.meta.Int(.signed, std.meta.bitCount(@TypeOf(x))); 939 if (x > -minInt(int)) return error.Overflow; 940 941 if (x == -minInt(int)) return minInt(int); 942 943 return -@intCast(int, x); 944 } 945 946 test "math.negateCast" { 947 try testing.expect((negateCast(@as(u32, 999)) catch unreachable) == -999); 948 try testing.expect(@TypeOf(negateCast(@as(u32, 999)) catch unreachable) == i32); 949 950 try testing.expect((negateCast(@as(u32, -minInt(i32))) catch unreachable) == minInt(i32)); 951 try testing.expect(@TypeOf(negateCast(@as(u32, -minInt(i32))) catch unreachable) == i32); 952 953 try testing.expectError(error.Overflow, negateCast(@as(u32, maxInt(i32) + 10))); 954 } 955 956 /// Cast an integer to a different integer type. If the value doesn't fit, 957 /// return an error. 958 /// TODO make this an optional not an error. 959 pub fn cast(comptime T: type, x: anytype) (error{Overflow}!T) { 960 comptime assert(@typeInfo(T) == .Int); // must pass an integer 961 comptime assert(@typeInfo(@TypeOf(x)) == .Int); // must pass an integer 962 if (maxInt(@TypeOf(x)) > maxInt(T) and x > maxInt(T)) { 963 return error.Overflow; 964 } else if (minInt(@TypeOf(x)) < minInt(T) and x < minInt(T)) { 965 return error.Overflow; 966 } else { 967 return @intCast(T, x); 968 } 969 } 970 971 test "math.cast" { 972 try testing.expectError(error.Overflow, cast(u8, @as(u32, 300))); 973 try testing.expectError(error.Overflow, cast(i8, @as(i32, -200))); 974 try testing.expectError(error.Overflow, cast(u8, @as(i8, -1))); 975 try testing.expectError(error.Overflow, cast(u64, @as(i8, -1))); 976 977 try testing.expect((try cast(u8, @as(u32, 255))) == @as(u8, 255)); 978 try testing.expect(@TypeOf(try cast(u8, @as(u32, 255))) == u8); 979 } 980 981 pub const AlignCastError = error{UnalignedMemory}; 982 983 /// Align cast a pointer but return an error if it's the wrong alignment 984 pub fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!@TypeOf(@alignCast(alignment, ptr)) { 985 const addr = @ptrToInt(ptr); 986 if (addr % alignment != 0) { 987 return error.UnalignedMemory; 988 } 989 return @alignCast(alignment, ptr); 990 } 991 992 pub fn isPowerOfTwo(v: anytype) bool { 993 assert(v != 0); 994 return (v & (v - 1)) == 0; 995 } 996 997 pub fn floorPowerOfTwo(comptime T: type, value: T) T { 998 var x = value; 999 1000 comptime var i = 1; 1001 inline while (@typeInfo(T).Int.bits > i) : (i *= 2) { 1002 x |= (x >> i); 1003 } 1004 1005 return x - (x >> 1); 1006 } 1007 1008 test "math.floorPowerOfTwo" { 1009 try testFloorPowerOfTwo(); 1010 comptime try testFloorPowerOfTwo(); 1011 } 1012 1013 fn testFloorPowerOfTwo() !void { 1014 try testing.expect(floorPowerOfTwo(u32, 63) == 32); 1015 try testing.expect(floorPowerOfTwo(u32, 64) == 64); 1016 try testing.expect(floorPowerOfTwo(u32, 65) == 64); 1017 try testing.expect(floorPowerOfTwo(u4, 7) == 4); 1018 try testing.expect(floorPowerOfTwo(u4, 8) == 8); 1019 try testing.expect(floorPowerOfTwo(u4, 9) == 8); 1020 } 1021 1022 /// Returns the next power of two (if the value is not already a power of two). 1023 /// Only unsigned integers can be used. Zero is not an allowed input. 1024 /// Result is a type with 1 more bit than the input type. 1025 pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits + 1) { 1026 comptime assert(@typeInfo(T) == .Int); 1027 comptime assert(@typeInfo(T).Int.signedness == .unsigned); 1028 assert(value != 0); 1029 const PromotedType = std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits + 1); 1030 const ShiftType = std.math.Log2Int(PromotedType); 1031 return @as(PromotedType, 1) << @intCast(ShiftType, @typeInfo(T).Int.bits - @clz(T, value - 1)); 1032 } 1033 1034 /// Returns the next power of two (if the value is not already a power of two). 1035 /// Only unsigned integers can be used. Zero is not an allowed input. 1036 /// If the value doesn't fit, returns an error. 1037 pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) { 1038 comptime assert(@typeInfo(T) == .Int); 1039 const info = @typeInfo(T).Int; 1040 comptime assert(info.signedness == .unsigned); 1041 const PromotedType = std.meta.Int(info.signedness, info.bits + 1); 1042 const overflowBit = @as(PromotedType, 1) << info.bits; 1043 var x = ceilPowerOfTwoPromote(T, value); 1044 if (overflowBit & x != 0) { 1045 return error.Overflow; 1046 } 1047 return @intCast(T, x); 1048 } 1049 1050 pub fn ceilPowerOfTwoAssert(comptime T: type, value: T) T { 1051 return ceilPowerOfTwo(T, value) catch unreachable; 1052 } 1053 1054 test "math.ceilPowerOfTwoPromote" { 1055 try testCeilPowerOfTwoPromote(); 1056 comptime try testCeilPowerOfTwoPromote(); 1057 } 1058 1059 fn testCeilPowerOfTwoPromote() !void { 1060 try testing.expectEqual(@as(u33, 1), ceilPowerOfTwoPromote(u32, 1)); 1061 try testing.expectEqual(@as(u33, 2), ceilPowerOfTwoPromote(u32, 2)); 1062 try testing.expectEqual(@as(u33, 64), ceilPowerOfTwoPromote(u32, 63)); 1063 try testing.expectEqual(@as(u33, 64), ceilPowerOfTwoPromote(u32, 64)); 1064 try testing.expectEqual(@as(u33, 128), ceilPowerOfTwoPromote(u32, 65)); 1065 try testing.expectEqual(@as(u6, 8), ceilPowerOfTwoPromote(u5, 7)); 1066 try testing.expectEqual(@as(u6, 8), ceilPowerOfTwoPromote(u5, 8)); 1067 try testing.expectEqual(@as(u6, 16), ceilPowerOfTwoPromote(u5, 9)); 1068 try testing.expectEqual(@as(u5, 16), ceilPowerOfTwoPromote(u4, 9)); 1069 } 1070 1071 test "math.ceilPowerOfTwo" { 1072 try testCeilPowerOfTwo(); 1073 comptime try testCeilPowerOfTwo(); 1074 } 1075 1076 fn testCeilPowerOfTwo() !void { 1077 try testing.expectEqual(@as(u32, 1), try ceilPowerOfTwo(u32, 1)); 1078 try testing.expectEqual(@as(u32, 2), try ceilPowerOfTwo(u32, 2)); 1079 try testing.expectEqual(@as(u32, 64), try ceilPowerOfTwo(u32, 63)); 1080 try testing.expectEqual(@as(u32, 64), try ceilPowerOfTwo(u32, 64)); 1081 try testing.expectEqual(@as(u32, 128), try ceilPowerOfTwo(u32, 65)); 1082 try testing.expectEqual(@as(u5, 8), try ceilPowerOfTwo(u5, 7)); 1083 try testing.expectEqual(@as(u5, 8), try ceilPowerOfTwo(u5, 8)); 1084 try testing.expectEqual(@as(u5, 16), try ceilPowerOfTwo(u5, 9)); 1085 try testing.expectError(error.Overflow, ceilPowerOfTwo(u4, 9)); 1086 } 1087 1088 pub fn log2_int(comptime T: type, x: T) Log2Int(T) { 1089 if (@typeInfo(T) != .Int or @typeInfo(T).Int.signedness != .unsigned) 1090 @compileError("log2_int requires an unsigned integer, found "++@typeName(T)); 1091 assert(x != 0); 1092 return @intCast(Log2Int(T), @typeInfo(T).Int.bits - 1 - @clz(T, x)); 1093 } 1094 1095 pub fn log2_int_ceil(comptime T: type, x: T) Log2IntCeil(T) { 1096 if (@typeInfo(T) != .Int or @typeInfo(T).Int.signedness != .unsigned) 1097 @compileError("log2_int_ceil requires an unsigned integer, found "++@typeName(T)); 1098 assert(x != 0); 1099 if (x == 1) return 0; 1100 const log2_val: Log2IntCeil(T) = log2_int(T, x - 1); 1101 return log2_val + 1; 1102 } 1103 1104 test "std.math.log2_int_ceil" { 1105 try testing.expect(log2_int_ceil(u32, 1) == 0); 1106 try testing.expect(log2_int_ceil(u32, 2) == 1); 1107 try testing.expect(log2_int_ceil(u32, 3) == 2); 1108 try testing.expect(log2_int_ceil(u32, 4) == 2); 1109 try testing.expect(log2_int_ceil(u32, 5) == 3); 1110 try testing.expect(log2_int_ceil(u32, 6) == 3); 1111 try testing.expect(log2_int_ceil(u32, 7) == 3); 1112 try testing.expect(log2_int_ceil(u32, 8) == 3); 1113 try testing.expect(log2_int_ceil(u32, 9) == 4); 1114 try testing.expect(log2_int_ceil(u32, 10) == 4); 1115 } 1116 1117 ///Cast a value to a different type. If the value doesn't fit in, or can't be perfectly represented by, 1118 ///the new type, it will be converted to the closest possible representation. 1119 pub fn lossyCast(comptime T: type, value: anytype) T { 1120 switch (@typeInfo(T)) { 1121 .Float => { 1122 switch (@typeInfo(@TypeOf(value))) { 1123 .Int => return @intToFloat(T, value), 1124 .Float => return @floatCast(T, value), 1125 .ComptimeInt => return @as(T, value), 1126 .ComptimeFloat => return @as(T, value), 1127 else => @compileError("bad type"), 1128 } 1129 }, 1130 .Int => { 1131 switch (@typeInfo(@TypeOf(value))) { 1132 .Int, .ComptimeInt => { 1133 if (value > maxInt(T)) { 1134 return @as(T, maxInt(T)); 1135 } else if (value < minInt(T)) { 1136 return @as(T, minInt(T)); 1137 } else { 1138 return @intCast(T, value); 1139 } 1140 }, 1141 .Float, .ComptimeFloat => { 1142 if (value > maxInt(T)) { 1143 return @as(T, maxInt(T)); 1144 } else if (value < minInt(T)) { 1145 return @as(T, minInt(T)); 1146 } else { 1147 return @floatToInt(T, value); 1148 } 1149 }, 1150 else => @compileError("bad type"), 1151 } 1152 }, 1153 else => @compileError("bad result type"), 1154 } 1155 } 1156 1157 test "math.lossyCast" { 1158 try testing.expect(lossyCast(i16, 70000.0) == @as(i16, 32767)); 1159 try testing.expect(lossyCast(u32, @as(i16, -255)) == @as(u32, 0)); 1160 try testing.expect(lossyCast(i9, @as(u32, 200)) == @as(i9, 200)); 1161 } 1162 1163 test "math.f64_min" { 1164 const f64_min_u64 = 0x0010000000000000; 1165 const fmin: f64 = f64_min; 1166 try testing.expect(@bitCast(u64, fmin) == f64_min_u64); 1167 } 1168 1169 pub fn maxInt(comptime T: type) comptime_int { 1170 const info = @typeInfo(T); 1171 const bit_count = info.Int.bits; 1172 if (bit_count == 0) return 0; 1173 return (1 << (bit_count - @boolToInt(info.Int.signedness == .signed))) - 1; 1174 } 1175 1176 pub fn minInt(comptime T: type) comptime_int { 1177 const info = @typeInfo(T); 1178 const bit_count = info.Int.bits; 1179 if (info.Int.signedness == .unsigned) return 0; 1180 if (bit_count == 0) return 0; 1181 return -(1 << (bit_count - 1)); 1182 } 1183 1184 test "minInt and maxInt" { 1185 try testing.expect(maxInt(u0) == 0); 1186 try testing.expect(maxInt(u1) == 1); 1187 try testing.expect(maxInt(u8) == 255); 1188 try testing.expect(maxInt(u16) == 65535); 1189 try testing.expect(maxInt(u32) == 4294967295); 1190 try testing.expect(maxInt(u64) == 18446744073709551615); 1191 try testing.expect(maxInt(u128) == 340282366920938463463374607431768211455); 1192 1193 try testing.expect(maxInt(i0) == 0); 1194 try testing.expect(maxInt(i1) == 0); 1195 try testing.expect(maxInt(i8) == 127); 1196 try testing.expect(maxInt(i16) == 32767); 1197 try testing.expect(maxInt(i32) == 2147483647); 1198 try testing.expect(maxInt(i63) == 4611686018427387903); 1199 try testing.expect(maxInt(i64) == 9223372036854775807); 1200 try testing.expect(maxInt(i128) == 170141183460469231731687303715884105727); 1201 1202 try testing.expect(minInt(u0) == 0); 1203 try testing.expect(minInt(u1) == 0); 1204 try testing.expect(minInt(u8) == 0); 1205 try testing.expect(minInt(u16) == 0); 1206 try testing.expect(minInt(u32) == 0); 1207 try testing.expect(minInt(u63) == 0); 1208 try testing.expect(minInt(u64) == 0); 1209 try testing.expect(minInt(u128) == 0); 1210 1211 try testing.expect(minInt(i0) == 0); 1212 try testing.expect(minInt(i1) == -1); 1213 try testing.expect(minInt(i8) == -128); 1214 try testing.expect(minInt(i16) == -32768); 1215 try testing.expect(minInt(i32) == -2147483648); 1216 try testing.expect(minInt(i63) == -4611686018427387904); 1217 try testing.expect(minInt(i64) == -9223372036854775808); 1218 try testing.expect(minInt(i128) == -170141183460469231731687303715884105728); 1219 } 1220 1221 test "max value type" { 1222 const x: u32 = maxInt(i32); 1223 try testing.expect(x == 2147483647); 1224 } 1225 1226 pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits * 2) { 1227 const ResultInt = std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits * 2); 1228 return @as(ResultInt, a) * @as(ResultInt, b); 1229 } 1230 1231 test "math.mulWide" { 1232 try testing.expect(mulWide(u8, 5, 5) == 25); 1233 try testing.expect(mulWide(i8, 5, -5) == -25); 1234 try testing.expect(mulWide(u8, 100, 100) == 10000); 1235 } 1236 1237 /// See also `CompareOperator`. 1238 pub const Order = enum { 1239 /// Less than (`<`) 1240 lt, 1241 1242 /// Equal (`==`) 1243 eq, 1244 1245 /// Greater than (`>`) 1246 gt, 1247 1248 pub fn invert(self: Order) Order { 1249 return switch (self) { 1250 .lt => .gt, 1251 .eq => .eq, 1252 .gt => .lt, 1253 }; 1254 } 1255 1256 pub fn compare(self: Order, op: CompareOperator) bool { 1257 return switch (self) { 1258 .lt => switch (op) { 1259 .lt => true, 1260 .lte => true, 1261 .eq => false, 1262 .gte => false, 1263 .gt => false, 1264 .neq => true, 1265 }, 1266 .eq => switch (op) { 1267 .lt => false, 1268 .lte => true, 1269 .eq => true, 1270 .gte => true, 1271 .gt => false, 1272 .neq => false, 1273 }, 1274 .gt => switch (op) { 1275 .lt => false, 1276 .lte => false, 1277 .eq => false, 1278 .gte => true, 1279 .gt => true, 1280 .neq => true, 1281 }, 1282 }; 1283 } 1284 }; 1285 1286 /// Given two numbers, this function returns the order they are with respect to each other. 1287 pub fn order(a: anytype, b: anytype) Order { 1288 if (a == b) { 1289 return .eq; 1290 } else if (a < b) { 1291 return .lt; 1292 } else if (a > b) { 1293 return .gt; 1294 } else { 1295 unreachable; 1296 } 1297 } 1298 1299 /// See also `Order`. 1300 pub const CompareOperator = enum { 1301 /// Less than (`<`) 1302 lt, 1303 /// Less than or equal (`<=`) 1304 lte, 1305 /// Equal (`==`) 1306 eq, 1307 /// Greater than or equal (`>=`) 1308 gte, 1309 /// Greater than (`>`) 1310 gt, 1311 /// Not equal (`!=`) 1312 neq, 1313 }; 1314 1315 /// This function does the same thing as comparison operators, however the 1316 /// operator is a runtime-known enum value. Works on any operands that 1317 /// support comparison operators. 1318 pub fn compare(a: anytype, op: CompareOperator, b: anytype) bool { 1319 return switch (op) { 1320 .lt => a < b, 1321 .lte => a <= b, 1322 .eq => a == b, 1323 .neq => a != b, 1324 .gt => a > b, 1325 .gte => a >= b, 1326 }; 1327 } 1328 1329 test "compare between signed and unsigned" { 1330 try testing.expect(compare(@as(i8, -1), .lt, @as(u8, 255))); 1331 try testing.expect(compare(@as(i8, 2), .gt, @as(u8, 1))); 1332 try testing.expect(!compare(@as(i8, -1), .gte, @as(u8, 255))); 1333 try testing.expect(compare(@as(u8, 255), .gt, @as(i8, -1))); 1334 try testing.expect(!compare(@as(u8, 255), .lte, @as(i8, -1))); 1335 try testing.expect(compare(@as(i8, -1), .lt, @as(u9, 255))); 1336 try testing.expect(!compare(@as(i8, -1), .gte, @as(u9, 255))); 1337 try testing.expect(compare(@as(u9, 255), .gt, @as(i8, -1))); 1338 try testing.expect(!compare(@as(u9, 255), .lte, @as(i8, -1))); 1339 try testing.expect(compare(@as(i9, -1), .lt, @as(u8, 255))); 1340 try testing.expect(!compare(@as(i9, -1), .gte, @as(u8, 255))); 1341 try testing.expect(compare(@as(u8, 255), .gt, @as(i9, -1))); 1342 try testing.expect(!compare(@as(u8, 255), .lte, @as(i9, -1))); 1343 try testing.expect(compare(@as(u8, 1), .lt, @as(u8, 2))); 1344 try testing.expect(@bitCast(u8, @as(i8, -1)) == @as(u8, 255)); 1345 try testing.expect(!compare(@as(u8, 255), .eq, @as(i8, -1))); 1346 try testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1))); 1347 } 1348 1349 test "order" { 1350 try testing.expect(order(0, 0) == .eq); 1351 try testing.expect(order(1, 0) == .gt); 1352 try testing.expect(order(-1, 0) == .lt); 1353 } 1354 1355 test "order.invert" { 1356 try testing.expect(Order.invert(order(0, 0)) == .eq); 1357 try testing.expect(Order.invert(order(1, 0)) == .lt); 1358 try testing.expect(Order.invert(order(-1, 0)) == .gt); 1359 } 1360 1361 test "order.compare" { 1362 try testing.expect(order(-1, 0).compare(.lt)); 1363 try testing.expect(order(-1, 0).compare(.lte)); 1364 try testing.expect(order(0, 0).compare(.lte)); 1365 try testing.expect(order(0, 0).compare(.eq)); 1366 try testing.expect(order(0, 0).compare(.gte)); 1367 try testing.expect(order(1, 0).compare(.gte)); 1368 try testing.expect(order(1, 0).compare(.gt)); 1369 try testing.expect(order(1, 0).compare(.neq)); 1370 } 1371 1372 test "math.comptime" { 1373 const v = comptime (sin(@as(f32, 1)) + ln(@as(f32, 5))); 1374 try testing.expect(v == sin(@as(f32, 1)) + ln(@as(f32, 5))); 1375 } 1376 1377 /// Returns a mask of all ones if value is true, 1378 /// and a mask of all zeroes if value is false. 1379 /// Compiles to one instruction for register sized integers. 1380 pub inline fn boolMask(comptime MaskInt: type, value: bool) MaskInt { 1381 if (@typeInfo(MaskInt) != .Int) 1382 @compileError("boolMask requires an integer mask type."); 1383 1384 if (MaskInt == u0 or MaskInt == i0) 1385 @compileError("boolMask cannot convert to u0 or i0, they are too small."); 1386 1387 // The u1 and i1 cases tend to overflow, 1388 // so we special case them here. 1389 if (MaskInt == u1) return @boolToInt(value); 1390 if (MaskInt == i1) { 1391 // The @as here is a workaround for #7950 1392 return @bitCast(i1, @as(u1, @boolToInt(value))); 1393 } 1394 1395 return -%@intCast(MaskInt, @boolToInt(value)); 1396 } 1397 1398 test "boolMask" { 1399 const runTest = struct { 1400 fn runTest() !void { 1401 try testing.expectEqual(@as(u1, 0), boolMask(u1, false)); 1402 try testing.expectEqual(@as(u1, 1), boolMask(u1, true)); 1403 1404 try testing.expectEqual(@as(i1, 0), boolMask(i1, false)); 1405 try testing.expectEqual(@as(i1, -1), boolMask(i1, true)); 1406 1407 try testing.expectEqual(@as(u13, 0), boolMask(u13, false)); 1408 try testing.expectEqual(@as(u13, 0x1FFF), boolMask(u13, true)); 1409 1410 try testing.expectEqual(@as(i13, 0), boolMask(i13, false)); 1411 try testing.expectEqual(@as(i13, -1), boolMask(i13, true)); 1412 1413 try testing.expectEqual(@as(u32, 0), boolMask(u32, false)); 1414 try testing.expectEqual(@as(u32, 0xFFFF_FFFF), boolMask(u32, true)); 1415 1416 try testing.expectEqual(@as(i32, 0), boolMask(i32, false)); 1417 try testing.expectEqual(@as(i32, -1), boolMask(i32, true)); 1418 } 1419 }.runTest; 1420 try runTest(); 1421 comptime try runTest(); 1422 }