Blender V5.3
BLI_normalized_int_types.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "BLI_math_vector.hh"
12
13namespace blender {
14
15namespace detail {
16template<typename T, int CompLen, int... CompBitCounts> struct NormalizedIntVec;
17
18template<typename T, int SizeX, int SizeY> struct NormalizedIntVec<T, 2, SizeX, SizeY> {
21 constexpr static bool is_signed = std::is_signed<T>();
22 constexpr static T x_max = (1 << (SizeX - int(is_signed))) - 1;
23 constexpr static T y_max = (1 << (SizeY - int(is_signed))) - 1;
24#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1944 && _MSC_VER < 1950
25 // Workaround for MSVC 17.14 ICE: use constexpr to indirectly reference size arguments
26 // Does not make a whole lot of sense, but it sidesteps the ICE.
27 constexpr static T SizeX_workaround = SizeX;
28 constexpr static T SizeY_workaround = SizeY;
29 T x : SizeX_workaround;
30 T y : SizeY_workaround;
31#else
32 T x : SizeX;
33 T y : SizeY;
34#endif
35 NormalizedIntVec() = default;
36 constexpr NormalizedIntVec(IntVecT value) : x(value.x), y(value.y) {}
37
38 operator IntVecT() const
39 {
40 return IntVecT(x, y);
41 }
42
43 static constexpr VecT max()
44 {
45 return VecT(x_max, y_max);
46 }
47
48 static constexpr VecT min()
49 {
50 if (is_signed) {
51 return VecT(-x_max, -y_max);
52 }
53 return VecT(0.0f, 0.0f);
54 }
55};
56
57template<typename T, int SizeX, int SizeY, int SizeZ, int SizeW>
58struct NormalizedIntVec<T, 4, SizeX, SizeY, SizeZ, SizeW> {
61 constexpr static bool is_signed = std::is_signed<T>();
62 constexpr static T x_max = (1 << (SizeX - int(is_signed))) - 1;
63 constexpr static T y_max = (1 << (SizeY - int(is_signed))) - 1;
64 constexpr static T z_max = (1 << (SizeZ - int(is_signed))) - 1;
65 constexpr static T w_max = (1 << (SizeW - int(is_signed))) - 1;
66
67#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER >= 1944 && _MSC_VER < 1950
68 // Workaround for MSVC 17.14 ICE: use constexpr to indirectly reference size arguments
69 // Does not make a whole lot of sense, but it sidesteps the ICE.
70 constexpr static T SizeX_workaround = SizeX;
71 constexpr static T SizeY_workaround = SizeY;
72 constexpr static T SizeZ_workaround = SizeZ;
73 constexpr static T SizeW_workaround = SizeW;
74 T x : SizeX_workaround;
75 T y : SizeY_workaround;
76 T z : SizeZ_workaround;
77 T w : SizeW_workaround;
78#else
79 T x : SizeX;
80 T y : SizeY;
81 T z : SizeZ;
82 T w : SizeW;
83#endif
84 NormalizedIntVec() = default;
85 constexpr NormalizedIntVec(IntVecT value) : x(value.x), y(value.y), z(value.z), w(value.w) {}
86
87 operator IntVecT() const
88 {
89 return IntVecT(x, y, z, w);
90 }
91
92 static constexpr VecT max()
93 {
94 return VecT(x_max, y_max, z_max, w_max);
95 }
96
97 static constexpr VecT min()
98 {
99 if constexpr (is_signed) {
100 return VecT(-x_max, -y_max, -z_max, -w_max);
101 }
102 return VecT(0.0f, 0.0f, 0.0f, 0.0f);
103 }
104};
105
106} // namespace detail
107
108template<typename T, int CompLen, int... CompBitCounts>
109 requires(std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>)
110struct NormalizedIntVecBase : detail::NormalizedIntVec<T, CompLen, CompBitCounts...> {
111 using IntPacked = detail::NormalizedIntVec<T, CompLen, CompBitCounts...>;
112 using typename IntPacked::IntVecT;
113 using typename IntPacked::VecT;
114
116
117 NormalizedIntVecBase(IntVecT value) : IntPacked(value) {}
118
119 /* Adding rounding would be the standard compliant conversion.
120 * But this would introduce perf regression. */
122 : IntPacked(IntVecT(math::clamp(val * IntPacked::max(), IntPacked::min(), IntPacked::max())))
123 {
124 }
125
126 operator VecT() const
127 {
128 return VecT(IntVecT(*this)) / IntPacked::max();
129 }
130};
131
140
141} // namespace blender
btScalar max
constexpr T clamp(T, U, U) RET
#define T
BMesh Iterators.
NormalizedIntVecBase< int32_t, 4, 10, 10, 10, 2 > int1010102_norm
NormalizedIntVecBase< uint32_t, 4, 16, 16, 16, 16 > ushort4_norm
NormalizedIntVecBase< uint32_t, 4, 8, 8, 8, 8 > uchar4_norm
NormalizedIntVecBase< uint32_t, 4, 10, 10, 10, 2 > uint1010102_norm
NormalizedIntVecBase< int32_t, 4, 8, 8, 8, 8 > char4_norm
NormalizedIntVecBase< int32_t, 2, 16, 16 > short2_norm
NormalizedIntVecBase< uint32_t, 2, 16, 16 > ushort2_norm
NormalizedIntVecBase< int32_t, 4, 16, 16, 16, 16 > short4_norm
#define min(a, b)
Definition sort.cc:38
detail::NormalizedIntVec< T, CompLen, CompBitCounts... > IntPacked