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