Skip to content

Commit 2d09a53

Browse files
committed
Avoid aliasing in mspnr_operator
1 parent b62ac4f commit 2d09a53

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Source/astcenccli_error_metrics.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ static float mpsnr_operator(
6868
float val,
6969
int fstop
7070
) {
71-
if32 p;
72-
p.u = 0x3f800000 + (fstop << 23); // 0x3f800000 is 1.0f
73-
val *= p.f;
74-
val = powf(val, (1.0f / 2.2f));
75-
val *= 255.0f;
71+
// Fast implementation of pow(2.0, fstop), assuming IEEE float layout
72+
uint32_t uscale = 0x3f800000 + (fstop << 23);
7673

77-
return astc::clamp(val, 0.0f, 255.0f);
74+
// Future: Can use std:bit_cast with C++20
75+
float scale;
76+
std::memcpy(&scale, &uscale, sizeof(float));
77+
78+
val = powf(val * scale, (1.0f / 2.2f));
79+
return astc::clamp(val * 255.0f, 0.0f, 255.0f);
7880
}
7981

8082
/**

0 commit comments

Comments
 (0)