Skip to content

Commit 2cdf67c

Browse files
authored
Make eligible inline functions in llmath.h constexpr (#2944)
1 parent 0cb91ee commit 2cdf67c

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

indra/llcommon/stdtypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,14 @@ class narrow
164164
FROM mValue;
165165

166166
public:
167-
narrow(FROM value): mValue(value) {}
167+
constexpr narrow(FROM value): mValue(value) {}
168168

169169
/*---------------------- Narrowing unsigned to signed ----------------------*/
170170
template <typename TO,
171171
typename std::enable_if<std::is_unsigned<FROM>::value &&
172172
std::is_signed<TO>::value,
173173
bool>::type = true>
174-
inline
174+
constexpr
175175
operator TO() const
176176
{
177177
// The reason we skip the
@@ -189,7 +189,7 @@ class narrow
189189
typename std::enable_if<! (std::is_unsigned<FROM>::value &&
190190
std::is_signed<TO>::value),
191191
bool>::type = true>
192-
inline
192+
constexpr
193193
operator TO() const
194194
{
195195
// two different assert()s so we can tell which condition failed

indra/llmath/llmath.h

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ constexpr F32 GIMBAL_THRESHOLD = 0.000436f; // sets the gimballock threshold 0
8989
constexpr F32 FP_MAG_THRESHOLD = 0.0000001f;
9090

9191
// TODO: Replace with logic like is_approx_equal
92-
inline bool is_approx_zero( F32 f ) { return (-F_APPROXIMATELY_ZERO < f) && (f < F_APPROXIMATELY_ZERO); }
92+
constexpr bool is_approx_zero(F32 f) { return (-F_APPROXIMATELY_ZERO < f) && (f < F_APPROXIMATELY_ZERO); }
9393

9494
// These functions work by interpreting sign+exp+mantissa as an unsigned
9595
// integer.
@@ -148,33 +148,17 @@ inline F64 llabs(const F64 a)
148148
return F64(std::fabs(a));
149149
}
150150

151-
inline S32 lltrunc( F32 f )
151+
constexpr S32 lltrunc(F32 f)
152152
{
153-
#if LL_WINDOWS && !defined( __INTEL_COMPILER ) && (ADDRESS_SIZE == 32)
154-
// Avoids changing the floating point control word.
155-
// Add or subtract 0.5 - epsilon and then round
156-
const static U32 zpfp[] = { 0xBEFFFFFF, 0x3EFFFFFF };
157-
S32 result;
158-
__asm {
159-
fld f
160-
mov eax, f
161-
shr eax, 29
162-
and eax, 4
163-
fadd dword ptr [zpfp + eax]
164-
fistp result
165-
}
166-
return result;
167-
#else
168-
return (S32)f;
169-
#endif
153+
return narrow(f);
170154
}
171155

172-
inline S32 lltrunc( F64 f )
156+
constexpr S32 lltrunc(F64 f)
173157
{
174-
return (S32)f;
158+
return narrow(f);
175159
}
176160

177-
inline S32 llfloor( F32 f )
161+
inline S32 llfloor(F32 f)
178162
{
179163
#if LL_WINDOWS && !defined( __INTEL_COMPILER ) && (ADDRESS_SIZE == 32)
180164
// Avoids changing the floating point control word.
@@ -284,7 +268,7 @@ constexpr F32 FAST_MAG_BETA = 0.397824734759f;
284268
//constexpr F32 FAST_MAG_ALPHA = 0.948059448969f;
285269
//constexpr F32 FAST_MAG_BETA = 0.392699081699f;
286270

287-
inline F32 fastMagnitude(F32 a, F32 b)
271+
constexpr F32 fastMagnitude(F32 a, F32 b)
288272
{
289273
a = (a > 0) ? a : -a;
290274
b = (b > 0) ? b : -b;
@@ -342,7 +326,7 @@ inline F32 llfastpow(const F32 x, const F32 y)
342326
}
343327

344328

345-
inline F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
329+
constexpr F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
346330
{
347331
// compute the power of ten
348332
F32 bar = 1.f;
@@ -360,25 +344,25 @@ inline F32 snap_to_sig_figs(F32 foo, S32 sig_figs)
360344

361345
using std::lerp;
362346

363-
inline F32 lerp2d(F32 x00, F32 x01, F32 x10, F32 x11, F32 u, F32 v)
347+
constexpr F32 lerp2d(F32 x00, F32 x01, F32 x10, F32 x11, F32 u, F32 v)
364348
{
365349
F32 a = x00 + (x01-x00)*u;
366350
F32 b = x10 + (x11-x10)*u;
367351
F32 r = a + (b-a)*v;
368352
return r;
369353
}
370354

371-
inline F32 ramp(F32 x, F32 a, F32 b)
355+
constexpr F32 ramp(F32 x, F32 a, F32 b)
372356
{
373357
return (a == b) ? 0.0f : ((a - x) / (a - b));
374358
}
375359

376-
inline F32 rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
360+
constexpr F32 rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
377361
{
378362
return lerp(y1, y2, ramp(x, x1, x2));
379363
}
380364

381-
inline F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
365+
constexpr F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
382366
{
383367
if (y1 < y2)
384368
{
@@ -391,7 +375,7 @@ inline F32 clamp_rescale(F32 x, F32 x1, F32 x2, F32 y1, F32 y2)
391375
}
392376

393377

394-
inline F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
378+
constexpr F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
395379
{
396380
if (x <= x0)
397381
return s0;
@@ -404,14 +388,14 @@ inline F32 cubic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
404388
return s0 + (s1 - s0) * (f * f) * (3.0f - 2.0f * f);
405389
}
406390

407-
inline F32 cubic_step( F32 x )
391+
constexpr F32 cubic_step( F32 x )
408392
{
409393
x = llclampf(x);
410394

411395
return (x * x) * (3.0f - 2.0f * x);
412396
}
413397

414-
inline F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
398+
constexpr F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
415399
{
416400
if (x <= x0)
417401
return s0;
@@ -425,7 +409,7 @@ inline F32 quadratic_step( F32 x, F32 x0, F32 x1, F32 s0, F32 s1 )
425409
return (s0 * (1.f - f_squared)) + ((s1 - s0) * f_squared);
426410
}
427411

428-
inline F32 llsimple_angle(F32 angle)
412+
constexpr F32 llsimple_angle(F32 angle)
429413
{
430414
while(angle <= -F_PI)
431415
angle += F_TWO_PI;
@@ -435,7 +419,7 @@ inline F32 llsimple_angle(F32 angle)
435419
}
436420

437421
//SDK - Renamed this to get_lower_power_two, since this is what this actually does.
438-
inline U32 get_lower_power_two(U32 val, U32 max_power_two)
422+
constexpr U32 get_lower_power_two(U32 val, U32 max_power_two)
439423
{
440424
if(!max_power_two)
441425
{
@@ -457,7 +441,7 @@ inline U32 get_lower_power_two(U32 val, U32 max_power_two)
457441
// number of digits, then add one. We subtract 1 initially to handle
458442
// the case where the number passed in is actually a power of two.
459443
// WARNING: this only works with 32 bit ints.
460-
inline U32 get_next_power_two(U32 val, U32 max_power_two)
444+
constexpr U32 get_next_power_two(U32 val, U32 max_power_two)
461445
{
462446
if(!max_power_two)
463447
{
@@ -536,7 +520,8 @@ inline void ll_remove_outliers(std::vector<VEC_TYPE>& data, F32 k)
536520
// Note: in our code, values labeled as sRGB are ALWAYS gamma corrected linear values, NOT linear values with monitor gamma applied
537521
// Note: stored color values should always be gamma corrected linear (i.e. the values returned from an on-screen color swatch)
538522
// Note: DO NOT cache the conversion. This leads to error prone synchronization and is actually slower in the typical case due to cache misses
539-
inline float linearTosRGB(const float val) {
523+
inline float linearTosRGB(const float val)
524+
{
540525
if (val < 0.0031308f) {
541526
return val * 12.92f;
542527
}
@@ -551,7 +536,8 @@ inline float linearTosRGB(const float val) {
551536
// Note: Stored color values should generally be gamma corrected sRGB.
552537
// If you're serializing the return value of this function, you're probably doing it wrong.
553538
// Note: DO NOT cache the conversion. This leads to error prone synchronization and is actually slower in the typical case due to cache misses.
554-
inline float sRGBtoLinear(const float val) {
539+
inline float sRGBtoLinear(const float val)
540+
{
555541
if (val < 0.04045f) {
556542
return val / 12.92f;
557543
}

0 commit comments

Comments
 (0)