Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
hl_real.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_HL_REAL_H
21#define DT_KERNELS_HL_REAL_H
22
23#include "compensated.h" // csum_two_sum(): Knuth's two-sum, one copy for both headers
24
25// hl_real_t: the arithmetic of the sparse Cholesky solvers (highlights_sparse.cl), which need
26// more than single precision -- the biharmonic systems they factor are conditioned like h^-4,
27// about 3e8 at the DT_HL_SPARSE_MAX cap, and a float factorization loses every digit and
28// hits a negative pivot. It is one of two things, and the HOST decides which, per device:
29//
30// DT_DEVICE_FP64 defined -> native double. The host sets -DDT_DEVICE_FP64=1 from
31// CL_DEVICE_DOUBLE_FP_CONFIG (common/opencl.c), the same way
32// it sets NVIDIA_SM_20. Every device with real fp64 keeps the
33// arithmetic it always had, bit for bit.
34// otherwise -> double-float: a float2 (hi, lo), |lo| <= ulp(hi)/2, carrying
35// ~48 bits of mantissa out of two 24-bit floats. Knuth two-sum,
36// Dekker/fma two-product, Newton division and square root.
37// Apple's OpenCL-on-Metal has no fp64 at all; this is what lets
38// the solver kernels build there.
39//
40// Do NOT decide this on the cl_khr_fp64 preprocessor macro: Apple's runtime DEFINES it without
41// having the extension, the program then builds, and every kernel touching a double fails at
42// clCreateKernel (-48). Measured on Apple M1, see doc/highlights-reconstruction.md.
43//
44// Both representations are 8 bytes, so the host allocates the same buffers either way and
45// converts a double vector on its way in and out (_sp_cl_upload_real / _sp_cl_read_real in
46// src/math/sparse_cholesky_cl.h). The two-product relies on fma() being a fused, exactly
47// rounded operation; two-sum has no multiply, so -cl-mad-enable cannot contract it, but
48// reassociating math options fold it to plain float -- see compensated.h.
49
50#ifdef DT_DEVICE_FP64
51
52// The host has read real fp64 off the device, so the extension macros can be trusted here.
53// cl_amd_fp64 is the older AMD extension some devices carry instead of cl_khr_fp64.
54#if defined(cl_khr_fp64)
55#pragma OPENCL EXTENSION cl_khr_fp64 : enable
56#elif defined(cl_amd_fp64)
57#pragma OPENCL EXTENSION cl_amd_fp64 : enable
58#endif
59typedef double hl_real_t;
60
61static inline hl_real_t hl_real(const float x) { return (double)x; }
62static inline float hl_float(const hl_real_t x) { return (float)x; }
63static inline hl_real_t hl_zero(void) { return 0.0; }
64static inline hl_real_t hl_add(const hl_real_t a, const hl_real_t b) { return a + b; }
65static inline hl_real_t hl_sub(const hl_real_t a, const hl_real_t b) { return a - b; }
66static inline hl_real_t hl_mul(const hl_real_t a, const hl_real_t b) { return a * b; }
67static inline hl_real_t hl_div(const hl_real_t a, const hl_real_t b) { return a / b; }
68static inline hl_real_t hl_sqrt(const hl_real_t a) { return sqrt(a); }
69
70#else // double-float
71
72typedef float2 hl_real_t;
73
74// csum_two_sum(a, b), valid only when |a| >= |b| (Dekker): one operation fewer
75static inline float2 hl_quick_two_sum(const float a, const float b)
76{
77 const float s = a + b;
78 return (float2)(s, b - (s - a));
79}
80
81// (p, e) with p = fl(a * b) and p + e == a * b exactly, through the fused multiply-add
82static inline float2 hl_two_prod(const float a, const float b)
83{
84 const float p = a * b;
85 return (float2)(p, fma(a, b, -p));
86}
87
88static inline hl_real_t hl_real(const float x) { return (float2)(x, 0.f); }
89static inline float hl_float(const hl_real_t x) { return x.x + x.y; }
90static inline hl_real_t hl_zero(void) { return (float2)(0.f, 0.f); }
91
92static inline hl_real_t hl_add(const hl_real_t a, const hl_real_t b)
93{
94 float2 s = csum_two_sum(a.x, b.x);
95 const float2 t = csum_two_sum(a.y, b.y);
96 s.y += t.x;
97 s = hl_quick_two_sum(s.x, s.y);
98 s.y += t.y;
99 return hl_quick_two_sum(s.x, s.y);
100}
101
102static inline hl_real_t hl_sub(const hl_real_t a, const hl_real_t b)
103{
104 return hl_add(a, (float2)(-b.x, -b.y));
105}
106
107static inline hl_real_t hl_mul(const hl_real_t a, const hl_real_t b)
108{
109 float2 p = hl_two_prod(a.x, b.x);
110 p.y += a.x * b.y + a.y * b.x;
111 return hl_quick_two_sum(p.x, p.y);
112}
113
114// a / b: a float quotient corrected twice by the double-float remainder
115static inline hl_real_t hl_div(const hl_real_t a, const hl_real_t b)
116{
117 const float q1 = a.x / b.x;
118 hl_real_t r = hl_sub(a, hl_mul(b, hl_real(q1)));
119 const float q2 = r.x / b.x;
120 r = hl_sub(r, hl_mul(b, hl_real(q2)));
121 const float q3 = r.x / b.x;
122 const float2 q = hl_quick_two_sum(q1, q2);
123 return hl_add(q, hl_real(q3));
124}
125
126// sqrt(a): a float root corrected once by Newton. A non-positive argument goes through the
127// float sqrt so that zero stays zero and a negative pivot becomes NaN, as it does in double --
128// the host validates the solution for finiteness and takes the same fallback either way.
129static inline hl_real_t hl_sqrt(const hl_real_t a)
130{
131 if(a.x <= 0.f) return (float2)(sqrt(a.x), 0.f);
132 const float s = sqrt(a.x);
133 const hl_real_t r = hl_sub(a, hl_two_prod(s, s));
134 return hl_quick_two_sum(s, r.x / (2.f * s));
135}
136
137#endif // DT_DEVICE_FP64
138
139#endif // DT_KERNELS_HL_REAL_H
static const float x
const int t
static float2 csum_two_sum(const float a, const float b)
Definition compensated.h:44
static hl_real_t hl_real(const float x)
Definition hl_real.h:88
static hl_real_t hl_mul(const hl_real_t a, const hl_real_t b)
Definition hl_real.h:107
static hl_real_t hl_div(const hl_real_t a, const hl_real_t b)
Definition hl_real.h:115
static hl_real_t hl_add(const hl_real_t a, const hl_real_t b)
Definition hl_real.h:92
static hl_real_t hl_zero(void)
Definition hl_real.h:90
float2 hl_real_t
Definition hl_real.h:72
static float2 hl_quick_two_sum(const float a, const float b)
Definition hl_real.h:75
static hl_real_t hl_sub(const hl_real_t a, const hl_real_t b)
Definition hl_real.h:102
static hl_real_t hl_sqrt(const hl_real_t a)
Definition hl_real.h:129
static float2 hl_two_prod(const float a, const float b)
Definition hl_real.h:82
static float hl_float(const hl_real_t x)
Definition hl_real.h:89
const float r