cocos2d/cocos/audio/android/audio_utils/include/audio_utils/primitives.h
static inline uint8_t clamp8_from_float(float f)
{
    /* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
     * floating point significand. The normal shift is 3<<22, but the -7 offset
     * is used to multiply by 128.
     */
    static const float offset = (float)((3 << (22 - 7)) + 1 /* to cancel -1.0 */);
    /* zero = (0x11f << 22) =  0x47c00000 */
    static const int32_t limneg = (0x11f << 22) /*zero*/;
    static const int32_t limpos = (0x11f << 22) /*zero*/ + 255; /* 0x47c000ff */

    union {
        float f;
        int32_t i;
    } u;

    u.f = f + offset; /* recenter valid range */
    /* Now the valid range is represented as integers between [limneg, limpos].
     * Clamp using the fact that float representation (as an integer) is an ordered set.
     */
    if (u.i < limneg)
        return 0;
    if (u.i > limpos)
        return 255;
    return u.i; /* Return lower 8 bits, the part of interest in the significand. */
}
/*
 * Convert a IEEE 754 single precision float [-1.0, 1.0) to uint8_t [0, 0xff]
 * with clamping.  Note the open bound at 1.0, values within 1/128 of 1.0 map
 * to 255 instead of 256 (early clamping due to the smaller positive integer subrange).
 *
 * Values outside the range [-1.0, 1.0) are properly clamped to 0 and 255,
 * including -Inf and +Inf. NaN will generally be treated either as 0 or 255,
 * depending on the sign bit inside NaN (whose representation is not unique).
 * Nevertheless, strictly speaking, NaN behavior should be considered undefined.
 *
 * Rounding of 0.5 lsb is to even (default for IEEE 754).
 */
Convert a IEEE 754 single precision float [-1.0, 1.0) to uint8_t [0, 0xff]
with clamping.  Note the open bound at 1.0, values within 1/128 of 1.0 map
to 255 instead of 256 (early clamping due to the smaller positive integer subrange).

Values outside the range [-1.0, 1.0) are properly clamped to 0 and 255,
including -Inf and +Inf. NaN will generally be treated either as 0 or 255,
depending on the sign bit inside NaN (whose representation is not unique).
Nevertheless, strictly speaking, NaN behavior should be considered undefined.

Rounding of 0.5 lsb is to even (default for IEEE 754).
Convert a IEEE 754 single precision float [ - 1 . 0 1 . 0 ) to uint8_t [ 0 0xff ] with clamping . Note the open bound at 1 . 0 values within 1 / 128 of 1 . 0 map to 255 instead of 256 ( early clamping due to the smaller positive integer subrange ) . Values outside the range [ - 1 . 0 1 . 0 ) are properly clamped to 0 and 255 including - Inf and + Inf . NaN will generally be treated either as 0 or 255 depending on the sign bit inside NaN ( whose representation is not unique ) . Nevertheless strictly speaking NaN behavior should be considered undefined . Rounding of 0 . 5 lsb is to even ( default for IEEE 754 ) .
static inline uint8_t clamp8_from_float(float f)
{
    static const float offset = (float)((3 << (22 - 7)) + 1 );
    static const int32_t limneg = (0x11f << 22) ;
    static const int32_t limpos = (0x11f << 22)  + 255; 
    union {
        float f;
        int32_t i;
    } u;
    u.f = f + offset; 
    if (u.i < limneg)
        return 0;
    if (u.i > limpos)
        return 255;
    return u.i; 
}
static inline uint8_t clamp8_from_float ( float f ) { static const float offset = ( float ) ( ( 3 << ( 22 - 7 ) ) + 1 ) ; static const int32_t limneg = ( 0x11f << 22 ) ; static const int32_t limpos = ( 0x11f << 22 ) + 255 ; union { float f ; int32_t i ; } u ; u . f = f + offset ; if ( u . i < limneg ) return 0 ; if ( u . i > limpos ) return 255 ; return u . i ; }
Convert a IEEE 754 single precision float [-1.0, 1.0) to uint8_t [0, 0xff]
 with clamping.
Convert a IEEE 754 single precision float [ - 1 . 0 1 . 0 ) to uint8_t [ 0 0xff ] with clamping .
/* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
     * floating point significand. The normal shift is 3<<22, but the -7 offset
     * is used to multiply by 128.
     */ /* to cancel -1.0 */ /* zero = (0x11f << 22) =  0x47c00000 */ /*zero*/ /*zero*/ /* 0x47c000ff */ /* recenter valid range */ /* Now the valid range is represented as integers between [limneg, limpos].
     * Clamp using the fact that float representation (as an integer) is an ordered set.
     */ /* Return lower 8 bits, the part of interest in the significand. */