From 7a592cbb31f351627e7d508de9fafacfa65404cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20D=C3=ADaz?= Date: Fri, 16 Feb 2024 13:11:49 -0300 Subject: [PATCH 1/3] port math.round from Golang --- core/math/math.odin | 103 +++++++++++----- tests/core/math/test_core_math.odin | 181 ++++++++++++++++++++++++++++ 2 files changed, 254 insertions(+), 30 deletions(-) diff --git a/core/math/math.odin b/core/math/math.odin index 7fdbcba04..982f4b984 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -644,42 +644,85 @@ trunc :: proc{ } @(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 + + 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) -round_f32 :: proc "contextless" (x: f32) -> f32 { - 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) -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) -} -@(require_results) -round_f64le :: proc "contextless" (x: f64le) -> f64le { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(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_f16, round_f16le, round_f16be, round_f32, round_f32le, round_f32be, diff --git a/tests/core/math/test_core_math.odin b/tests/core/math/test_core_math.odin index 30e1875c0..df989bff6 100644 --- a/tests/core/math/test_core_math.odin +++ b/tests/core/math/test_core_math.odin @@ -19,6 +19,10 @@ main :: proc() { test_trunc_f32(&t) test_trunc_f64(&t) + test_round_f16(&t) + test_round_f32(&t) + test_round_f64(&t) + test_nan(&t) test_acos(&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)) } +@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{ 4.9790119248836735e+00, From cd61251d398bda34075c3b9c7d98e5e985dd6ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20D=C3=ADaz?= Date: Sat, 17 Feb 2024 07:25:33 -0300 Subject: [PATCH 2/3] add Go license --- core/math/math.odin | 92 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/core/math/math.odin b/core/math/math.odin index 982f4b984..570c2d255 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -645,6 +645,36 @@ trunc :: proc{ @(require_results) round_f16 :: proc "contextless" (x: f16) -> f16 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F16_MASK shift :: F16_SHIFT bias :: F16_BIAS @@ -672,6 +702,36 @@ round_f16 :: proc "contextless" (x: f16) -> f16 { @(require_results) round_f32 :: proc "contextless" (x: f32) -> f32 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F32_MASK shift :: F32_SHIFT bias :: F32_BIAS @@ -699,6 +759,36 @@ round_f32 :: proc "contextless" (x: f32) -> f32 { @(require_results) round_f64 :: proc "contextless" (x: f64) -> f64 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F64_MASK shift :: F64_SHIFT bias :: F64_BIAS @@ -2396,4 +2486,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) \ No newline at end of file +QNAN_F64 :: f64(0h7FF8_0000_0000_0001) From 23cd64ec357d4ba66476878dbe6cc63f71dac0a9 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 29 Feb 2024 23:39:18 +0100 Subject: [PATCH 3/3] reduce dynamic library dependencies for macos release --- .github/workflows/nightly.yml | 8 ++++++-- build_odin.sh | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 0a344ebf1..709f968a7 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -83,7 +83,9 @@ jobs: TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin - run: make nightly + # These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to + # not link with libunwind bundled with LLVM but link with libunwind on the system. + run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly - name: Bundle run: | mkdir dist @@ -117,7 +119,9 @@ jobs: TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin - run: make nightly + # These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to + # not link with libunwind bundled with LLVM but link with libunwind on the system. + run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly - name: Bundle run: | mkdir dist diff --git a/build_odin.sh b/build_odin.sh index 589aeb550..fab6c5fd1 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -63,8 +63,7 @@ Darwin) fi CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" - LDFLAGS="$LDFLAGS -liconv -ldl -framework System" - LDFLAGS="$LDFLAGS -lLLVM-C" + LDFLAGS="$LDFLAGS -liconv -ldl -framework System -lLLVM" ;; FreeBSD) CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)"