Skip to content

Commit 1d0cd9b

Browse files
Code maintenance using predefined math defines
1. Instead of calculating 2*PI, PI/2 and PI/4 we use existing defines M_PI_2f M_PI_4f and introduce DT_2PI_F. 2. root(2.0) is replaced by M_SQRT2_F
1 parent f2f25f2 commit 1d0cd9b

30 files changed

Lines changed: 167 additions & 138 deletions

data/kernels/basic.cl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,12 +3243,12 @@ colorzones_v3 (read_only image2d_t in,
32433243

32443244
if(x >= width || y >= height) return;
32453245

3246-
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
3246+
float4 pixel = readpixel(in, x, y);
32473247

32483248
const float a = pixel.y;
32493249
const float b = pixel.z;
3250-
const float h = fmod(atan2(b, a) + 2.0f*M_PI_F, 2.0f*M_PI_F)/(2.0f*M_PI_F);
3251-
const float C = sqrt(b*b + a*a);
3250+
const float h = fmod(atan2(b, a) + DT_2PI_F, DT_2PI_F) / DT_2PI_F;
3251+
const float C = dt_fast_hypot(b, a);
32523252

32533253
float select = 0.0f;
32543254
float blend = 0.0f;
@@ -3264,7 +3264,7 @@ colorzones_v3 (read_only image2d_t in,
32643264
default:
32653265
case DT_IOP_COLORZONES_h:
32663266
select = h;
3267-
blend = pow(1.0f - C/128.0f, 2.0f);
3267+
blend = dtcl_pow(1.0f - C/128.0f, 2.0f);
32683268
break;
32693269
}
32703270

@@ -3273,11 +3273,11 @@ colorzones_v3 (read_only image2d_t in,
32733273
blend *= blend; // saturation isn't as prone to artifacts:
32743274
// const float Cm = 2.0f* (blend*0.5f + (1.0f-blend)*lookup(d->lut[1], select));
32753275
const float Cm = 2.0f * lookup(table_a, select);
3276-
const float L = pixel.x * pow(2.0f, 4.0f*Lm);
3276+
const float L = pixel.x * dtcl_pow(2.0f, 4.0f*Lm);
32773277

32783278
pixel.x = L;
3279-
pixel.y = cos(2.0f*M_PI_F*(h + hm)) * Cm * C;
3280-
pixel.z = sin(2.0f*M_PI_F*(h + hm)) * Cm * C;
3279+
pixel.y = dtcl_cos(DT_2PI_F*(h + hm)) * Cm * C;
3280+
pixel.z = dtcl_sin(DT_2PI_F*(h + hm)) * Cm * C;
32813281

32823282
write_imagef (out, (int2)(x, y), pixel);
32833283
}
@@ -3297,10 +3297,10 @@ colorzones (read_only image2d_t in,
32973297

32983298
if(x >= width || y >= height) return;
32993299

3300-
float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
3300+
float4 pixel = readpixel(in, x, y);
33013301

33023302
float4 LCh;
3303-
const float normalize_C = 1.f / (128.0f * sqrt(2.f));
3303+
const float normalize_C = 1.f / (128.0f * M_SQRT2_F);
33043304

33053305
LCh = Lab_2_LCH(pixel);
33063306

data/kernels/blendop.cl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ blendif_factor_Lab(const float4 input, const float4 output,
194194
float4 LCH_input = Lab_2_LCH(input);
195195
float4 LCH_output = Lab_2_LCH(output);
196196

197-
scaled[DEVELOP_BLENDIF_C_in] = LCH_input.y / (128.0f*sqrt(2.0f)); // C scaled to 0..1
197+
scaled[DEVELOP_BLENDIF_C_in] = LCH_input.y / (128.0f*M_SQRT2_F); // C scaled to 0..1
198198
scaled[DEVELOP_BLENDIF_h_in] = LCH_input.z; // h scaled to 0..1
199199

200-
scaled[DEVELOP_BLENDIF_C_out] = LCH_output.y / (128.0f*sqrt(2.0f)); // C scaled to 0..1
200+
scaled[DEVELOP_BLENDIF_C_out] = LCH_output.y / (128.0f*M_SQRT2_F); // C scaled to 0..1
201201
scaled[DEVELOP_BLENDIF_h_out] = LCH_output.z; // h scaled to 0..1
202202
}
203203

@@ -1266,9 +1266,9 @@ blendop_rgb_hsl(__read_only image2d_t in_a, __read_only image2d_t in_b, __read_o
12661266
ta = RGB_2_HSV(a);
12671267
tb = RGB_2_HSV(b);
12681268
// blend color vectors of input and output
1269-
d = ta.y*cos(2.0f*M_PI_F*ta.x) * (1.0f - opacity) + tb.y*cos(2.0f*M_PI_F*tb.x) * opacity;
1270-
s = ta.y*sin(2.0f*M_PI_F*ta.x) * (1.0f - opacity) + tb.y*sin(2.0f*M_PI_F*tb.x) * opacity;
1271-
to.x = fmod(atan2(s, d)/(2.0f*M_PI_F)+1.0f, 1.0f);
1269+
d = ta.y*cos(DT_2PI_F*ta.x) * (1.0f - opacity) + tb.y*cos(DT_2PI_F*tb.x) * opacity;
1270+
s = ta.y*sin(DT_2PI_F*ta.x) * (1.0f - opacity) + tb.y*sin(DT_2PI_F*tb.x) * opacity;
1271+
to.x = fmod(atan2(s, d)/DT_2PI_F+1.0f, 1.0f);
12721272
to.y = dt_fast_hypot(s, d);
12731273
to.z = ta.z;
12741274
o = HSV_2_RGB(to);
@@ -1560,12 +1560,12 @@ blendop_display_channel(__read_only image2d_t in_a, __read_only image2d_t in_b,
15601560
break;
15611561
case DT_DEV_PIXELPIPE_DISPLAY_LCH_C:
15621562
LCH = Lab_2_LCH(a);
1563-
c = clipf(LCH.y / (128.0f * sqrt(2.0f) / exp2(boost_factors[DEVELOP_BLENDIF_C_in])));
1563+
c = clipf(LCH.y / (128.0f * M_SQRT2_F / exp2(boost_factors[DEVELOP_BLENDIF_C_in])));
15641564
is_lab = 1;
15651565
break;
15661566
case (DT_DEV_PIXELPIPE_DISPLAY_LCH_C | DT_DEV_PIXELPIPE_DISPLAY_OUTPUT):
15671567
LCH = Lab_2_LCH(b);
1568-
c = clipf(LCH.y / (128.0f * sqrt(2.0f)) / exp2(boost_factors[DEVELOP_BLENDIF_C_out]));
1568+
c = clipf(LCH.y / (128.0f * M_SQRT2_F) / exp2(boost_factors[DEVELOP_BLENDIF_C_out]));
15691569
is_lab = 1;
15701570
break;
15711571
case DT_DEV_PIXELPIPE_DISPLAY_LCH_h:

data/kernels/colorharmonizer.cl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ kernel void colorharmonizer_map(read_only image2d_t in,
9999
float4 xyY = dt_D65_XYZ_to_xyY(XYZ_D65);
100100
float4 JCH = xyY_to_dt_UCS_JCH(xyY, L_white);
101101

102-
const float hue = (JCH.z + M_PI_F) / (2.0f * M_PI_F);
102+
const float hue = (JCH.z + M_PI_F) / DT_2PI_F;
103103

104104
const int idx = y * width + x;
105105
jch_out[idx] = (float4)(JCH.x, JCH.y, hue, pix_in.w);
@@ -146,7 +146,7 @@ kernel void colorharmonizer_apply(write_only image2d_t out,
146146
float4 JCH;
147147
JCH.x = J;
148148
JCH.y = fmax(chroma * (1.0f + corr.y * chroma_weight), 0.0f);
149-
JCH.z = wrap_hue(hue + corr.x * effect_strength * chroma_weight) * 2.0f * M_PI_F - M_PI_F;
149+
JCH.z = wrap_hue(hue + corr.x * effect_strength * chroma_weight) * DT_2PI_F - M_PI_F;
150150

151151
float4 xyY = dt_UCS_JCH_to_xyY(JCH, L_white);
152152
float4 XYZ_D65 = dt_xyY_to_XYZ(xyY);

data/kernels/colorreconstruction.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ colorreconstruction_splat(
9999
case COLORRECONSTRUCT_PRECEDENCE_HUE:
100100
m = atan2(pixel.z, pixel.y) - params.x;
101101
// readjust m into [-pi, +pi] interval
102-
m = m > M_PI_F ? m - 2*M_PI_F : (m < -M_PI_F ? m + 2*M_PI_F : m);
102+
m = m > M_PI_F ? m - DT_2PI_F : (m < -M_PI_F ? m + DT_2PI_F : m);
103103
weight = exp(-m*m/params.y);
104104
break;
105105

data/kernels/colorspace.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static inline float4 Lab_2_LCH(float4 Lab)
5656
{
5757
float H = atan2(Lab.z, Lab.y);
5858

59-
H = (H > 0.0f) ? H / (2.0f*M_PI_F) : 1.0f - fabs(H) / (2.0f*M_PI_F);
59+
H = (H > 0.0f) ? H / DT_2PI_F : 1.0f - fabs(H) / DT_2PI_F;
6060

6161
const float L = Lab.x;
6262
const float C = dt_fast_hypot(Lab.y, Lab.z);
@@ -68,8 +68,8 @@ static inline float4 Lab_2_LCH(float4 Lab)
6868
static inline float4 LCH_2_Lab(float4 LCH)
6969
{
7070
const float L = LCH.x;
71-
const float a = cos(2.0f*M_PI_F*LCH.z) * LCH.y;
72-
const float b = sin(2.0f*M_PI_F*LCH.z) * LCH.y;
71+
const float a = cos(DT_2PI_F*LCH.z) * LCH.y;
72+
const float b = sin(DT_2PI_F*LCH.z) * LCH.y;
7373

7474
return (float4)(L, a, b, LCH.w);
7575
}
@@ -434,7 +434,7 @@ static inline float4 JzAzBz_2_XYZ(const float4 JzAzBz)
434434

435435
static inline float4 JzAzBz_to_JzCzhz(float4 JzAzBz)
436436
{
437-
const float h = atan2(JzAzBz.z, JzAzBz.y) / (2.0f * M_PI_F);
437+
const float h = atan2(JzAzBz.z, JzAzBz.y) / DT_2PI_F;
438438
float4 JzCzhz;
439439
JzCzhz.x = JzAzBz.x;
440440
JzCzhz.y = dt_fast_hypot(JzAzBz.y, JzAzBz.z);
@@ -955,7 +955,7 @@ static inline float lookup_gamut(global const float *gamut_lut, const float x)
955955

956956
// Linearly interpolate the value of the gamut LUT at the hue angle in radians.
957957
// convert in LUT coordinate
958-
const float x_test = (float)LUT_ELEM * (x + M_PI_F) / (2.f * M_PI_F);
958+
const float x_test = (float)LUT_ELEM * (x + M_PI_F) / DT_2PI_F;
959959

960960
// find the 2 closest integer coordinates (next/previous)
961961
const float x_prev = floor(x_test);

data/kernels/common.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,26 @@ constant sampler_t samplerA = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE
3131

3232

3333
#ifndef M_PI_F
34-
#define M_PI_F 3.14159265358979323846f // should be defined by the OpenCL compiler acc. to standard
34+
#define M_PI_F 3.14159265358979323846f
3535
#endif
3636

37+
#ifndef M_LN2f
3738
#define M_LN2f 0.69314718055994530942f
39+
#endif
40+
41+
#ifndef M_PI_2f
42+
#define M_PI_2f 1.57079632679489661923f
43+
#endif
44+
45+
#ifndef M_PI_4f
46+
#define M_PI_4f 0.78539816339744830962f
47+
#endif
48+
49+
#ifndef M_SQRT2_F
50+
#define M_SQRT2_F 1.41421356237309504880f
51+
#endif
52+
53+
#define DT_2PI_F 6.28318530717958647693f
3854

3955
#define LUT_ELEM 512 // gamut LUT number of elements:
4056

src/chart/thinplate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ int thinplate_match(const tonecurve_t *curve, // tonecurve to apply after this (
440440
float thinplate_color_pos(const float L, const float a, const float b)
441441
{
442442
const float h = atan2f(b, a) + M_PI_F;
443-
const int sector = 4.0f * h / (2.0f * M_PI_F);
443+
const int sector = 4.0f * h / DT_2PI_F;
444444
return 256.0 * sector + L; // C;
445445
}
446446

src/common/colorspaces_inline_conversions.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static inline void dt_Luv_to_Lch(const dt_aligned_pixel_t Luv, dt_aligned_pixel_
330330
Lch[0] = Luv[0]; // L stays L
331331
Lch[1] = hypotf(Luv[2], Luv[1]); // chroma radius
332332
Lch[2] = atan2f(Luv[2], Luv[1]); // hue angle
333-
Lch[2] = (Lch[2] < 0.f) ? 2.f * M_PI + Lch[2] : Lch[2]; // ensure angle is positive modulo 2 pi
333+
Lch[2] = (Lch[2] < 0.f) ? DT_2PI_F + Lch[2] : Lch[2]; // ensure angle is positive modulo 2 pi
334334
}
335335

336336

@@ -753,9 +753,9 @@ static inline void dt_Lab_2_LCH(const dt_aligned_pixel_t Lab, dt_aligned_pixel_t
753753
float var_H = atan2f(Lab[2], Lab[1]);
754754

755755
if(var_H > 0.0f)
756-
var_H = var_H / (2.0f * M_PI_F);
756+
var_H = var_H / DT_2PI_F;
757757
else
758-
var_H = 1.0f - fabsf(var_H) / (2.0f * M_PI_F);
758+
var_H = 1.0f - fabsf(var_H) / DT_2PI_F;
759759

760760
LCH[0] = Lab[0];
761761
LCH[1] = hypotf(Lab[1], Lab[2]);
@@ -767,8 +767,8 @@ DT_OMP_DECLARE_SIMD()
767767
static inline void dt_LCH_2_Lab(const dt_aligned_pixel_t LCH, dt_aligned_pixel_t Lab)
768768
{
769769
Lab[0] = LCH[0];
770-
Lab[1] = cosf(2.0f * M_PI_F * LCH[2]) * LCH[1];
771-
Lab[2] = sinf(2.0f * M_PI_F * LCH[2]) * LCH[1];
770+
Lab[1] = cosf(DT_2PI_F * LCH[2]) * LCH[1];
771+
Lab[2] = sinf(DT_2PI_F * LCH[2]) * LCH[1];
772772
}
773773

774774
static inline float dt_camera_rgb_luminance(const dt_aligned_pixel_t rgb)
@@ -862,7 +862,7 @@ static inline void dt_XYZ_2_JzAzBz(const dt_aligned_pixel_t XYZ_D65, dt_aligned_
862862
DT_OMP_DECLARE_SIMD(aligned(JzAzBz, JzCzhz: 16))
863863
static inline void dt_JzAzBz_2_JzCzhz(const dt_aligned_pixel_t JzAzBz, dt_aligned_pixel_t JzCzhz)
864864
{
865-
float var_H = atan2f(JzAzBz[2], JzAzBz[1]) / (2.0f * M_PI_F);
865+
float var_H = atan2f(JzAzBz[2], JzAzBz[1]) / DT_2PI_F;
866866
JzCzhz[0] = JzAzBz[0];
867867
JzCzhz[1] = hypotf(JzAzBz[1], JzAzBz[2]);
868868
JzCzhz[2] = var_H >= 0.0f ? var_H : 1.0f + var_H;
@@ -872,8 +872,8 @@ DT_OMP_DECLARE_SIMD(aligned(JzCzhz, JzAzBz: 16))
872872
static inline void dt_JzCzhz_2_JzAzBz(const dt_aligned_pixel_t JzCzhz, dt_aligned_pixel_t JzAzBz)
873873
{
874874
JzAzBz[0] = JzCzhz[0];
875-
JzAzBz[1] = cosf(2.0f * M_PI_F * JzCzhz[2]) * JzCzhz[1];
876-
JzAzBz[2] = sinf(2.0f * M_PI_F * JzCzhz[2]) * JzCzhz[1];
875+
JzAzBz[1] = cosf(DT_2PI_F * JzCzhz[2]) * JzCzhz[1];
876+
JzAzBz[2] = sinf(DT_2PI_F * JzCzhz[2]) * JzCzhz[1];
877877
}
878878

879879
DT_OMP_DECLARE_SIMD(aligned(JzAzBz, XYZ_D65: 16))

src/common/darktable_ucs_22_helpers.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ static inline float Delta_H(const float h_1, const float h_2)
1010
// Compute the difference between 2 angles
1111
// and force the result in [-pi; pi] radians
1212
float diff = h_1 - h_2;
13-
diff += (diff < -M_PI_F) ? 2.f * M_PI_F : 0.f;
14-
diff -= (diff > M_PI_F) ? 2.f * M_PI_F : 0.f;
13+
diff += (diff < -M_PI_F) ? DT_2PI_F : 0.f;
14+
diff -= (diff > M_PI_F) ? DT_2PI_F : 0.f;
1515
return diff;
1616
}
1717

@@ -57,7 +57,7 @@ static inline void dt_UCS_22_build_gamut_LUT(dt_colormatrix_t input_matrix, floa
5757
DT_OMP_FOR(reduction(+: gamut_LUT[:LUT_ELEM], sampler[:LUT_ELEM]))
5858
for(int i = 0; i < 50 * LUT_ELEM; i++)
5959
{
60-
const float angle = -M_PI_F + ((float)i) / (float)(50 * LUT_ELEM) * 2.f * M_PI_F;
60+
const float angle = -M_PI_F + ((float)i) / (float)(50 * LUT_ELEM) * DT_2PI_F;
6161
const float tan_angle = tanf(angle);
6262

6363
const float t_1 = Delta_H(angle, h_blue) / Delta_H(h_red, h_blue);
@@ -96,7 +96,7 @@ static inline void dt_UCS_22_build_gamut_LUT(dt_colormatrix_t input_matrix, floa
9696

9797
// Get the hue angle in darktable UCS
9898
const float hue = atan2f(UV_star_prime[1], UV_star_prime[0]);
99-
int index = roundf((float)(LUT_ELEM - 1) * (hue + M_PI_F) / (2.f * M_PI_F));
99+
int index = roundf((float)(LUT_ELEM - 1) * (hue + M_PI_F) / DT_2PI_F);
100100
index += (index < 0) ? LUT_ELEM : 0;
101101
index -= (index >= LUT_ELEM) ? LUT_ELEM : 0;
102102
// Warning: we store M², the square of the colorfulness
@@ -137,7 +137,7 @@ static inline float lookup_gamut(const float gamut_lut[LUT_ELEM], const float hu
137137
*/
138138

139139
// convert hue in LUT index coordinate
140-
const float x_test = (float)LUT_ELEM * (hue + M_PI_F) / (2.f * M_PI_F);
140+
const float x_test = (float)LUT_ELEM * (hue + M_PI_F) / DT_2PI_F;
141141

142142
const float x_prev = floor(x_test);
143143
const float x_next = ceil(x_test);

src/common/histogram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static inline void _bin_Lab_LCh(const dt_dev_histogram_collection_params_t *cons
144144
float *in = (float *)pixel + 4 * (roi->width * j + roi->crop_x);
145145
const float max_bin = params->bins_count - 1;
146146
const dt_aligned_pixel_t scale = { max_bin / 100.0f,
147-
max_bin / (128.0f * sqrtf(2.0f)),
147+
max_bin / (128.0f * M_SQRT2_F),
148148
max_bin, 0.0f };
149149

150150
for(int i = 0; i < roi->width - roi->crop_right - roi->crop_x; i++)

0 commit comments

Comments
 (0)