Browse Source

port math.round from Golang

Juan Ignacio Díaz 1 year ago
parent
commit
7a592cbb31
2 changed files with 254 additions and 30 deletions
  1. 73 30
      core/math/math.odin
  2. 181 0
      tests/core/math/test_core_math.odin

+ 73 - 30
core/math/math.odin

@@ -644,42 +644,85 @@ trunc :: proc{
 }
 }
 
 
 @(require_results)
 @(require_results)
-round_f16   :: proc "contextless" (x: f16)   -> f16 {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
-@(require_results)
-round_f16le :: proc "contextless" (x: f16le) -> f16le {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
-@(require_results)
-round_f16be :: proc "contextless" (x: f16be) -> f16be {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
+round_f16 :: proc "contextless" (x: f16) -> f16 {
+	mask  :: F16_MASK
+	shift :: F16_SHIFT
+	bias  :: F16_BIAS
 
 
-@(require_results)
-round_f32   :: proc "contextless" (x: f32)   -> f32 {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
-@(require_results)
-round_f32le :: proc "contextless" (x: f32le) -> f32le {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
-@(require_results)
-round_f32be :: proc "contextless" (x: f32be) -> f32be {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
-}
-@(require_results)
-round_f64   :: proc "contextless" (x: f64)   -> f64 {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
+	bits := transmute(u16)x
+	e := (bits >> shift) & mask
+
+	if e < bias {
+		bits &= 0x8000
+		if e == bias - 1 {
+			bits |= transmute(u16)f16(1)
+		}
+	} else if e < bias + shift {
+		half     :: 1 << (shift - 1)
+		mantissa :: (1 << shift) - 1
+		e -= bias
+		bits += half >> e
+		bits &~= mantissa >> e
+	}
+
+	return transmute(f16)bits
 }
 }
+@(require_results) round_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(round_f16(f16(x))) }
+@(require_results) round_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(round_f16(f16(x))) }
+
 @(require_results)
 @(require_results)
-round_f64le :: proc "contextless" (x: f64le) -> f64le {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
+round_f32 :: proc "contextless" (x: f32) -> f32 {
+	mask  :: F32_MASK
+	shift :: F32_SHIFT
+	bias  :: F32_BIAS
+
+	bits := transmute(u32)x
+	e := (bits >> shift) & mask
+
+	if e < bias {
+		bits &= 0x8000_0000
+		if e == bias - 1 {
+			bits |= transmute(u32)f32(1)
+		}
+	} else if e < bias + shift {
+		half     :: 1 << (shift - 1)
+		mantissa :: (1 << shift) - 1
+		e -= bias
+		bits += half >> e
+		bits &~= mantissa >> e
+	}
+
+	return transmute(f32)bits
 }
 }
+@(require_results) round_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(round_f32(f32(x))) }
+@(require_results) round_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(round_f32(f32(x))) }
+
 @(require_results)
 @(require_results)
-round_f64be :: proc "contextless" (x: f64be) -> f64be {
-	return ceil(x - 0.5) if x < 0 else floor(x + 0.5)
+round_f64 :: proc "contextless" (x: f64) -> f64 {
+	mask  :: F64_MASK
+	shift :: F64_SHIFT
+	bias  :: F64_BIAS
+
+	bits := transmute(u64)x
+	e := (bits >> shift) & mask
+
+	if e < bias {
+		bits &= 0x8000_0000_0000_0000
+		if e == bias - 1 {
+			bits |= transmute(u64)f64(1)
+		}
+	} else if e < bias + shift {
+		half     :: 1 << (shift - 1)
+		mantissa :: (1 << shift) - 1
+		e -= bias
+		bits += half >> e
+		bits &~= mantissa >> e
+	}
+
+	return transmute(f64)bits
 }
 }
+@(require_results) round_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(round_f64(f64(x))) }
+@(require_results) round_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(round_f64(f64(x))) }
 round :: proc{
 round :: proc{
 	round_f16, round_f16le, round_f16be,
 	round_f16, round_f16le, round_f16be,
 	round_f32, round_f32le, round_f32be,
 	round_f32, round_f32le, round_f32be,

+ 181 - 0
tests/core/math/test_core_math.odin

@@ -19,6 +19,10 @@ main :: proc() {
 	test_trunc_f32(&t)
 	test_trunc_f32(&t)
 	test_trunc_f64(&t)
 	test_trunc_f64(&t)
 
 
+	test_round_f16(&t)
+	test_round_f32(&t)
+	test_round_f64(&t)
+
 	test_nan(&t)
 	test_nan(&t)
 	test_acos(&t)
 	test_acos(&t)
 	test_acosh(&t)
 	test_acosh(&t)
@@ -307,6 +311,183 @@ test_trunc_f64 :: proc(t: ^testing.T) {
 	tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
 	tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
 }
 }
 
 
+@test
+test_round_f16 :: proc(t: ^testing.T) {
+	r, v: f16
+
+	Datum :: struct {
+		i: int,
+		v: f16,
+		e: f16,
+	}
+	@static data := []Datum{
+		{ 0, 10.5, 11 },
+		{ 1, -10.5, -11 },
+
+		{ 2, math.F16_MAX, math.F16_MAX },
+		{ 3, -math.F16_MAX, -math.F16_MAX },
+		{ 4, math.F16_MIN, 0.0 },
+		{ 5, -math.F16_MIN, -0.0 },
+		{ 6, 0.0, 0.0 },
+		{ 7, -0.0, -0.0 },
+		{ 8, 1, 1 },
+		{ 9, -1, -1 },
+		{ 10, math.INF_F16, math.INF_F16 },
+		{ 11, math.NEG_INF_F16, math.NEG_INF_F16 },
+
+		/* From https://en.wikipedia.org/wiki/Half-precision_floating-point_format */
+		{ 12, 0h3C01, 1 }, // 0x1.004p+0 (smallest > 1)
+		{ 13, -0h3C01, -1 },
+		{ 14, 0h3BFF, 1 }, // 0x1.ffcp-1 (largest < 1)
+		{ 15, -0h3BFF, -1 },
+		{ 16, 0h0001, 0.0 }, // 0x0.004p-14 (smallest subnormal)
+		{ 17, -0h0001, -0.0 },
+		{ 18, 0h03FF, 0.0 }, // 0x0.ffcp-14 (largest subnormal)
+		{ 19, -0h03FF, -0.0 },
+
+		{ 20, 0hC809, -8 }, // -0x1.024p+3
+		{ 21, 0h4458, 4 }, // 0x1.16p+2
+	}
+
+	for d, i in data {
+		assert(i == d.i)
+		r = math.round_f16(d.v)
+		tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
+	}
+
+	v = math.SNAN_F16
+	r = math.round_f16(v)
+	tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+
+	v = math.QNAN_F16
+	r = math.round_f16(v)
+	tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+}
+
+@test
+test_round_f32 :: proc(t: ^testing.T) {
+	r, v: f32
+
+	Datum :: struct {
+		i: int,
+		v: f32,
+		e: f32,
+	}
+	@static data := []Datum{
+		{ 0, 10.5, 11 },
+		{ 1, -10.5, -11 },
+
+		{ 2, math.F32_MAX, math.F32_MAX },
+		{ 3, -math.F32_MAX, -math.F32_MAX },
+		{ 4, math.F32_MIN, 0.0 },
+		{ 5, -math.F32_MIN, -0.0 },
+		{ 6, 0.0, 0.0 },
+		{ 7, -0.0, -0.0 },
+		{ 8, 1, 1 },
+		{ 9, -1, -1 },
+		{ 10, math.INF_F32, math.INF_F32 },
+		{ 11, math.NEG_INF_F32, math.NEG_INF_F32 },
+
+		/* From https://en.wikipedia.org/wiki/Single-precision_floating-point_format */
+		{ 12, 0h3F80_0001, 1 }, // 0x1.000002p+0 (smallest > 1)
+		{ 13, -0h3F80_0001, -1 },
+		{ 14, 0h3F7F_FFFF, 1 }, // 0x1.fffffep-1 (largest < 1)
+		{ 15, -0h3F7F_FFFF, -1 },
+		{ 16, 0h0000_0001, 0.0 }, // 0x0.000002p-126 (smallest subnormal)
+		{ 17, -0h0000_0001, -0.0 },
+		{ 18, 0h007F_FFFF, 0.0 }, // 0x0.fffffep-126 (largest subnormal)
+		{ 19, -0h007F_FFFF, -0.0 },
+
+		/* From libc-test src/math/sanity/roundf.h */
+		{ 20, 0hC101_11D0, -8 }, // -0x1.0223ap+3
+		{ 21, 0h408B_0C34, 4 }, // 0x1.161868p+2
+		{ 22, 0hC106_1A5A, -8 }, // -0x1.0c34b4p+3
+		{ 23, 0hC0D1_0378, -7 }, // -0x1.a206fp+2
+		{ 24, 0h4114_45DE, 9 }, // 0x1.288bbcp+3
+		{ 25, 0h3F29_77E8, 1.0 }, // 0x1.52efdp-1
+		{ 26, 0hBED0_2E64, -0.0 }, // -0x1.a05cc8p-2
+		{ 27, 0h3F0F_CF7D, 1.0 }, // 0x1.1f9efap-1
+		{ 28, 0h3F46_2ED8, 1.0 }, // 0x1.8c5dbp-1
+		{ 29, 0hBF2D_C375, -1.0 }, // -0x1.5b86eap-1
+	}
+
+	for d, i in data {
+		assert(i == d.i)
+		r = math.round_f32(d.v)
+		tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
+	}
+
+	v = math.SNAN_F32
+	r = math.round_f32(v)
+	tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+
+	v = math.QNAN_F32
+	r = math.round_f32(v)
+	tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+}
+
+@test
+test_round_f64 :: proc(t: ^testing.T) {
+	r, v: f64
+
+	Datum :: struct {
+		i: int,
+		v: f64,
+		e: f64,
+	}
+	data := []Datum{
+		{ 0, 10.5, 11 }, // Issue #1574 fract in linalg/glm is broken
+		{ 1, -10.5, -11 },
+
+		{ 2, math.F64_MAX, math.F64_MAX },
+		{ 3, -math.F64_MAX, -math.F64_MAX },
+		{ 4, math.F64_MIN, 0.0 },
+		{ 5, -math.F64_MIN, -0.0 },
+		{ 6, 0.0, 0.0 },
+		{ 7, -0.0, -0.0 },
+		{ 8, 1, 1 },
+		{ 9, -1, -1 },
+		{ 10, math.INF_F64, math.INF_F64 },
+		{ 11, math.NEG_INF_F64, math.NEG_INF_F64 },
+
+		/* From https://en.wikipedia.org/wiki/Double-precision_floating-point_format */
+		{ 12, 0h3FF0_0000_0000_0001, 1 }, // 0x1.0000000000001p+0 (smallest > 1)
+		{ 13, -0h3FF0_0000_0000_0001, -1 },
+		{ 14, 0h3FEF_FFFF_FFFF_FFFF, 1 }, // 0x1.fffffffffffffp-1 (largest < 1)
+		{ 15, -0h3FEF_FFFF_FFFF_FFFF, -1 },
+		{ 16, 0h0000_0000_0000_0001, 0.0 }, // 0x0.0000000000001p-1022 (smallest subnormal)
+		{ 17, -0h0000_0000_0000_0001, -0.0 },
+		{ 18, 0h000F_FFFF_FFFF_FFFF, 0.0 }, // 0x0.fffffffffffffp-1022 (largest subnormal)
+		{ 19, -0h000F_FFFF_FFFF_FFFF, -0.0 },
+
+		/* From libc-test src/math/sanity/round.h */
+		{ 20, 0hC020_2239_F3C6_A8F1, -8 }, // -0x1.02239f3c6a8f1p+3
+		{ 21, 0h4011_6186_8E18_BC67, 4 }, // 0x1.161868e18bc67p+2
+		{ 22, 0hC020_C34B_3E01_E6E7, -8 }, // -0x1.0c34b3e01e6e7p+3
+		{ 23, 0hC01A_206F_0A19_DCC4, -7 }, // -0x1.a206f0a19dcc4p+2
+		{ 24, 0h4022_88BB_B0D6_A1E6, 9 }, // 0x1.288bbb0d6a1e6p+3
+		{ 25, 0h3FE5_2EFD_0CD8_0497, 1.0 }, // 0x1.52efd0cd80497p-1
+		{ 26, 0hBFDA_05CC_7544_81D1, -0.0 }, // -0x1.a05cc754481d1p-2
+		{ 27, 0h3FE1_F9EF_9347_45CB, 1.0 }, // 0x1.1f9ef934745cbp-1
+		{ 28, 0h3FE8_C5DB_097F_7442, 1.0 }, // 0x1.8c5db097f7442p-1
+		{ 29, 0hBFE5_B86E_A811_8A0E, -1.0 }, // -0x1.5b86ea8118a0ep-1
+	}
+
+	for d, i in data {
+		assert(i == d.i)
+		r = math.round_f64(d.v)
+		tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e))
+	}
+
+	v = math.SNAN_F64
+	r = math.round_f64(v)
+	tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+
+	v = math.QNAN_F64
+	r = math.round_f64(v)
+	tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r))
+}
+
 
 
 vf := []f64{
 vf := []f64{
 	4.9790119248836735e+00,
 	4.9790119248836735e+00,