Debian Codegraph @

modules/skcms/src/skcms_internals.h

raw
1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #pragma once
9
10 // skcms_internals.h contains APIs shared by skcms' internals and its test tools.
11 // Please don't use this header from outside the skcms repo.
12
13 #include <stdbool.h>
14 #include <stdint.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 // ~~~~ General Helper Macros ~~~~
21 // skcms can leverage some C++ extensions when they are present.
22 #define ARRAY_COUNT(arr) (int)(sizeof((arr)) / sizeof(*(arr)))
23
24 #if defined(__has_cpp_attribute)
25 #if __has_cpp_attribute(clang::fallthrough)
26 #define SKCMS_FALLTHROUGH [[clang::fallthrough]]
27 #elif __has_cpp_attribute(gnu::fallthrough)
28 #define SKCMS_FALLTHROUGH [[gnu::fallthrough]]
29 #endif
30
31 #if defined(__clang__) && !defined(SKCMS_HAS_MUSTTAIL)
32 // [[clang::musttail]] is great for performance, but it's not well supported and we run into
33 // a variety of problems when we use it. Fortunately, it's an optional feature that doesn't
34 // affect correctness, and usually the compiler will generate a tail-call even for us
35 // whether or not we force it to do so.
36 //
37 // Known limitations:
38 // - Sanitizers do not work well with [[clang::musttail]], and corrupt src/dst pointers.
39 // (https://github.com/llvm/llvm-project/issues/70849)
40 // - Wasm tail-calls were only introduced in 2023 and aren't a mainstream feature yet.
41 // - Clang 18 runs into an ICE on armv7/androideabi with [[clang::musttail]].
42 // (http://crbug.com/1504548)
43 // - Android RISC-V also runs into an ICE (b/314692534)
44 // - So does Linux ppc64le (https://github.com/llvm/llvm-project/issues/108014,
45 // https://github.com/llvm/llvm-project/issues/98859)
46 // - LoongArch developers indicate they had to turn it off
47 // - Windows builds generate incorrect code with [[clang::musttail]] and crash mysteriously.
48 // (http://crbug.com/1505442)
49 #if __has_cpp_attribute(clang::musttail) && !__has_feature(memory_sanitizer) \
50 && !__has_feature(address_sanitizer) \
51 && !defined(__EMSCRIPTEN__) \
52 && !defined(__arm__) \
53 && !defined(__riscv) \
54 && !defined(__powerpc__) \
55 && !defined(__loongarch__) \
56 && !defined(_WIN32) && !defined(__SYMBIAN32__)
57 #define SKCMS_HAS_MUSTTAIL 1
58 #endif
59 #elif defined(__GNUC__) && !defined(SKCMS_HAS_MUSTTAIL)
60 // GCC on riscv64 does not support our tail call functions
61 // cf. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121784
62 #if __has_cpp_attribute(clang::musttail) && !defined(__riscv)
63 #define SKCMS_HAS_MUSTTAIL 1
64 #else
65 #define SKCMS_HAS_MUSTTAIL 0
66 #endif
67 #elif !defined(__clang__) && !defined(SKCMS_HAS_MUSTTAIL)
68 #if __has_cpp_attribute(clang::musttail)
69 #define SKCMS_HAS_MUSTTAIL 1
70 #else
71 #define SKCMS_HAS_MUSTTAIL 0
72 #endif
73 #endif
74 #endif
75
76 #ifndef SKCMS_FALLTHROUGH
77 #define SKCMS_FALLTHROUGH
78 #endif
79 #ifndef SKCMS_HAS_MUSTTAIL
80 #define SKCMS_HAS_MUSTTAIL 0
81 #endif
82
83 #if defined(__clang__)
84 #define SKCMS_MAYBE_UNUSED __attribute__((unused))
85 #pragma clang diagnostic ignored "-Wused-but-marked-unused"
86 #elif defined(__GNUC__)
87 #define SKCMS_MAYBE_UNUSED __attribute__((unused))
88 #elif defined(_MSC_VER)
89 #define SKCMS_MAYBE_UNUSED __pragma(warning(suppress:4100))
90 #else
91 #define SKCMS_MAYBE_UNUSED
92 #endif
93
94 // sizeof(x) will return size_t, which is 32-bit on some machines and 64-bit on others.
95 // We have better testing on 64-bit machines, so force 32-bit machines to behave like 64-bit.
96 //
97 // Please do not use sizeof() directly, and size_t only when required.
98 // (We have no way of enforcing these requests...)
99 #define SAFE_SIZEOF(x) ((uint64_t)sizeof(x))
100
101 // Same sort of thing for _Layout structs with a variable sized array at the end (named "variable").
102 #define SAFE_FIXED_SIZE(type) ((uint64_t)offsetof(type, variable))
103
104 // If this isn't Clang, GCC, or Emscripten with SIMD support, we are in SKCMS_PORTABLE mode.
105 #if !defined(SKCMS_PORTABLE) && !(defined(__clang__) || \
106 defined(__GNUC__) || \
107 (defined(__EMSCRIPTEN__) && defined(__wasm_simd128__)))
108 #define SKCMS_PORTABLE 1
109 #endif
110
111 // If we are in SKCMS_PORTABLE mode or running on a non-x86-64 platform, we can't enable HSW or SKX.
112 // We also disable HSW/SKX on Android, even if it's Android on x64, since it's unlikely to benefit.
113 #if defined(SKCMS_PORTABLE) || !defined(__x86_64__) || defined(ANDROID) || defined(__ANDROID__)
114 #undef SKCMS_FORCE_HSW
115 #if !defined(SKCMS_DISABLE_HSW)
116 #define SKCMS_DISABLE_HSW 1
117 #endif
118
119 #undef SKCMS_FORCE_SKX
120 #if !defined(SKCMS_DISABLE_SKX)
121 #define SKCMS_DISABLE_SKX 1
122 #endif
123 #endif
124
125 // ~~~~ Shared ~~~~
126 typedef struct skcms_ICCTag {
127 uint32_t signature;
128 uint32_t type;
129 uint32_t size;
130 const uint8_t* buf;
131 } skcms_ICCTag;
132
133 typedef struct skcms_ICCProfile skcms_ICCProfile;
134 typedef struct skcms_TransferFunction skcms_TransferFunction;
135 typedef union skcms_Curve skcms_Curve;
136
137 void skcms_GetTagByIndex (const skcms_ICCProfile*, uint32_t idx, skcms_ICCTag*);
138 bool skcms_GetTagBySignature(const skcms_ICCProfile*, uint32_t sig, skcms_ICCTag*);
139
140 float skcms_MaxRoundtripError(const skcms_Curve* curve, const skcms_TransferFunction* inv_tf);
141
142 // 252 of a random shuffle of all possible bytes.
143 // 252 is evenly divisible by 3 and 4. Only 192, 10, 241, and 43 are missing.
144 // Used for ICC profile equivalence testing.
145 extern const uint8_t skcms_252_random_bytes[252];
146
147 // ~~~~ Portable Math ~~~~
148 static inline float floorf_(float x) {
149 float roundtrip = (float)((int)x);
150 return roundtrip > x ? roundtrip - 1 : roundtrip;
151 }
152 static inline float fabsf_(float x) { return x < 0 ? -x : x; }
153 float powf_(float, float);
154
155 #ifdef __cplusplus
156 }
157 #endif