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#pragma once
26
27
28#if defined(_OPENMP) && !defined(_WIN32) && !defined(__GNUC__)
29
30#pragma omp declare simd
31extern float fmaxf(const float x, const float y);
32
33#pragma omp declare simd
34extern float fminf(const float x, const float y);
35
36#pragma omp declare simd
37extern float fabsf(const float x);
38
39#pragma omp declare simd
40extern float powf(const float x, const float y);
41
42#pragma omp declare simd
43extern float sqrtf(const float x);
44
45#pragma omp declare simd
46extern float cbrtf(const float x);
47
48#pragma omp declare simd
49extern float log2f(const float x);
50
51#pragma omp declare simd
52extern float exp2f(const float x);
53
54#pragma omp declare simd
55extern float log10f(const float x);
56
57#pragma omp declare simd
58extern float expf(const float x);
59
60#pragma omp declare simd
61extern float logf(const float x);
62
63#endif
64
65
66/* Bring our own optimized maths functions because Clang makes dumb shit */
67
69static inline float fast_exp10f(const float x)
70{
71 // we use the property : 10^x = exp(log(10) * x) = 2^(log(10) * x / log(2))
72 // max relative error over x = [0; 4] is 1.5617955706227326e-15
73 return exp2f(3.3219280948873626f * x);
74}
75
76// Since we are at it, write an optimized expf
78static inline float fast_expf(const float x)
79{
80 // we use the property : exp(x) = 2^(x / log(2))
81 // max relative error over x = [0; 4] is 5.246203046472202e-16
82 return exp2f(1.4426950408889634f * x);
83}
84
85
86__OMP_DECLARE_SIMD__(aligned(vector:16))
87static inline float v_maxf(const float vector[3])
88{
89 // Find the max over an RGB vector
90 return fmaxf(fmaxf(vector[0], vector[1]), vector[2]);
91}
92
93
94__OMP_DECLARE_SIMD__(aligned(vector:16))
95static inline float v_minf(const float vector[3])
96{
97 // Find the min over an RGB vector
98 return fminf(fminf(vector[0], vector[1]), vector[2]);
99}
100
101__OMP_DECLARE_SIMD__(aligned(vector:16))
102static inline float v_sumf(const float vector[3])
103{
104 return vector[0] + vector[1] + vector[2];
105}
106
107
109static inline float fmaxabsf(const float a, const float b)
110{
111 // Find the max in absolute value and return it with its sign
112 return (fabsf(a) > fabsf(b) && !isnan(a)) ? a :
113 (isnan(b)) ? 0.f : b;
114}
115
116
118static inline float fminabsf(const float a, const float b)
119{
120 // Find the min in absolute value and return it with its sign
121 return (fabsf(a) < fabsf(b) && !isnan(a)) ? a :
122 (isnan(b)) ? 0.f : b;
123}
124
125
127static inline float clamp_simd(const float x)
128{
129 return fminf(fmaxf(x, 0.0f), 1.0f);
130}
131// clang-format off
132// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
133// vim: shiftwidth=2 expandtab tabstop=2 cindent
134// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
135// clang-format on
136
#define __OMP_DECLARE_SIMD__(...)
Definition darktable.h:263
static const float x
Definition iop_profile.h:235
static float fminabsf(const float a, const float b)
Definition openmp_maths.h:118
static float fmaxabsf(const float a, const float b)
Definition openmp_maths.h:109
static float v_maxf(const float vector[3])
Definition openmp_maths.h:87
static float v_sumf(const float vector[3])
Definition openmp_maths.h:102
static float fast_exp10f(const float x)
Definition openmp_maths.h:69
static float clamp_simd(const float x)
Definition openmp_maths.h:127
static float fast_expf(const float x)
Definition openmp_maths.h:78
static float v_minf(const float vector[3])
Definition openmp_maths.h:95