Browse Source

Merge branch 'master' of https://github.com/odin-lang/Odin

gingerBill 1 year ago
parent
commit
6564ce0fb0
4 changed files with 129 additions and 4 deletions
  1. 49 1
      core/math/math.odin
  2. 5 2
      tests/internal/Makefile
  3. 2 1
      tests/internal/build.bat
  4. 73 0
      tests/internal/test_pow.odin

+ 49 - 1
core/math/math.odin

@@ -203,7 +203,55 @@ 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.
+		// Note that pow(2, -1075) returns 0h1 on Windows and 0h0 on macOS & Linux.
+		return 0h00000000_00000000
+	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 +2350,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)

+ 5 - 2
tests/internal/Makefile

@@ -1,9 +1,12 @@
 ODIN=../../odin
 
-all: rtti_test map_test
+all: rtti_test map_test pow_test
 
 rtti_test:
 	$(ODIN) run test_rtti.odin -file -vet -strict-style -o:minimal
 
 map_test:
-	$(ODIN) run test_map.odin -file -vet -strict-style -o:minimal
+	$(ODIN) run test_map.odin -file -vet -strict-style -o:minimal
+
+pow_test:
+	$(ODIN) run test_pow.odin -file -vet -strict-style -o:minimal

+ 2 - 1
tests/internal/build.bat

@@ -2,4 +2,5 @@
 set PATH_TO_ODIN==..\..\odin
 rem %PATH_TO_ODIN% run test_rtti.odin -file -vet -strict-style -o:minimal || exit /b
 %PATH_TO_ODIN% run test_map.odin  -file -vet -strict-style -o:minimal || exit /b
-rem -define:SEED=42
+rem -define:SEED=42
+%PATH_TO_ODIN% run test_pow.odin  -file -vet -strict-style -o:minimal || exit /b

+ 73 - 0
tests/internal/test_pow.odin

@@ -0,0 +1,73 @@
+package test_internal_math_pow
+
+import "core:fmt"
+import "core:math"
+import "core:os"
+import "core:testing"
+
+@test
+pow_test :: proc(t: ^testing.T) {
+	for exp in -2000..=2000 {
+		{
+			v1 := math.pow(2, f64(exp))
+			v2 := math.pow2_f64(exp)
+			_v1 := transmute(u64)v1
+			_v2 := transmute(u64)v2
+			if exp == -1075 && ODIN_OS == .Windows {
+				// LLVM on Windows returns 0h00000000_00000001 for pow(2, -1075),
+				// unlike macOS and Linux where it returns 0h00000000_00000000
+				// pow2_f64 returns the same float on all platforms because it isn't this stupid
+				_v1 = 0h00000000_00000000
+			}
+			expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f64(%d) == math.pow(2, %d) (= %16x), got %16x", exp, exp, _v1, _v2))
+		}
+		{
+			v1 := math.pow(2, f32(exp))
+			v2 := math.pow2_f32(exp)
+			_v1 := transmute(u32)v1
+			_v2 := transmute(u32)v2
+			expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f32(%d) == math.pow(2, %d) (= %08x), got %08x", exp, exp, _v1, _v2))
+		}
+		{
+			v1 := math.pow(2, f16(exp))
+			v2 := math.pow2_f16(exp)
+			_v1 := transmute(u16)v1
+			_v2 := transmute(u16)v2
+			expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f16(%d) == math.pow(2, %d) (= %04x), got %04x", exp, exp, _v1, _v2))
+		}
+	}
+}
+
+// -------- -------- -------- -------- -------- -------- -------- -------- -------- --------
+
+main :: proc() {
+	t := testing.T{}
+
+	pow_test(&t)
+
+	fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
+	if TEST_fail > 0 {
+		os.exit(1)
+	}
+}
+
+TEST_count := 0
+TEST_fail  := 0
+
+when ODIN_TEST {
+	expect  :: testing.expect
+	log     :: testing.log
+} else {
+	expect  :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
+		TEST_count += 1
+		if !condition {
+			TEST_fail += 1
+			fmt.printf("[%v] %v\n", loc, message)
+			return
+		}
+	}
+	log     :: proc(t: ^testing.T, v: any, loc := #caller_location) {
+		fmt.printf("[%v] ", loc)
+		fmt.printf("log: %v\n", v)
+	}
+}