[babl] directly use stdint types in half-float reference code
- From: Ãyvind KolÃs <ok src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [babl] directly use stdint types in half-float reference code
- Date: Wed, 2 May 2012 00:35:26 +0000 (UTC)
commit 0836ea79cf2158ad75e0f5d2ecbff0e844651821
Author: Ãyvind KolÃs <pippin gimp org>
Date: Wed May 2 02:34:42 2012 +0200
directly use stdint types in half-float reference code
babl/base/type-half.c | 144 +++++++++++++++++++++++--------------------------
1 files changed, 68 insertions(+), 76 deletions(-)
---
diff --git a/babl/base/type-half.c b/babl/base/type-half.c
index 798b7dd..e45e34a 100644
--- a/babl/base/type-half.c
+++ b/babl/base/type-half.c
@@ -68,23 +68,16 @@
#include "babl-ids.h"
#include "babl-base.h"
-
-#define mwSize int
-#define INT16_TYPE int16_t
-#define UINT16_TYPE uint16_t
-#define INT32_TYPE int32_t
-#define UINT32_TYPE uint32_t
-
static int next = 1; /* should be 0 for big endian */
//-----------------------------------------------------------------------------
-static void doubles2halfp(void *target, void *source, mwSize numel)
+static void doubles2halfp(void *target, void *source, long numel)
{
- UINT16_TYPE *hp = (UINT16_TYPE *) target; // Type pun output as an unsigned 16-bit int
- UINT32_TYPE *xp = (UINT32_TYPE *) source; // Type pun input as an unsigned 32-bit int
- UINT16_TYPE hs, he, hm;
- UINT32_TYPE x, xs, xe, xm;
+ uint16_t *hp = (uint16_t *) target; // Type pun output as an unsigned 16-bit int
+ uint32_t *xp = (uint32_t *) source; // Type pun input as an unsigned 32-bit int
+ uint16_t hs, he, hm;
+ uint32_t x, xs, xe, xm;
int hes;
xp += next; // Little Endian adjustment if necessary
@@ -95,39 +88,39 @@ static void doubles2halfp(void *target, void *source, mwSize numel)
while( numel-- ) {
x = *xp++; xp++; // The extra xp++ is to skip over the remaining 32 bits of the mantissa
if( (x & 0x7FFFFFFFu) == 0 ) { // Signed zero
- *hp++ = (UINT16_TYPE) (x >> 16); // Return the signed zero
+ *hp++ = (uint16_t) (x >> 16); // Return the signed zero
} else { // Not zero
xs = x & 0x80000000u; // Pick off sign bit
xe = x & 0x7FF00000u; // Pick off exponent bits
xm = x & 0x000FFFFFu; // Pick off mantissa bits
if( xe == 0 ) { // Denormal will underflow, return a signed zero
- *hp++ = (UINT16_TYPE) (xs >> 16);
+ *hp++ = (uint16_t) (xs >> 16);
} else if( xe == 0x7FF00000u ) { // Inf or NaN (all the exponent bits are set)
if( xm == 0 ) { // If mantissa is zero ...
- *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u); // Signed Inf
+ *hp++ = (uint16_t) ((xs >> 16) | 0x7C00u); // Signed Inf
} else {
- *hp++ = (UINT16_TYPE) 0xFE00u; // NaN, only 1st mantissa bit set
+ *hp++ = (uint16_t) 0xFE00u; // NaN, only 1st mantissa bit set
}
} else { // Normalized number
- hs = (UINT16_TYPE) (xs >> 16); // Sign bit
+ hs = (uint16_t) (xs >> 16); // Sign bit
hes = ((int)(xe >> 20)) - 1023 + 15; // Exponent unbias the double, then bias the halfp
if( hes >= 0x1F ) { // Overflow
- *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u); // Signed Inf
+ *hp++ = (uint16_t) ((xs >> 16) | 0x7C00u); // Signed Inf
} else if( hes <= 0 ) { // Underflow
if( (10 - hes) > 21 ) { // Mantissa shifted all the way off & no rounding possibility
- hm = (UINT16_TYPE) 0u; // Set mantissa to zero
+ hm = (uint16_t) 0u; // Set mantissa to zero
} else {
xm |= 0x00100000u; // Add the hidden leading bit
- hm = (UINT16_TYPE) (xm >> (11 - hes)); // Mantissa
+ hm = (uint16_t) (xm >> (11 - hes)); // Mantissa
if( (xm >> (10 - hes)) & 0x00000001u ) // Check for rounding
- hm += (UINT16_TYPE) 1u; // Round, might overflow into exp bit, but this is OK
+ hm += (uint16_t) 1u; // Round, might overflow into exp bit, but this is OK
}
*hp++ = (hs | hm); // Combine sign bit and mantissa bits, biased exponent is zero
} else {
- he = (UINT16_TYPE) (hes << 10); // Exponent
- hm = (UINT16_TYPE) (xm >> 10); // Mantissa
+ he = (uint16_t) (hes << 10); // Exponent
+ hm = (uint16_t) (xm >> 10); // Mantissa
if( xm & 0x00000200u ) // Check for rounding
- *hp++ = (hs | he | hm) + (UINT16_TYPE) 1u; // Round, might overflow to inf, this is OK
+ *hp++ = (hs | he | hm) + (uint16_t) 1u; // Round, might overflow to inf, this is OK
else
*hp++ = (hs | he | hm); // No rounding
}
@@ -139,13 +132,13 @@ static void doubles2halfp(void *target, void *source, mwSize numel)
#if 0
//-----------------------------------------------------------------------------
-static void halfp2singles(void *target, void *source, mwSize numel)
+static void halfp2singles(void *target, void *source, long numel)
{
- UINT16_TYPE *hp = (UINT16_TYPE *) source; // Type pun input as an unsigned 16-bit int
- UINT32_TYPE *xp = (UINT32_TYPE *) target; // Type pun output as an unsigned 32-bit int
- UINT16_TYPE h, hs, he, hm;
- UINT32_TYPE xs, xe, xm;
- INT32_TYPE xes;
+ uint16_t *hp = (uint16_t *) source; // Type pun input as an unsigned 16-bit int
+ uint32_t *xp = (uint32_t *) target; // Type pun output as an unsigned 32-bit int
+ uint16_t h, hs, he, hm;
+ uint32_t xs, xe, xm;
+ int32_t xes;
int e;
if( source == NULL || target == NULL ) // Nothing to convert (e.g., imag part of pure real)
@@ -153,7 +146,7 @@ static void halfp2singles(void *target, void *source, mwSize numel)
while( numel-- ) {
h = *hp++;
if( (h & 0x7FFFu) == 0 ) { // Signed zero
- *xp++ = ((UINT32_TYPE) h) << 16; // Return the signed zero
+ *xp++ = ((uint32_t) h) << 16; // Return the signed zero
} else { // Not zero
hs = h & 0x8000u; // Pick off sign bit
he = h & 0x7C00u; // Pick off exponent bits
@@ -164,34 +157,34 @@ static void halfp2singles(void *target, void *source, mwSize numel)
e++;
hm <<= 1;
} while( (hm & 0x0400u) == 0 ); // Shift until leading bit overflows into exponent bit
- xs = ((UINT32_TYPE) hs) << 16; // Sign bit
- xes = ((INT32_TYPE) (he >> 10)) - 15 + 127 - e; // Exponent unbias the halfp, then bias the single
- xe = (UINT32_TYPE) (xes << 23); // Exponent
- xm = ((UINT32_TYPE) (hm & 0x03FFu)) << 13; // Mantissa
+ xs = ((uint32_t) hs) << 16; // Sign bit
+ xes = ((int32_t) (he >> 10)) - 15 + 127 - e; // Exponent unbias the halfp, then bias the single
+ xe = (uint32_t) (xes << 23); // Exponent
+ xm = ((uint32_t) (hm & 0x03FFu)) << 13; // Mantissa
*xp++ = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits
} else if( he == 0x7C00u ) { // Inf or NaN (all the exponent bits are set)
if( hm == 0 ) { // If mantissa is zero ...
- *xp++ = (((UINT32_TYPE) hs) << 16) | ((UINT32_TYPE) 0x7F800000u); // Signed Inf
+ *xp++ = (((uint32_t) hs) << 16) | ((uint32_t) 0x7F800000u); // Signed Inf
} else {
- *xp++ = (UINT32_TYPE) 0xFFC00000u; // NaN, only 1st mantissa bit set
+ *xp++ = (uint32_t) 0xFFC00000u; // NaN, only 1st mantissa bit set
}
} else { // Normalized number
- xs = ((UINT32_TYPE) hs) << 16; // Sign bit
- xes = ((INT32_TYPE) (he >> 10)) - 15 + 127; // Exponent unbias the halfp, then bias the single
- xe = (UINT32_TYPE) (xes << 23); // Exponent
- xm = ((UINT32_TYPE) hm) << 13; // Mantissa
+ xs = ((uint32_t) hs) << 16; // Sign bit
+ xes = ((int32_t) (he >> 10)) - 15 + 127; // Exponent unbias the halfp, then bias the single
+ xe = (uint32_t) (xes << 23); // Exponent
+ xm = ((uint32_t) hm) << 13; // Mantissa
*xp++ = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits
}
}
}
}
-static void singles2halfp(void *target, void *source, mwSize numel)
+static void singles2halfp(void *target, void *source, long numel)
{
- UINT16_TYPE *hp = (UINT16_TYPE *) target; // Type pun output as an unsigned 16-bit int
- UINT32_TYPE *xp = (UINT32_TYPE *) source; // Type pun input as an unsigned 32-bit int
- UINT16_TYPE hs, he, hm;
- UINT32_TYPE x, xs, xe, xm;
+ uint16_t *hp = (uint16_t *) target; // Type pun output as an unsigned 16-bit int
+ uint32_t *xp = (uint32_t *) source; // Type pun input as an unsigned 32-bit int
+ uint16_t hs, he, hm;
+ uint32_t x, xs, xe, xm;
int hes;
if( source == NULL || target == NULL ) { // Nothing to convert (e.g., imag part of pure real)
@@ -200,39 +193,39 @@ static void singles2halfp(void *target, void *source, mwSize numel)
while( numel-- ) {
x = *xp++;
if( (x & 0x7FFFFFFFu) == 0 ) { // Signed zero
- *hp++ = (UINT16_TYPE) (x >> 16); // Return the signed zero
+ *hp++ = (uint16_t) (x >> 16); // Return the signed zero
} else { // Not zero
xs = x & 0x80000000u; // Pick off sign bit
xe = x & 0x7F800000u; // Pick off exponent bits
xm = x & 0x007FFFFFu; // Pick off mantissa bits
if( xe == 0 ) { // Denormal will underflow, return a signed zero
- *hp++ = (UINT16_TYPE) (xs >> 16);
+ *hp++ = (uint16_t) (xs >> 16);
} else if( xe == 0x7F800000u ) { // Inf or NaN (all the exponent bits are set)
if( xm == 0 ) { // If mantissa is zero ...
- *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u); // Signed Inf
+ *hp++ = (uint16_t) ((xs >> 16) | 0x7C00u); // Signed Inf
} else {
- *hp++ = (UINT16_TYPE) 0xFE00u; // NaN, only 1st mantissa bit set
+ *hp++ = (uint16_t) 0xFE00u; // NaN, only 1st mantissa bit set
}
} else { // Normalized number
- hs = (UINT16_TYPE) (xs >> 16); // Sign bit
+ hs = (uint16_t) (xs >> 16); // Sign bit
hes = ((int)(xe >> 23)) - 127 + 15; // Exponent unbias the single, then bias the halfp
if( hes >= 0x1F ) { // Overflow
- *hp++ = (UINT16_TYPE) ((xs >> 16) | 0x7C00u); // Signed Inf
+ *hp++ = (uint16_t) ((xs >> 16) | 0x7C00u); // Signed Inf
} else if( hes <= 0 ) { // Underflow
if( (14 - hes) > 24 ) { // Mantissa shifted all the way off & no rounding possibility
- hm = (UINT16_TYPE) 0u; // Set mantissa to zero
+ hm = (uint16_t) 0u; // Set mantissa to zero
} else {
xm |= 0x00800000u; // Add the hidden leading bit
- hm = (UINT16_TYPE) (xm >> (14 - hes)); // Mantissa
+ hm = (uint16_t) (xm >> (14 - hes)); // Mantissa
if( (xm >> (13 - hes)) & 0x00000001u ) // Check for rounding
- hm += (UINT16_TYPE) 1u; // Round, might overflow into exp bit, but this is OK
+ hm += (uint16_t) 1u; // Round, might overflow into exp bit, but this is OK
}
*hp++ = (hs | hm); // Combine sign bit and mantissa bits, biased exponent is zero
} else {
- he = (UINT16_TYPE) (hes << 10); // Exponent
- hm = (UINT16_TYPE) (xm >> 13); // Mantissa
+ he = (uint16_t) (hes << 10); // Exponent
+ hm = (uint16_t) (xm >> 13); // Mantissa
if( xm & 0x00001000u ) // Check for rounding
- *hp++ = (hs | he | hm) + (UINT16_TYPE) 1u; // Round, might overflow to inf, this is OK
+ *hp++ = (hs | he | hm) + (uint16_t) 1u; // Round, might overflow to inf, this is OK
else
*hp++ = (hs | he | hm); // No rounding
}
@@ -242,16 +235,15 @@ static void singles2halfp(void *target, void *source, mwSize numel)
}
#endif
-
//-----------------------------------------------------------------------------
-static void halfp2doubles(void *target, void *source, mwSize numel)
+static void halfp2doubles(void *target, void *source, long numel)
{
- UINT16_TYPE *hp = (UINT16_TYPE *) source; // Type pun input as an unsigned 16-bit int
- UINT32_TYPE *xp = (UINT32_TYPE *) target; // Type pun output as an unsigned 32-bit int
- UINT16_TYPE h, hs, he, hm;
- UINT32_TYPE xs, xe, xm;
- INT32_TYPE xes;
+ uint16_t *hp = (uint16_t *) source; // Type pun input as an unsigned 16-bit int
+ uint32_t *xp = (uint32_t *) target; // Type pun output as an unsigned 32-bit int
+ uint16_t h, hs, he, hm;
+ uint32_t xs, xe, xm;
+ int32_t xes;
int e;
xp += next; // Little Endian adjustment if necessary
@@ -261,7 +253,7 @@ static void halfp2doubles(void *target, void *source, mwSize numel)
while( numel-- ) {
h = *hp++;
if( (h & 0x7FFFu) == 0 ) { // Signed zero
- *xp++ = ((UINT32_TYPE) h) << 16; // Return the signed zero
+ *xp++ = ((uint32_t) h) << 16; // Return the signed zero
} else { // Not zero
hs = h & 0x8000u; // Pick off sign bit
he = h & 0x7C00u; // Pick off exponent bits
@@ -272,22 +264,22 @@ static void halfp2doubles(void *target, void *source, mwSize numel)
e++;
hm <<= 1;
} while( (hm & 0x0400u) == 0 ); // Shift until leading bit overflows into exponent bit
- xs = ((UINT32_TYPE) hs) << 16; // Sign bit
- xes = ((INT32_TYPE) (he >> 10)) - 15 + 1023 - e; // Exponent unbias the halfp, then bias the double
- xe = (UINT32_TYPE) (xes << 20); // Exponent
- xm = ((UINT32_TYPE) (hm & 0x03FFu)) << 10; // Mantissa
+ xs = ((uint32_t) hs) << 16; // Sign bit
+ xes = ((int32_t) (he >> 10)) - 15 + 1023 - e; // Exponent unbias the halfp, then bias the double
+ xe = (uint32_t) (xes << 20); // Exponent
+ xm = ((uint32_t) (hm & 0x03FFu)) << 10; // Mantissa
*xp++ = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits
} else if( he == 0x7C00u ) { // Inf or NaN (all the exponent bits are set)
if( hm == 0 ) { // If mantissa is zero ...
- *xp++ = (((UINT32_TYPE) hs) << 16) | ((UINT32_TYPE) 0x7FF00000u); // Signed Inf
+ *xp++ = (((uint32_t) hs) << 16) | ((uint32_t) 0x7FF00000u); // Signed Inf
} else {
- *xp++ = (UINT32_TYPE) 0xFFF80000u; // NaN, only the 1st mantissa bit set
+ *xp++ = (uint32_t) 0xFFF80000u; // NaN, only the 1st mantissa bit set
}
} else {
- xs = ((UINT32_TYPE) hs) << 16; // Sign bit
- xes = ((INT32_TYPE) (he >> 10)) - 15 + 1023; // Exponent unbias the halfp, then bias the double
- xe = (UINT32_TYPE) (xes << 20); // Exponent
- xm = ((UINT32_TYPE) hm) << 10; // Mantissa
+ xs = ((uint32_t) hs) << 16; // Sign bit
+ xes = ((int32_t) (he >> 10)) - 15 + 1023; // Exponent unbias the halfp, then bias the double
+ xe = (uint32_t) (xes << 20); // Exponent
+ xm = ((uint32_t) hm) << 10; // Mantissa
*xp++ = (xs | xe | xm); // Combine sign bit, exponent bits, and mantissa bits
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]