cocos2d/cocos/audio/android/audio_utils/include/audio_utils/primitives.h
static inline int16_t clamp16_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 -15 offset
     * is used to multiply by 32768.
     */
    static const float offset = (float)(3 << (22 - 15));
    /* zero = (0x10f << 22) =  0x43c00000 (not directly used) */
    static const int32_t limneg = (0x10f << 22) /*zero*/ - 32768; /* 0x43bf8000 */
    static const int32_t limpos = (0x10f << 22) /*zero*/ + 32767; /* 0x43c07fff */

    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)
        u.i = -32768;
    else if (u.i > limpos)
        u.i = 32767;
    return u.i; /* Return lower 16 bits, the part of interest in the significand. */
}
/*
 * Convert a IEEE 754 single precision float [-1.0, 1.0) to int16_t [-32768, 32767]
 * with clamping.  Note the open bound at 1.0, values within 1/65536 of 1.0 map
 * to 32767 instead of 32768 (early clamping due to the smaller positive integer subrange).
 *
 * Values outside the range [-1.0, 1.0) are properly clamped to -32768 and 32767,
 * including -Inf and +Inf. NaN will generally be treated either as -32768 or 32767,
 * 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).
 */


Values outside the range [-1.0, 1.0) are properly clamped to -32768 and 32767,
including -Inf and +Inf. NaN will generally be treated either as -32768 or 32767,
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).
Values outside the range [ - 1 . 0 1 . 0 ) are properly clamped to - 32768 and 32767 including - Inf and + Inf . NaN will generally be treated either as - 32768 or 32767 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 int16_t clamp16_from_float(float f)
{
    static const float offset = (float)(3 << (22 - 15));
    static const int32_t limneg = (0x10f << 22)  - 32768; 
    static const int32_t limpos = (0x10f << 22)  + 32767; 
    union {
        float f;
        int32_t i;
    } u;
    u.f = f + offset; 
    if (u.i < limneg)
        u.i = -32768;
    else if (u.i > limpos)
        u.i = 32767;
    return u.i; 
}
static inline int16_t clamp16_from_float ( float f ) { static const float offset = ( float ) ( 3 << ( 22 - 15 ) ) ; static const int32_t limneg = ( 0x10f << 22 ) - 32768 ; static const int32_t limpos = ( 0x10f << 22 ) + 32767 ; union { float f ; int32_t i ; } u ; u . f = f + offset ; if ( u . i < limneg ) u . i = -32768 ; else if ( u . i > limpos ) u . i = 32767 ; return u . i ; }
Convert a IEEE 754 single precision float [-1.0, 1.0) to int16_t [-32768, 32767]
 with clamping.
Convert a IEEE 754 single precision float [ - 1 . 0 1 . 0 ) to int16_t [ - 32768 32767 ] 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 -15 offset
     * is used to multiply by 32768.
     */ /* zero = (0x10f << 22) =  0x43c00000 (not directly used) */ /*zero*/ /* 0x43bf8000 */ /*zero*/ /* 0x43c07fff */ /* 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 16 bits, the part of interest in the significand. */