Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
compensated.h
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4 Copyright (C) 2026 Miguel Moquillon.
5
6 Ansel is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Ansel is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with darktable. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef DT_KERNELS_COMPENSATED_H
21#define DT_KERNELS_COMPENSATED_H
22
23// Compensated single-precision summation, for the reductions that used to accumulate in
24// double and narrow their result to float on the way out. A sum is carried as a float2
25// (value, running error): every addition captures the bit its rounding lost through
26// Knuth's two-sum -- exact, s + e == a + b, whatever the magnitudes -- and folds it into
27// the error lane. The bound is a couple of ulp on the result, independent of the number of
28// terms, which is at least what the double gave once narrowed. Nothing here multiplies, so
29// -cl-mad-enable cannot contract any of it. It does rely on the compiler NOT reassociating:
30// under -cl-unsafe-math-optimizations or -cl-fast-relaxed-math clang folds the error term of
31// csum_two_sum() to 0.0 (measured, doc/opencl-math-accuracy.md), and dt_opencl_device_init()
32// migrates the per-device `building` key away from the old default that carried them.
33//
34// It exists because cl_khr_fp64 is an OPTIONAL OpenCL extension: Apple's runtime has none,
35// and the reductions in highlights_harmonic.cl / highlights_sparse.cl were the reason five
36// of that module's GPU stages fell back to the CPU there.
37
38static inline float2 csum_zero(void)
39{
40 return (float2)(0.f, 0.f);
41}
42
43// Knuth two-sum: (s, e) with s = fl(a + b) and s + e == a + b exactly.
44static inline float2 csum_two_sum(const float a, const float b)
45{
46 const float s = a + b;
47 const float bb = s - a;
48 const float e = (a - (s - bb)) + (b - bb);
49 return (float2)(s, e);
50}
51
52// acc + x, the rounding error of the addition kept in the error lane
53static inline float2 csum_add(const float2 acc, const float x)
54{
55 const float2 t = csum_two_sum(acc.x, x);
56 return (float2)(t.x, acc.y + t.y);
57}
58
59// merge two partial sums, for the tree reductions in local memory
60static inline float2 csum_merge(const float2 a, const float2 b)
61{
62 const float2 t = csum_two_sum(a.x, b.x);
63 return (float2)(t.x, a.y + b.y + t.y);
64}
65
66// the value of a compensated sum
67static inline float csum_value(const float2 acc)
68{
69 return acc.x + acc.y;
70}
71
72#endif // DT_KERNELS_COMPENSATED_H
static const float x
const int t
static float2 csum_merge(const float2 a, const float2 b)
Definition compensated.h:60
static float2 csum_zero(void)
Definition compensated.h:38
static float csum_value(const float2 acc)
Definition compensated.h:67
static float2 csum_add(const float2 acc, const float x)
Definition compensated.h:53
static float2 csum_two_sum(const float a, const float b)
Definition compensated.h:44