Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
openmp_maths.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2020-2021 Aurélien PIERRE.
4 Copyright (C) 2021 Ralf Brown.
5 Copyright (C) 2022 Martin Bařinka.
6
7 darktable is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 darktable is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with darktable. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21
22#include <glib.h> // for inline
23#include <math.h> // for log, logf, powf
24
25#ifndef DT_MATH_OPENMP_MATHS_H
26#define DT_MATH_OPENMP_MATHS_H
27
28
29#if defined(_OPENMP) && !defined(_WIN32) && !defined(__GNUC__)
30
31#pragma omp declare simd
32extern float fmaxf(const float x, const float y);
33
34#pragma omp declare simd
35extern float fminf(const float x, const float y);
36
37#pragma omp declare simd
38extern float fabsf(const float x);
39
40#pragma omp declare simd
41extern float powf(const float x, const float y);
42
43#pragma omp declare simd
44extern float sqrtf(const float x);
45
46#pragma omp declare simd
47extern float cbrtf(const float x);
48
49#pragma omp declare simd
50extern float log2f(const float x);
51
52#pragma omp declare simd
53extern float exp2f(const float x);
54
55#pragma omp declare simd
56extern float log10f(const float x);
57
58#pragma omp declare simd
59extern float expf(const float x);
60
61#pragma omp declare simd
62extern float logf(const float x);
63
64#endif
65
66
67/* Bring our own optimized maths functions because Clang makes dumb shit */
68
70static inline float fast_exp10f(const float x)
71{
72 // we use the property : 10^x = exp(log(10) * x) = 2^(log(10) * x / log(2))
73 // max relative error over x = [0; 4] is 1.5617955706227326e-15
74 return exp2f(3.3219280948873626f * x);
75}
76
77// Since we are at it, write an optimized expf
79static inline float fast_expf(const float x)
80{
81 // we use the property : exp(x) = 2^(x / log(2))
82 // max relative error over x = [0; 4] is 5.246203046472202e-16
83 return exp2f(1.4426950408889634f * x);
84}
85
86
87__OMP_DECLARE_SIMD__(aligned(vector:16))
88static inline float v_maxf(const float vector[3])
89{
90 // Find the max over an RGB vector
91 return fmaxf(fmaxf(vector[0], vector[1]), vector[2]);
92}
93
94
95__OMP_DECLARE_SIMD__(aligned(vector:16))
96static inline float v_minf(const float vector[3])
97{
98 // Find the min over an RGB vector
99 return fminf(fminf(vector[0], vector[1]), vector[2]);
100}
101
102__OMP_DECLARE_SIMD__(aligned(vector:16))
103static inline float v_sumf(const float vector[3])
104{
105 return vector[0] + vector[1] + vector[2];
106}
107
108
110static inline float fmaxabsf(const float a, const float b)
111{
112 // Find the max in absolute value and return it with its sign
113 return (fabsf(a) > fabsf(b) && !isnan(a)) ? a :
114 (isnan(b)) ? 0.f : b;
115}
116
117
119static inline float fminabsf(const float a, const float b)
120{
121 // Find the min in absolute value and return it with its sign
122 return (fabsf(a) < fabsf(b) && !isnan(a)) ? a :
123 (isnan(b)) ? 0.f : b;
124}
125
126
128static inline float clamp_simd(const float x)
129{
130 return fminf(fmaxf(x, 0.0f), 1.0f);
131}
132
133#endif // DT_MATH_OPENMP_MATHS_H
134
135// clang-format off
136// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
137// vim: shiftwidth=2 expandtab tabstop=2 cindent
138// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
139// clang-format on
140
static const float x
#define __OMP_DECLARE_SIMD__(...)
Definition openmp.h:100
static float fminabsf(const float a, const float b)
static float fmaxabsf(const float a, const float b)
static float v_maxf(const float vector[3])
static float v_sumf(const float vector[3])
static float fast_exp10f(const float x)
static float clamp_simd(const float x)
static float fast_expf(const float x)
static float v_minf(const float vector[3])