Skip to content

Commit 914fb26

Browse files
committed
Use local frexp
1 parent 18618d5 commit 914fb26

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

Source/astcenc_image.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static float float_to_lns(float p)
4141
}
4242

4343
int expo;
44-
float normfrac = frexpf(p, &expo);
44+
float normfrac = astc::frexp(p, &expo);
4545
float p1;
4646
if (expo < -13)
4747
{
@@ -56,11 +56,17 @@ static float float_to_lns(float p)
5656
}
5757

5858
if (p1 < 384.0f)
59+
{
5960
p1 *= 4.0f / 3.0f;
61+
}
6062
else if (p1 <= 1408.0f)
63+
{
6164
p1 += 128.0f;
65+
}
6266
else
67+
{
6368
p1 = (p1 + 512.0f) * (4.0f / 5.0f);
69+
}
6470

6571
p1 += ((float)expo) * 2048.0f;
6672
return p1 + 1.0f;
@@ -72,15 +78,23 @@ static uint16_t lns_to_sf16(uint16_t p)
7278
uint16_t ec = p >> 11;
7379
uint16_t mt;
7480
if (mc < 512)
81+
{
7582
mt = 3 * mc;
83+
}
7684
else if (mc < 1536)
85+
{
7786
mt = 4 * mc - 512;
87+
}
7888
else
89+
{
7990
mt = 5 * mc - 2048;
91+
}
8092

8193
uint16_t res = (ec << 10) | (mt >> 3);
8294
if (res > 0x7BFF)
95+
{
8396
res = 0x7BFF;
97+
}
8498
return res;
8599
}
86100

@@ -91,10 +105,14 @@ static uint16_t lns_to_sf16(uint16_t p)
91105
uint16_t unorm16_to_sf16(uint16_t p)
92106
{
93107
if (p == 0xFFFF)
108+
{
94109
return 0x3C00; // value of 1.0
110+
}
95111

96112
if (p < 4)
113+
{
97114
return p << 8;
115+
}
98116

99117
int lz = clz32(p) - 16;
100118
p <<= (lz + 1);

Source/astcenc_mathlib.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@
9292
to future vectorization.
9393
============================================================================ */
9494

95+
// Union for manipulation of float bit patterns
96+
typedef union
97+
{
98+
uint32_t u;
99+
int32_t s;
100+
float f;
101+
} if32;
102+
95103
// These are namespaced to avoid colliding with C standard library functions.
96104
namespace astc
97105
{
@@ -405,6 +413,23 @@ static inline float sqrt(float v)
405413
return std::sqrt(v);
406414
}
407415

416+
/**
417+
* @brief Extract mantissa and exponent of a float value.
418+
*
419+
* @param v The input value.
420+
* @param[out] expo The output exponent.
421+
*
422+
* @return The mantissa.
423+
*/
424+
static inline float frexp(float v, int* expo)
425+
{
426+
if32 p;
427+
p.f = v;
428+
*expo = ((p.u >> 23) & 0xFF) - 126;
429+
p.u = (p.u & 0x807fffff) | 0x3f000000;
430+
return p.f;
431+
}
432+
408433
/**
409434
* @brief Log base 2, linearized from 2^-14.
410435
*
@@ -580,13 +605,6 @@ static inline float3 normalize(float3 p) { return p * astc::rsqrt(dot(p, p)); }
580605
/* ============================================================================
581606
Softfloat library with fp32 and fp16 conversion functionality.
582607
============================================================================ */
583-
typedef union if32_
584-
{
585-
uint32_t u;
586-
int32_t s;
587-
float f;
588-
} if32;
589-
590608
uint32_t clz32(uint32_t p);
591609

592610
/* sized soft-float types. These are mapped to the sized integer

0 commit comments

Comments
 (0)