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