aboutsummaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2023-11-04 22:14:44 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2023-11-04 22:14:44 +0100
commit620128046801eb6b5156e58e386d2b42a4e7ae68 (patch)
treec95a049f2d6537b22a2cefcbaa8d530bf95a2118 /core/math
parent5edb2c568840248f63de3fab58a02f134f182525 (diff)
Add math.pow2_f{16,32,64}, fast floating point 2^x where x is an integer.
Diffstat (limited to 'core/math')
-rw-r--r--core/math/math.odin49
1 files changed, 48 insertions, 1 deletions
diff --git a/core/math/math.odin b/core/math/math.odin
index 4215a8075..a5d86cffb 100644
--- a/core/math/math.odin
+++ b/core/math/math.odin
@@ -203,7 +203,54 @@ pow10_f64 :: proc "contextless" (n: f64) -> f64 {
return 0
}
+pow2_f64 :: proc(#any_int exp: int) -> (res: f64) {
+ switch {
+ case exp >= -1022 && exp <= 1023: // Normal
+ return transmute(f64)(u64(exp + F64_BIAS) << F64_SHIFT)
+ case exp < -1075: // Underflow
+ return f64(0)
+ case exp == -1075: // Underflow
+ return 0h00000000_00000001
+ case exp < -1022: // Denormal
+ x := u64(exp + (F64_SHIFT + 1) + F64_BIAS) << F64_SHIFT
+ return f64(1) / (1 << (F64_SHIFT + 1)) * transmute(f64)x
+ case exp > 1023: // Overflow, +Inf
+ return 0h7ff00000_00000000
+ }
+ unreachable()
+}
+pow2_f32 :: proc(#any_int exp: int) -> (res: f32) {
+ switch {
+ case exp >= -126 && exp <= 127: // Normal
+ return transmute(f32)(u32(exp + F32_BIAS) << F32_SHIFT)
+ case exp < -151: // Underflow
+ return f32(0)
+ case exp < -126: // Denormal
+ x := u32(exp + (F32_SHIFT + 1) + F32_BIAS) << F32_SHIFT
+ return f32(1) / (1 << (F32_SHIFT + 1)) * transmute(f32)x
+ case exp > 127: // Overflow, +Inf
+ return 0h7f80_0000
+ }
+ unreachable()
+}
+
+pow2_f16 :: proc(#any_int exp: int) -> (res: f16) {
+ switch {
+ case exp >= -14 && exp <= 15: // Normal
+ return transmute(f16)(u16(exp + F16_BIAS) << F16_SHIFT)
+ case exp < -25: // Underflow
+ return 0h0000
+ case exp == -25: // Underflow
+ return 0h0001
+ case exp < -14: // Denormal
+ x := u16(exp + (F16_SHIFT + 1) + F16_BIAS) << F16_SHIFT
+ return f16(1) / (1 << (F16_SHIFT + 1)) * transmute(f16)x
+ case exp > 15: // Overflow, +Inf
+ return 0h7c00
+ }
+ unreachable()
+}
@(require_results)
ldexp_f64 :: proc "contextless" (val: f64, exp: int) -> f64 {
@@ -2302,4 +2349,4 @@ INF_F64 :: f64(0h7FF0_0000_0000_0000)
NEG_INF_F64 :: f64(0hFFF0_0000_0000_0000)
SNAN_F64 :: f64(0h7FF0_0000_0000_0001)
-QNAN_F64 :: f64(0h7FF8_0000_0000_0001)
+QNAN_F64 :: f64(0h7FF8_0000_0000_0001) \ No newline at end of file