Created
June 1, 2017 00:20
-
-
Save ashafq/34c04cc357df1eec4161ad7dd7905c0b to your computer and use it in GitHub Desktop.
Convert an array of float to int16_t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <assert.h> | |
#include <immintrin.h> | |
static void v_f32_to_s1x15i(int16_t *dst, const float *src, size_t len) | |
{ | |
const __m128 minval = _mm_set1_ps(-1.0F); | |
const __m128 maxval = _mm_set1_ps(+0x7fffff8p-27F); | |
const __m128 scale = _mm_set1_ps(32768.0F); | |
__m128 *srcp = (__m128 *) __builtin_assume_aligned(src, 16); | |
__m128i *dstp = (__m128i *) __builtin_assume_aligned(dst, 16); | |
assert(len % 8 == 0 && "len must be a multiple of 8"); | |
for (size_t i = 0; i < len; i+=8) { | |
/* Load samples */ | |
__m128 x1 = _mm_load_ps((float *) srcp++); | |
/* Clamp values */ | |
__m128 xc1 = _mm_max_ps(_mm_min_ps(x1, maxval), minval); | |
/* Multiply by scaling factor */ | |
__m128 y1 = _mm_mul_ps(xc1, scale); | |
/* Convert to packed 32 bit integer */ | |
__m128i yc1 = _mm_cvtps_epi32(y1); | |
/* Load samples */ | |
__m128 x2 = _mm_load_ps((float *) srcp++); | |
/* Clamp values */ | |
__m128 xc2 = _mm_max_ps(_mm_min_ps(x2, maxval), minval); | |
/* Multiply by scaling factor */ | |
__m128 y2 = _mm_mul_ps(xc2, scale); | |
/* Convert to packed 32 bit integer */ | |
__m128i yc2= _mm_cvtps_epi32(y2); | |
/* Pack them down to 16 bits */ | |
__m128i yp = _mm_packs_epi32(yc1, yc2); | |
/* Store output */ | |
_mm_store_si128(dstp++, yp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment