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