Skip to content

Commit b9c47ea

Browse files
committed
Remove remaining type aliasing though unions
1 parent f6904e9 commit b9c47ea

File tree

6 files changed

+20
-35
lines changed

6 files changed

+20
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.o
1010
*.a
1111
__pycache__
12+
pve
1213
Scratch
1314
Proto
1415

Source/astcenc_decompress_symbolic.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// ----------------------------------------------------------------------------
3-
// Copyright 2011-2024 Arm Limited
3+
// Copyright 2011-2025 Arm Limited
44
//
55
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
66
// use this file except in compliance with the License. You may obtain a copy
@@ -163,9 +163,7 @@ void unpack_weights(
163163
*/
164164
static float error_color_nan()
165165
{
166-
if32 v;
167-
v.u = 0xFFFFE000U;
168-
return v.f;
166+
return astc::uint_as_float(0xFFFFE000u);
169167
}
170168

171169
/* See header for documentation. */

Source/astcenc_entry.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,19 @@ static const std::array<astcenc_preset_config, 6> preset_configs_low {{
136136
/**
137137
* @brief Validate CPU floating point meets assumptions made in the codec.
138138
*
139-
* The codec is written with the assumption that a float threaded through the @c if32 union will be
140-
* stored and reloaded as a 32-bit IEEE-754 float with round-to-nearest rounding. This is always the
141-
* case in an IEEE-754 compliant system, however not every system or compilation mode is actually
142-
* IEEE-754 compliant. This normally fails if the code is compiled with fast math enabled.
139+
* The codec is written with the assumption that float bit patterns are valid
140+
* IEEE754 values that are stored and reloaded with round-to-nearest rounding.
141+
* This is always the case in an IEEE-754 compliant system, however not every
142+
* system or compilation mode is actually IEEE-754 compliant. This normally
143+
* fails if the code is compiled with fast math enabled, for example.
143144
*
144-
* @return Return @c ASTCENC_SUCCESS if validated, otherwise an error on failure.
145+
* @return Return @c ASTCENC_SUCCESS if validated, an error on failure.
145146
*/
146147
static astcenc_error validate_cpu_float()
147148
{
148-
if32 p;
149149
volatile float xprec_testval = 2.51f;
150-
p.f = xprec_testval + 12582912.0f;
151-
float q = p.f - 12582912.0f;
150+
float store = xprec_testval + 12582912.0f;
151+
float q = store - 12582912.0f;
152152

153153
if (q != 3.0f)
154154
{

Source/astcenc_mathlib.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@
124124
to future vectorization.
125125
============================================================================ */
126126

127-
// Union for manipulation of float bit patterns
128-
typedef union
129-
{
130-
uint32_t u;
131-
int32_t s;
132-
float f;
133-
} if32;
134-
135127
// These are namespaced to avoid colliding with C standard library functions.
136128
namespace astc
137129
{
@@ -475,11 +467,10 @@ static inline float sqrt(float v)
475467
*/
476468
static inline float frexp(float v, int* expo)
477469
{
478-
if32 p;
479-
p.f = v;
480-
*expo = ((p.u >> 23) & 0xFF) - 126;
481-
p.u = (p.u & 0x807fffff) | 0x3f000000;
482-
return p.f;
470+
unsigned int iv = astc::float_as_uint(v);
471+
*expo = ((iv >> 23) & 0xFF) - 126;
472+
iv = (iv & 0x807fffff) | 0x3f000000;
473+
return astc::uint_as_float(iv);
483474
}
484475

485476
/**

Source/astcenc_mathlib_softfloat.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,14 @@ static sf16 sf32_to_sf16(sf32 inp, roundmode rmode)
395395
/* convert from soft-float to native-float */
396396
float sf16_to_float(uint16_t p)
397397
{
398-
if32 i;
399-
i.u = sf16_to_sf32(p);
400-
return i.f;
398+
return astc::uint_as_float(sf16_to_sf32(p));
401399
}
402400

403401
/* convert from native-float to soft-float */
404402
uint16_t float_to_sf16(float p)
405403
{
406-
if32 i;
407-
i.f = p;
408-
return sf32_to_sf16(i.u, SF_NEARESTEVEN);
404+
unsigned int ip = astc::float_as_uint(p);
405+
return sf32_to_sf16(ip, SF_NEARESTEVEN);
409406
}
410407

411408
#endif

Source/cmake_core.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,11 @@ macro(astcenc_set_properties ASTCENC_TARGET_NAME ASTCENC_VENEER_TYPE)
199199
if(${ASTCENC_UBSAN})
200200
target_compile_options(${ASTCENC_TARGET_NAME}
201201
PRIVATE
202-
$<${is_gnu_fe}:-fsanitize=undefined>
203-
$<${is_gnu_fe}:-fno-sanitize-recover=all>)
202+
$<${is_gnu_fe}:-fsanitize=undefined>)
204203

205204
target_link_options(${ASTCENC_TARGET_NAME}
206205
PRIVATE
207-
$<${is_gnu_fe}:-fsanitize=undefined>
208-
$<${is_gnu_fe}:-fno-sanitize-recover=all>)
206+
$<${is_gnu_fe}:-fsanitize=undefined>)
209207
endif()
210208

211209
if(NOT ${ASTCENC_INVARIANCE})

0 commit comments

Comments
 (0)