From ea357c2247a7e8f3826cde9c4e812040ad1b5323 Mon Sep 17 00:00:00 2001 From: Sander van Rossen Date: Sat, 21 Nov 2026 22:27:55 +0100 Subject: [PATCH] Round half floats up into the next power of two, and read small ones at their value FloatToHalf rounded the mantissa up and then masked it to 10 bits, so a value that rounded past the largest mantissa lost the carry: 0.99995 became 0.5, 1.9999 became 1.0, and 65530 became 32768. A gradient's rotation is packed this way, so one at an angle near an axis pointed the wrong way. HalfToFloat read subnormal halves as half their value. - FloatToHalf adds the rounding to exponent and mantissa together: it carries into the exponent, and to infinity past the largest half. - HalfToFloat normalizes a subnormal with the right exponent. - Tests: rounding up carries into the exponent, rounding past the largest half is infinity, subnormals read as their value, and every finite half comes back as itself through a float. --- CHANGELOG.md | 1 + src/gui_core/gui_base_types.cpp | 17 ++++++------- tests/gui_base_types_test.cpp | 43 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80806973..2e902f91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -295,3 +295,4 @@ First public preview. The API is not stable yet. - `Panel::onChanged`: called after a frame in which the user pressed, released or dragged the mouse, scrolled, or typed in the panel, once its handlers have run. Hovering doesn't count. An app with several panels showing the same data rebuilds the others from it (`InvalidatePanel`), so their cached copies show the change. The showcase does it for panels of one kind, and for Viewports and Hierarchies, which both show the scene. - The library gives back the static IDs it makes. A panel `CreatePanel` made an ID for releases it when it closes: its tab, its window, its document, or a layout applied without it. `ClosePanel` closes a panel wherever it is, `ReleaseDockingLayout` closes a whole layout, and `CloseAllDocuments` closes a document set. `Cleanup` releases the application's own window IDs (menus, tooltips, pickers, toasts, popovers) and closes its torn-off panel windows. The showcase closes its documents at exit and holds no static ID when it's done. - Fixed: closing a docked panel with Ctrl+W, Ctrl+F4 or its tab menu's Close no longer frees what it holds while that frame still draws it. A viewport's scene drawn that frame read freed memory. A closed panel leaves its layout at once, and its callbacks are destroyed when the next frame starts. +- Fixed: `FloatToHalf` lost the carry when rounding up past the largest mantissa, so 0.99995 became 0.5 and 65530 became 32768; it now carries into the exponent, and to infinity past the largest half. A gradient at an angle near an axis, whose rotation is packed this way, pointed the wrong way. `HalfToFloat` read subnormal halves as half their value. diff --git a/src/gui_core/gui_base_types.cpp b/src/gui_core/gui_base_types.cpp index 5055fa1c..626d8c49 100644 --- a/src/gui_core/gui_base_types.cpp +++ b/src/gui_core/gui_base_types.cpp @@ -213,15 +213,11 @@ namespace tachyon } else { - // Normalized number - uint16_t hexp = uint16_t(newExp) << 10; - uint16_t hmant = uint16_t(mant >> 13); - - // Round to nearest - if (mant & 0x1000) - hmant++; - - return hsign | hexp | (hmant & 0x3FF); + // Normalized number, rounded to nearest. The rounding adds to exponent and + // mantissa together: past the largest mantissa it carries into the exponent (the + // next power of two), and past the largest exponent it gives infinity. + const uint16_t bitsOut = uint16_t((uint32_t(newExp) << 10) | (mant >> 13)); + return hsign | uint16_t(bitsOut + ((mant & 0x1000) ? 1 : 0)); } } @@ -254,7 +250,8 @@ namespace tachyon } mant &= 0x3FF; // remove leading 1 - fexp = (127 - 15 - shift) << 23; + // A subnormal half is mant * 2^-24: normalized, 1.m * 2^(-14 - shift). + fexp = (127 - 14 - shift) << 23; fmant = mant << 13; } } else if (exp == 31) diff --git a/tests/gui_base_types_test.cpp b/tests/gui_base_types_test.cpp index 9e63f4d2..d34446bd 100644 --- a/tests/gui_base_types_test.cpp +++ b/tests/gui_base_types_test.cpp @@ -193,4 +193,47 @@ namespace tachyon EXPECT_TRUE(std::isfinite(r.y)) << "Inverse() of a singular transform produced non-finite y"; } + // Half floats: the GPU's 16-bit floats, as the gradient rotation is packed. + + // A value that rounds up past the largest mantissa goes to the next power of two, not to + // half of it: the carry goes into the exponent. + TEST(HalfFloatTest, RoundingUpCarriesIntoTheExponent) + { + EXPECT_EQ(FloatToHalf(1.0f), 0x3C00); + EXPECT_EQ(FloatToHalf(0.99995f), 0x3C00) << "1.0, not 0.5"; + EXPECT_EQ(FloatToHalf(1.9999f), 0x4000) << "2.0, not 1.0"; + EXPECT_EQ(FloatToHalf(-1.9999f), 0xC000) << "-2.0"; + EXPECT_FLOAT_EQ(HalfToFloat(FloatToHalf(0.99995f)), 1.0f); + } + + // The smallest halves, below the normal range: a step of 2^-24. + TEST(HalfFloatTest, SubnormalsReadAsTheirValue) + { + EXPECT_FLOAT_EQ(HalfToFloat(0x0001), std::ldexp(1.0f, -24)); + EXPECT_FLOAT_EQ(HalfToFloat(0x0200), std::ldexp(1.0f, -15)); + EXPECT_FLOAT_EQ(HalfToFloat(0x03FF), std::ldexp(1023.0f, -24)); + EXPECT_FLOAT_EQ(HalfToFloat(0x8001), -std::ldexp(1.0f, -24)); + } + + // At the top of the range, rounding up past the largest half is infinity. + TEST(HalfFloatTest, RoundingPastTheLargestIsInfinity) + { + EXPECT_EQ(FloatToHalf(65504.0f), 0x7BFF) << "the largest half"; + EXPECT_EQ(FloatToHalf(65519.0f), 0x7BFF) << "rounds down to it"; + EXPECT_EQ(FloatToHalf(65530.0f), 0x7C00) << "rounds up, past it"; + EXPECT_EQ(FloatToHalf(-65530.0f), 0xFC00); + } + + // Every finite half comes back as itself through a float: subnormals too, which read as + // half their value before. + TEST(HalfFloatTest, EveryFiniteHalfRoundTrips) + { + for (uint32_t h = 0; h < 0x10000; ++h) + { + if ((h & 0x7C00) == 0x7C00) + continue; // infinity and NaN + const uint16_t half = uint16_t(h); + ASSERT_EQ(FloatToHalf(HalfToFloat(half)), half) << std::hex << "0x" << h; + } + } } \ No newline at end of file