From a7f2d17e6415782a57505cd1a14681de6736b942 Mon Sep 17 00:00:00 2001 From: Peter Harris Date: Thu, 7 May 2026 16:36:56 +0100 Subject: [PATCH 1/2] Avoid UBSAN warning on left-shift of of negative value --- Source/astcenc_color_unquantize.cpp | 40 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/Source/astcenc_color_unquantize.cpp b/Source/astcenc_color_unquantize.cpp index 8aacee15e..7ca1b050c 100644 --- a/Source/astcenc_color_unquantize.cpp +++ b/Source/astcenc_color_unquantize.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 // ---------------------------------------------------------------------------- -// Copyright 2011-2025 Arm Limited +// Copyright 2011-2026 Arm Limited // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy @@ -778,36 +778,38 @@ static void hdr_alpha_unpack( int& output0, int& output1 ) { - int v6 = input[0]; int v7 = input[1]; - int selector = ((v6 >> 7) & 1) | ((v7 >> 6) & 2); + int modeval = ((v6 >> 7) & 1) | ((v7 >> 6) & 2); v6 &= 0x7F; v7 &= 0x7F; - if (selector == 3) + + // Directly specified 12-bit alpha values + if (modeval == 3) { output0 = v6 << 5; output1 = v7 << 5; } + // Packed base + delta alpha values else { - v6 |= (v7 << (selector + 1)) & 0x780; - v7 &= (0x3f >> selector); - v7 ^= 32 >> selector; - v7 -= 32 >> selector; - v6 <<= (4 - selector); - v7 <<= (4 - selector); - v7 += v6; + // Transfer 1-3 high bits of v7 to make an 8-10 bit base + v6 |= (v7 << (modeval + 1)) & 0x780; - if (v7 < 0) - { - v7 = 0; - } - else if (v7 > 0xFFF) - { - v7 = 0xFFF; - } + // Extract remaining 4-6 bits and unbias to make signed delta + v7 &= (0x3f >> modeval); + v7 ^= 32 >> modeval; + v7 -= 32 >> modeval; + + // Scale unsigned base to 12 bits + v6 = v6 << (4 - modeval); + + // Scale signed delta to 6-10 bits + v7 = safe_signed_lsh(v7, 4 - modeval); + + // Clamp computed base+delta value to valid range + v7 = astc::clamp(v6 + v7, 0, 0xFFF); output0 = v6; output1 = v7; From aa82de56cf5ea0292d9f472a13c66c7d10610f58 Mon Sep 17 00:00:00 2001 From: Peter Harris Date: Thu, 7 May 2026 16:43:13 +0100 Subject: [PATCH 2/2] Change log --- Docs/ChangeLog-5x.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Docs/ChangeLog-5x.md b/Docs/ChangeLog-5x.md index f8173a832..515b248a6 100644 --- a/Docs/ChangeLog-5x.md +++ b/Docs/ChangeLog-5x.md @@ -17,8 +17,10 @@ The 5.5.0 release is a minor maintenance release. * **Bug fix:** Add missing low-clamp when storing decompressed HDR data into a UNORM8 image. Prior to this fix, output colors would be incorrect if a HDR void-extent block contained a negative FP16 color value. - * **Bug fix:** Avoid undefined NaN to int conversion behavior that is - triggered by NaNs generated by atan2() calls where both inputs are zero. + * **Bug fix:** Avoid undefined behavior on NaN to int conversion, that is + triggered by NaNs generated by `atan2()` calls where both inputs are zero. + * **Bug fix:** Avoid undefined behavior on left shift of negative integer, + that is triggered for some HDR encodings in `hdr_alpha_unpack()`. ## 5.4.0