diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2022-01-31 18:39:07 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2022-02-09 15:33:46 +0100 |
commit | 76bdbeb862f64aa8b8773b5183d28b3eb1bbfc8a (patch) | |
tree | fa34f1432339a2621f6195f307cf5e5a8a6f07cd /src/corelib/global/qfloat16.h | |
parent | df0b092d08028bde10ea0ffc1772a00e8062fd2d (diff) |
Document float-to-qfloat16's rounding more carefully
Having just spent another afternoon wrapping my head around why this
code is correct, I chose to save the next person (quite possibly me)
faced with it the slowest half of the effort that went into doing so.
This amends commit d3ff95dcb84861e8f42b480910d822b4ca8715b1
Change-Id: If0fcd2fb1fff5e99b1046d07039169e928fda9e4
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/corelib/global/qfloat16.h')
-rw-r--r-- | src/corelib/global/qfloat16.h | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h index a178563889..8361dd8764 100644 --- a/src/corelib/global/qfloat16.h +++ b/src/corelib/global/qfloat16.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Copyright (C) 2016 by Southwest Research Institute (R) ** Contact: http://www.qt-project.org/legal ** @@ -275,8 +275,12 @@ inline qfloat16::qfloat16(float f) noexcept if (mantissa) // keep nan from truncating to inf mantissa = qMax(1U << shift, mantissa); } else { - // round half to even + // Round half to even. First round up by adding one in the most + // significant bit we'll be discarding: mantissa += round; + // If the last bit we'll be keeping is now set, but all later bits are + // clear, we were at half and shouldn't have rounded up; decrement will + // clear this last kept bit. Any later set bit hides the decrement. if (mantissa & (1 << shift)) --mantissa; } |