Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
target_clones.h
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4
5 Ansel 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 Ansel 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 Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef DT_SYSTEM_TARGET_CLONES_H
19#define DT_SYSTEM_TARGET_CLONES_H
20
21/* Function multi-versioning (target_clones) for SIMD compute functions.
22 *
23 * This is THE canonical definition of __DT_CLONE_TARGETS__: darktable.h
24 * inherits it from here (it used to carry a deliberate duplicate until the
25 * de-glueing landed). Self-contained on purpose — low-level compute units
26 * (common/nn_model.c and friends) include this without pulling anything else.
27 *
28 * The guard keeps the definition inert if some translation unit ends up seeing
29 * it twice through different paths.
30 */
31
32#ifndef __DT_CLONE_TARGETS__
33
34/* Create cloned functions for various CPU SSE generations */
35/* See for instructions https://hannes.hauswedell.net/post/2017/12/09/fmv/ */
36/* TL;DR : use only on SIMD functions containing low-level paralellized/vectorized loops */
37#if __has_attribute(target_clones) && !defined(_WIN32) && !defined(NATIVE_ARCH) && !defined(_DEBUG)
38
39 /*
40 * Apple note:
41 * - arm64 vs x86_64 is handled by the universal binary, not target_clones.
42 * - target_clones on Apple is only useful within one slice.
43 * - the x86-64-v2/v3/v4 strings are not accepted by all Apple Clang versions.
44 *
45 * Therefore:
46 * - on Apple arm64: disable clones here,
47 * - on Apple x86_64: require explicit opt-in once the toolchain is validated.
48 */
49
50 #if defined(__APPLE__)
51
52 #if defined(__aarch64__) || defined(__arm64__)
53 #define __DT_CLONE_TARGETS__
54
55 #elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
56
57 /*
58 * Enable this from your build system only after verifying that the local
59 * Apple Clang accepts:
60 * target_clones("default","arch=x86-64","arch=x86-64-v2","arch=x86-64-v3","arch=x86-64-v4")
61 *
62 * Example:
63 * -DDT_APPLE_X86_TARGET_CLONES=1
64 */
65 #if defined(DT_APPLE_X86_TARGET_CLONES)
66 #define __DT_CLONE_TARGETS__ \
67 __attribute__((target_clones( \
68 "default", \
69 "arch=x86-64", \
70 "arch=x86-64-v2", \
71 "arch=x86-64-v3", \
72 "arch=x86-64-v4" \
73 )))
74 #else
75 #define __DT_CLONE_TARGETS__
76 #endif
77
78 #else
79 #define __DT_CLONE_TARGETS__
80 #endif
81
82 #elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64)
83 #define __DT_CLONE_TARGETS__ \
84 __attribute__((target_clones( \
85 "default", \
86 "arch=x86-64", \
87 "arch=x86-64-v2", \
88 "arch=x86-64-v3", \
89 "arch=x86-64-v4" \
90 )))
91
92 #elif defined(__PPC64__)
93 /* __PPC64__ is the only macro tested for in is_supported_platform.h, other macros would fail there anyway. */
94 #define __DT_CLONE_TARGETS__ __attribute__((target_clones("default","cpu=power9")))
95
96 #else
97 #define __DT_CLONE_TARGETS__
98 #endif
99
100#else
101 #define __DT_CLONE_TARGETS__
102#endif
103
104#endif // __DT_CLONE_TARGETS__
105#endif // DT_SYSTEM_TARGET_CLONES_H