Quellcode durchsuchen

Work around LLVM idiocy.

Jeroen van Rijn vor 1 Jahr
Ursprung
Commit
4cb0edc90b
3 geänderte Dateien mit 12 neuen und 5 gelöschten Zeilen
  1. 3 2
      core/math/math.odin
  2. BIN
      tests/internal/test_pow
  3. 9 3
      tests/internal/test_pow.odin

+ 3 - 2
core/math/math.odin

@@ -209,8 +209,9 @@ pow2_f64 :: proc(#any_int exp: int) -> (res: f64) {
 		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 == -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

BIN
tests/internal/test_pow


+ 9 - 3
tests/internal/test_pow.odin

@@ -13,21 +13,27 @@ pow_test :: proc(t: ^testing.T) {
 			v2 := math.pow2_f64(exp)
 			_v1 := transmute(u64)v1
 			_v2 := transmute(u64)v2
-			expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f64(%d) == math.pow(2, %d) (= %x), got %x", exp, exp, v1, 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) (= %x), got %x", exp, exp, v1, 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) (= %x), got %x", exp, exp, v1, v2))
+			expect(t, _v1 == _v2, fmt.tprintf("Expected math.pow2_f16(%d) == math.pow(2, %d) (= %04x), got %04x", exp, exp, _v1, _v2))
 		}
 	}
 }