Skip to content

More ubsan improvements #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/astcenc_color_unquantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static ASTCENC_SIMD_INLINE vint4 uncontract_color(
*/
static ASTCENC_SIMD_INLINE int32_t safe_signed_lsh(int32_t val, int shift)
{
// Future: When we support C++20 can swap memcpy for std::bitcast
// Future: Can use std:bit_cast with C++20
uint32_t uval;
std::memcpy(&uval, &val, sizeof(uint32_t));
uval <<= shift;
Expand Down
18 changes: 12 additions & 6 deletions Source/astcenccli_error_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ static float mpsnr_operator(
float val,
int fstop
) {
if32 p;
p.u = 0x3f800000 + (fstop << 23); // 0x3f800000 is 1.0f
val *= p.f;
val = powf(val, (1.0f / 2.2f));
val *= 255.0f;
// Future: Can use std:bit_cast with C++20

return astc::clamp(val, 0.0f, 255.0f);
// Fast implementation of pow(2.0, fstop), assuming IEEE float layout
// Memcpy to uint avoids ubsan complaints shift of negative int
unsigned int fstopu;
std::memcpy(&fstopu, &fstop, sizeof(int));
uint32_t uscale = 0x3f800000 + (fstopu << 23);

float scale;
std::memcpy(&scale, &uscale, sizeof(float));

val = powf(val * scale, (1.0f / 2.2f));
return astc::clamp(val * 255.0f, 0.0f, 255.0f);
}

/**
Expand Down