jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1193 lines
32KB

  1. /*
  2. Copyright (C) 2000 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #define _ISOC9X_SOURCE 1
  16. #define _ISOC99_SOURCE 1
  17. #define __USE_ISOC9X 1
  18. #define __USE_ISOC99 1
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <memory.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <limits.h>
  26. #ifdef __linux__
  27. #include <endian.h>
  28. #endif
  29. #include "memops.h"
  30. #if defined (__SSE2__) && !defined (__sun__)
  31. #include <emmintrin.h>
  32. #ifdef __SSE4_1__
  33. #include <smmintrin.h>
  34. #endif
  35. #endif
  36. #ifdef __ARM_NEON__
  37. #include <arm_neon.h>
  38. #endif
  39. /* Notes about these *_SCALING values.
  40. the MAX_<N>BIT values are floating point. when multiplied by
  41. a full-scale normalized floating point sample value (-1.0..+1.0)
  42. they should give the maxium value representable with an integer
  43. sample type of N bits. Note that this is asymmetric. Sample ranges
  44. for signed integer, 2's complement values are -(2^(N-1) to +(2^(N-1)-1)
  45. Complications
  46. -------------
  47. If we use +2^(N-1) for the scaling factors, we run into a problem:
  48. if we start with a normalized float value of -1.0, scaling
  49. to 24 bits would give -8388608 (-2^23), which is ideal.
  50. But with +1.0, we get +8388608, which is technically out of range.
  51. We never multiply a full range normalized value by this constant,
  52. but we could multiply it by a positive value that is close enough to +1.0
  53. to produce a value > +(2^(N-1)-1.
  54. There is no way around this paradox without wasting CPU cycles to determine
  55. which scaling factor to use (i.e. determine if its negative or not,
  56. use the right factor).
  57. So, for now (October 2008) we use 2^(N-1)-1 as the scaling factor.
  58. */
  59. #define SAMPLE_24BIT_SCALING 8388607.0f
  60. #define SAMPLE_16BIT_SCALING 32767.0f
  61. /* these are just values to use if the floating point value was out of range
  62. advice from Fons Adriaensen: make the limits symmetrical
  63. */
  64. #define SAMPLE_24BIT_MAX 8388607
  65. #define SAMPLE_24BIT_MIN -8388607
  66. #define SAMPLE_24BIT_MAX_F 8388607.0f
  67. #define SAMPLE_24BIT_MIN_F -8388607.0f
  68. #define SAMPLE_16BIT_MAX 32767
  69. #define SAMPLE_16BIT_MIN -32767
  70. #define SAMPLE_16BIT_MAX_F 32767.0f
  71. #define SAMPLE_16BIT_MIN_F -32767.0f
  72. /* these mark the outer edges of the range considered "within" range
  73. for a floating point sample value. values outside (and on the boundaries)
  74. of this range will be clipped before conversion; values within this
  75. range will be scaled to appropriate values for the target sample
  76. type.
  77. */
  78. #define NORMALIZED_FLOAT_MIN -1.0f
  79. #define NORMALIZED_FLOAT_MAX 1.0f
  80. /* define this in case we end up on a platform that is missing
  81. the real lrintf functions
  82. */
  83. #define f_round(f) lrintf(f)
  84. #define float_16(s, d)\
  85. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  86. (d) = SAMPLE_16BIT_MIN;\
  87. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  88. (d) = SAMPLE_16BIT_MAX;\
  89. } else {\
  90. (d) = f_round ((s) * SAMPLE_16BIT_SCALING);\
  91. }
  92. /* call this when "s" has already been scaled (e.g. when dithering)
  93. */
  94. #define float_16_scaled(s, d)\
  95. if ((s) <= SAMPLE_16BIT_MIN_F) {\
  96. (d) = SAMPLE_16BIT_MIN_F;\
  97. } else if ((s) >= SAMPLE_16BIT_MAX_F) { \
  98. (d) = SAMPLE_16BIT_MAX;\
  99. } else {\
  100. (d) = f_round ((s));\
  101. }
  102. #define float_24u32(s, d) \
  103. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  104. (d) = SAMPLE_24BIT_MIN << 8;\
  105. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  106. (d) = SAMPLE_24BIT_MAX << 8;\
  107. } else {\
  108. (d) = f_round ((s) * SAMPLE_24BIT_SCALING) << 8;\
  109. }
  110. /* call this when "s" has already been scaled (e.g. when dithering)
  111. */
  112. #define float_24u32_scaled(s, d)\
  113. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  114. (d) = SAMPLE_24BIT_MIN << 8;\
  115. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  116. (d) = SAMPLE_24BIT_MAX << 8; \
  117. } else {\
  118. (d) = f_round ((s)) << 8; \
  119. }
  120. #define float_24(s, d) \
  121. if ((s) <= NORMALIZED_FLOAT_MIN) {\
  122. (d) = SAMPLE_24BIT_MIN;\
  123. } else if ((s) >= NORMALIZED_FLOAT_MAX) {\
  124. (d) = SAMPLE_24BIT_MAX;\
  125. } else {\
  126. (d) = f_round ((s) * SAMPLE_24BIT_SCALING);\
  127. }
  128. /* call this when "s" has already been scaled (e.g. when dithering)
  129. */
  130. #define float_24_scaled(s, d)\
  131. if ((s) <= SAMPLE_24BIT_MIN_F) {\
  132. (d) = SAMPLE_24BIT_MIN;\
  133. } else if ((s) >= SAMPLE_24BIT_MAX_F) { \
  134. (d) = SAMPLE_24BIT_MAX; \
  135. } else {\
  136. (d) = f_round ((s)); \
  137. }
  138. #if defined (__SSE2__) && !defined (__sun__)
  139. /* generates same as _mm_set_ps(1.f, 1.f, 1f., 1f) but faster */
  140. static inline __m128 gen_one(void)
  141. {
  142. volatile __m128i x = { 0 }; /* shut up, GCC */
  143. __m128i ones = _mm_cmpeq_epi32(x, x);
  144. return (__m128)_mm_slli_epi32 (_mm_srli_epi32(ones, 25), 23);
  145. }
  146. static inline __m128 clip(__m128 s, __m128 min, __m128 max)
  147. {
  148. return _mm_min_ps(max, _mm_max_ps(s, min));
  149. }
  150. static inline __m128i float_24_sse(__m128 s)
  151. {
  152. const __m128 upper_bound = gen_one(); /* NORMALIZED_FLOAT_MAX */
  153. const __m128 lower_bound = _mm_sub_ps(_mm_setzero_ps(), upper_bound);
  154. __m128 clipped = clip(s, lower_bound, upper_bound);
  155. __m128 scaled = _mm_mul_ps(clipped, _mm_set1_ps(SAMPLE_24BIT_SCALING));
  156. return _mm_cvtps_epi32(scaled);
  157. }
  158. #endif
  159. #ifdef __ARM_NEON__
  160. static inline float32x4_t clip(float32x4_t s, float32x4_t min, float32x4_t max)
  161. {
  162. return vminq_f32(max, vmaxq_f32(s, min));
  163. }
  164. static inline int32x4_t float_24_neon(float32x4_t s)
  165. {
  166. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  167. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  168. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  169. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_24BIT_SCALING));
  170. return vcvtq_s32_f32(scaled);
  171. }
  172. static inline int16x4_t float_16_neon(float32x4_t s)
  173. {
  174. const float32x4_t upper_bound = vdupq_n_f32(NORMALIZED_FLOAT_MAX);
  175. const float32x4_t lower_bound = vdupq_n_f32(NORMALIZED_FLOAT_MIN);
  176. float32x4_t clipped = clip(s, lower_bound, upper_bound);
  177. float32x4_t scaled = vmulq_f32(clipped, vdupq_n_f32(SAMPLE_16BIT_SCALING));
  178. return vmovn_s32(vcvtq_s32_f32(scaled));
  179. }
  180. #endif
  181. /* Linear Congruential noise generator. From the music-dsp list
  182. * less random than rand(), but good enough and 10x faster
  183. */
  184. static unsigned int seed = 22222;
  185. static inline unsigned int fast_rand() {
  186. seed = (seed * 196314165) + 907633515;
  187. return seed;
  188. }
  189. /* functions for native float sample data */
  190. void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
  191. while (nsamples--) {
  192. *dst = *((float *) src);
  193. dst++;
  194. src += src_skip;
  195. }
  196. }
  197. void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
  198. while (nsamples--) {
  199. *((float *) dst) = *src;
  200. dst += dst_skip;
  201. src++;
  202. }
  203. }
  204. /* NOTES on function naming:
  205. foo_bar_d<TYPE>_s<TYPE>
  206. the "d<TYPE>" component defines the destination type for the operation
  207. the "s<TYPE>" component defines the source type for the operation
  208. TYPE can be one of:
  209. S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
  210. Ss - like S but reverse endian from the host CPU
  211. 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
  212. 32u24s - like 32u24 but reverse endian from the host CPU
  213. 24 - sample is an signed 24 bit integer value
  214. 24s - like 24 but reverse endian from the host CPU
  215. 16 - sample is an signed 16 bit integer value
  216. 16s - like 16 but reverse endian from the host CPU
  217. For obvious reasons, the reverse endian versions only show as source types.
  218. This covers all known sample formats at 16 bits or larger.
  219. */
  220. /* functions for native integer sample data */
  221. void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  222. {
  223. #ifdef __ARM_NEON__
  224. unsigned long unrolled = nsamples / 4;
  225. nsamples = nsamples & 3;
  226. while (unrolled--) {
  227. float32x4_t samples = vld1q_f32(src);
  228. int32x4_t converted = float_24_neon(samples);
  229. int32x4_t shifted = vshlq_n_s32(converted, 8);
  230. shifted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(shifted)));
  231. switch(dst_skip) {
  232. case 4:
  233. vst1q_s32((int32_t*)dst, shifted);
  234. break;
  235. default:
  236. vst1q_lane_s32((int32_t*)(dst), shifted, 0);
  237. vst1q_lane_s32((int32_t*)(dst+dst_skip), shifted, 1);
  238. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), shifted, 2);
  239. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), shifted, 3);
  240. break;
  241. }
  242. dst += 4*dst_skip;
  243. src+= 4;
  244. }
  245. #endif
  246. int32_t z;
  247. while (nsamples--) {
  248. float_24u32 (*src, z);
  249. #if __BYTE_ORDER == __LITTLE_ENDIAN
  250. dst[0]=(char)(z>>24);
  251. dst[1]=(char)(z>>16);
  252. dst[2]=(char)(z>>8);
  253. dst[3]=(char)(z);
  254. #elif __BYTE_ORDER == __BIG_ENDIAN
  255. dst[0]=(char)(z);
  256. dst[1]=(char)(z>>8);
  257. dst[2]=(char)(z>>16);
  258. dst[3]=(char)(z>>24);
  259. #endif
  260. dst += dst_skip;
  261. src++;
  262. }
  263. }
  264. void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  265. {
  266. #if defined (__SSE2__) && !defined (__sun__)
  267. __m128 int_max = _mm_set1_ps(SAMPLE_24BIT_MAX_F);
  268. __m128 int_min = _mm_sub_ps(_mm_setzero_ps(), int_max);
  269. __m128 factor = int_max;
  270. unsigned long unrolled = nsamples / 4;
  271. nsamples = nsamples & 3;
  272. while (unrolled--) {
  273. __m128 in = _mm_load_ps(src);
  274. __m128 scaled = _mm_mul_ps(in, factor);
  275. __m128 clipped = clip(scaled, int_min, int_max);
  276. __m128i y = _mm_cvttps_epi32(clipped);
  277. __m128i shifted = _mm_slli_epi32(y, 8);
  278. #ifdef __SSE4_1__
  279. *(int32_t*)dst = _mm_extract_epi32(shifted, 0);
  280. *(int32_t*)(dst+dst_skip) = _mm_extract_epi32(shifted, 1);
  281. *(int32_t*)(dst+2*dst_skip) = _mm_extract_epi32(shifted, 2);
  282. *(int32_t*)(dst+3*dst_skip) = _mm_extract_epi32(shifted, 3);
  283. #else
  284. __m128i shuffled1 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(0, 3, 2, 1));
  285. __m128i shuffled2 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(1, 0, 3, 2));
  286. __m128i shuffled3 = _mm_shuffle_epi32(shifted, _MM_SHUFFLE(2, 1, 0, 3));
  287. _mm_store_ss((float*)dst, (__m128)shifted);
  288. _mm_store_ss((float*)(dst+dst_skip), (__m128)shuffled1);
  289. _mm_store_ss((float*)(dst+2*dst_skip), (__m128)shuffled2);
  290. _mm_store_ss((float*)(dst+3*dst_skip), (__m128)shuffled3);
  291. #endif
  292. dst += 4*dst_skip;
  293. src+= 4;
  294. }
  295. while (nsamples--) {
  296. __m128 in = _mm_load_ss(src);
  297. __m128 scaled = _mm_mul_ss(in, factor);
  298. __m128 clipped = _mm_min_ss(int_max, _mm_max_ss(scaled, int_min));
  299. int y = _mm_cvttss_si32(clipped);
  300. *((int *) dst) = y<<8;
  301. dst += dst_skip;
  302. src++;
  303. }
  304. #elif defined(__ARM_NEON__)
  305. unsigned long unrolled = nsamples / 4;
  306. nsamples = nsamples & 3;
  307. while (unrolled--) {
  308. float32x4_t samples = vld1q_f32(src);
  309. int32x4_t converted = float_24_neon(samples);
  310. int32x4_t shifted = vshlq_n_s32(converted, 8);
  311. switch(dst_skip) {
  312. case 4:
  313. vst1q_s32((int32_t*)dst, shifted);
  314. break;
  315. default:
  316. vst1q_lane_s32((int32_t*)(dst), shifted, 0);
  317. vst1q_lane_s32((int32_t*)(dst+dst_skip), shifted, 1);
  318. vst1q_lane_s32((int32_t*)(dst+2*dst_skip), shifted, 2);
  319. vst1q_lane_s32((int32_t*)(dst+3*dst_skip), shifted, 3);
  320. break;
  321. }
  322. dst += 4*dst_skip;
  323. src+= 4;
  324. }
  325. #endif
  326. #if !defined (__SSE2__)
  327. while (nsamples--) {
  328. float_24u32 (*src, *((int32_t*) dst));
  329. dst += dst_skip;
  330. src++;
  331. }
  332. #endif
  333. }
  334. void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  335. {
  336. #ifdef __ARM_NEON__
  337. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  338. unsigned long unrolled = nsamples / 4;
  339. while (unrolled--) {
  340. int32x4_t src128;
  341. switch(src_skip)
  342. {
  343. case 4:
  344. src128 = vld1q_s32((int32_t*)src);
  345. break;
  346. case 8:
  347. src128 = vld2q_s32((int32_t*)src).val[0];
  348. break;
  349. default:
  350. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  351. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  352. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  353. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  354. break;
  355. }
  356. src128 = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(src128)));
  357. int32x4_t shifted = vshrq_n_s32(src128, 8);
  358. float32x4_t as_float = vcvtq_f32_s32(shifted);
  359. float32x4_t divided = vmulq_f32(as_float, factor);
  360. vst1q_f32(dst, divided);
  361. src += 4*src_skip;
  362. dst += 4;
  363. }
  364. nsamples = nsamples & 3;
  365. #endif
  366. /* ALERT: signed sign-extension portability !!! */
  367. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  368. while (nsamples--) {
  369. int x;
  370. #if __BYTE_ORDER == __LITTLE_ENDIAN
  371. x = (unsigned char)(src[0]);
  372. x <<= 8;
  373. x |= (unsigned char)(src[1]);
  374. x <<= 8;
  375. x |= (unsigned char)(src[2]);
  376. x <<= 8;
  377. x |= (unsigned char)(src[3]);
  378. #elif __BYTE_ORDER == __BIG_ENDIAN
  379. x = (unsigned char)(src[3]);
  380. x <<= 8;
  381. x |= (unsigned char)(src[2]);
  382. x <<= 8;
  383. x |= (unsigned char)(src[1]);
  384. x <<= 8;
  385. x |= (unsigned char)(src[0]);
  386. #endif
  387. *dst = (x >> 8) * scaling;
  388. dst++;
  389. src += src_skip;
  390. }
  391. }
  392. void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  393. {
  394. #if defined (__SSE2__) && !defined (__sun__)
  395. unsigned long unrolled = nsamples / 4;
  396. static float inv_sample_max_24bit = 1.0 / SAMPLE_24BIT_SCALING;
  397. __m128 factor = _mm_set1_ps(inv_sample_max_24bit);
  398. while (unrolled--)
  399. {
  400. int i1 = *((int *) src);
  401. src+= src_skip;
  402. int i2 = *((int *) src);
  403. src+= src_skip;
  404. int i3 = *((int *) src);
  405. src+= src_skip;
  406. int i4 = *((int *) src);
  407. src+= src_skip;
  408. __m128i src = _mm_set_epi32(i4, i3, i2, i1);
  409. __m128i shifted = _mm_srai_epi32(src, 8);
  410. __m128 as_float = _mm_cvtepi32_ps(shifted);
  411. __m128 divided = _mm_mul_ps(as_float, factor);
  412. _mm_storeu_ps(dst, divided);
  413. dst += 4;
  414. }
  415. nsamples = nsamples & 3;
  416. #elif defined(__ARM_NEON__)
  417. unsigned long unrolled = nsamples / 4;
  418. float32x4_t factor = vdupq_n_f32(1.0 / SAMPLE_24BIT_SCALING);
  419. while (unrolled--) {
  420. int32x4_t src128;
  421. switch(src_skip) {
  422. case 4:
  423. src128 = vld1q_s32((int32_t*)src);
  424. break;
  425. case 8:
  426. src128 = vld2q_s32((int32_t*)src).val[0];
  427. break;
  428. default:
  429. src128 = vld1q_lane_s32((int32_t*)src, src128, 0);
  430. src128 = vld1q_lane_s32((int32_t*)(src+src_skip), src128, 1);
  431. src128 = vld1q_lane_s32((int32_t*)(src+2*src_skip), src128, 2);
  432. src128 = vld1q_lane_s32((int32_t*)(src+3*src_skip), src128, 3);
  433. break;
  434. }
  435. int32x4_t shifted = vshrq_n_s32(src128, 8);
  436. float32x4_t as_float = vcvtq_f32_s32(shifted);
  437. float32x4_t divided = vmulq_f32(as_float, factor);
  438. vst1q_f32(dst, divided);
  439. src += 4*src_skip;
  440. dst += 4;
  441. }
  442. nsamples = nsamples & 3;
  443. #endif
  444. /* ALERT: signed sign-extension portability !!! */
  445. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  446. while (nsamples--) {
  447. *dst = (*((int *) src) >> 8) * scaling;
  448. dst++;
  449. src += src_skip;
  450. }
  451. }
  452. void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  453. {
  454. #ifdef __ARM_NEON__
  455. unsigned long unrolled = nsamples / 4;
  456. while (unrolled--) {
  457. int i;
  458. int32_t z[4];
  459. float32x4_t samples = vld1q_f32(src);
  460. int32x4_t converted = float_24_neon(samples);
  461. converted = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(converted)));
  462. vst1q_s32(z, converted);
  463. for (i = 0; i != 4; ++i) {
  464. memcpy (dst, ((char*)(z+i))+1, 3);
  465. dst += dst_skip;
  466. }
  467. src += 4;
  468. }
  469. nsamples = nsamples & 3;
  470. #endif
  471. int32_t z;
  472. while (nsamples--) {
  473. float_24 (*src, z);
  474. #if __BYTE_ORDER == __LITTLE_ENDIAN
  475. dst[0]=(char)(z>>16);
  476. dst[1]=(char)(z>>8);
  477. dst[2]=(char)(z);
  478. #elif __BYTE_ORDER == __BIG_ENDIAN
  479. dst[0]=(char)(z);
  480. dst[1]=(char)(z>>8);
  481. dst[2]=(char)(z>>16);
  482. #endif
  483. dst += dst_skip;
  484. src++;
  485. }
  486. }
  487. void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  488. {
  489. #if defined (__SSE2__) && !defined (__sun__)
  490. _MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
  491. while (nsamples >= 4) {
  492. int i;
  493. int32_t z[4];
  494. __m128 samples = _mm_loadu_ps(src);
  495. __m128i converted = float_24_sse(samples);
  496. #ifdef __SSE4_1__
  497. z[0] = _mm_extract_epi32(converted, 0);
  498. z[1] = _mm_extract_epi32(converted, 1);
  499. z[2] = _mm_extract_epi32(converted, 2);
  500. z[3] = _mm_extract_epi32(converted, 3);
  501. #else
  502. __m128i shuffled1 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(0, 3, 2, 1));
  503. __m128i shuffled2 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(1, 0, 3, 2));
  504. __m128i shuffled3 = _mm_shuffle_epi32(converted, _MM_SHUFFLE(2, 1, 0, 3));
  505. _mm_store_ss((float*)z, (__m128)converted);
  506. _mm_store_ss((float*)z+1, (__m128)shuffled1);
  507. _mm_store_ss((float*)z+2, (__m128)shuffled2);
  508. _mm_store_ss((float*)z+3, (__m128)shuffled3);
  509. #endif
  510. for (i = 0; i != 4; ++i) {
  511. memcpy (dst, z+i, 3);
  512. dst += dst_skip;
  513. }
  514. nsamples -= 4;
  515. src += 4;
  516. }
  517. #elif defined(__ARM_NEON__)
  518. unsigned long unrolled = nsamples / 4;
  519. while (unrolled--) {
  520. int i;
  521. int32_t z[4];
  522. float32x4_t samples = vld1q_f32(src);
  523. int32x4_t converted = float_24_neon(samples);
  524. vst1q_s32(z, converted);
  525. for (i = 0; i != 4; ++i) {
  526. memcpy (dst, z+i, 3);
  527. dst += dst_skip;
  528. }
  529. src += 4;
  530. }
  531. nsamples = nsamples & 3;
  532. #endif
  533. int32_t z;
  534. while (nsamples--) {
  535. float_24 (*src, z);
  536. #if __BYTE_ORDER == __LITTLE_ENDIAN
  537. memcpy (dst, &z, 3);
  538. #elif __BYTE_ORDER == __BIG_ENDIAN
  539. memcpy (dst, (char *)&z + 1, 3);
  540. #endif
  541. dst += dst_skip;
  542. src++;
  543. }
  544. }
  545. void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  546. {
  547. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_24BIT_SCALING;
  548. #ifdef __ARM_NEON__
  549. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  550. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  551. int32_t x[4];
  552. memset(x, 0, sizeof(x));
  553. unsigned long unrolled = nsamples / 4;
  554. while (unrolled--) {
  555. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  556. // right aligned / inverse sequence below -> *256
  557. memcpy(((char*)&x[0])+1, src, 3);
  558. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  559. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  560. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  561. #else
  562. memcpy(&x[0], src, 3);
  563. memcpy(&x[1], src+src_skip, 3);
  564. memcpy(&x[2], src+2*src_skip, 3);
  565. memcpy(&x[3], src+3*src_skip, 3);
  566. #endif
  567. src += 4 * src_skip;
  568. int32x4_t source = vld1q_s32(x);
  569. source = vreinterpretq_s32_u8(vrev32q_u8(vreinterpretq_u8_s32(source)));
  570. float32x4_t converted = vcvtq_f32_s32(source);
  571. float32x4_t scaled = vmulq_f32(converted, vscaling);
  572. vst1q_f32(dst, scaled);
  573. dst += 4;
  574. }
  575. nsamples = nsamples & 3;
  576. #endif
  577. /* ALERT: signed sign-extension portability !!! */
  578. while (nsamples--) {
  579. int x;
  580. #if __BYTE_ORDER == __LITTLE_ENDIAN
  581. x = (unsigned char)(src[0]);
  582. x <<= 8;
  583. x |= (unsigned char)(src[1]);
  584. x <<= 8;
  585. x |= (unsigned char)(src[2]);
  586. /* correct sign bit and the rest of the top byte */
  587. if (src[0] & 0x80) {
  588. x |= 0xff << 24;
  589. }
  590. #elif __BYTE_ORDER == __BIG_ENDIAN
  591. x = (unsigned char)(src[2]);
  592. x <<= 8;
  593. x |= (unsigned char)(src[1]);
  594. x <<= 8;
  595. x |= (unsigned char)(src[0]);
  596. /* correct sign bit and the rest of the top byte */
  597. if (src[2] & 0x80) {
  598. x |= 0xff << 24;
  599. }
  600. #endif
  601. *dst = x * scaling;
  602. dst++;
  603. src += src_skip;
  604. }
  605. }
  606. void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  607. {
  608. const jack_default_audio_sample_t scaling = 1.f/SAMPLE_24BIT_SCALING;
  609. #if defined (__SSE2__) && !defined (__sun__)
  610. const __m128 scaling_block = _mm_set_ps1(scaling);
  611. while (nsamples >= 4) {
  612. int x0, x1, x2, x3;
  613. memcpy((char*)&x0 + 1, src, 3);
  614. memcpy((char*)&x1 + 1, src+src_skip, 3);
  615. memcpy((char*)&x2 + 1, src+2*src_skip, 3);
  616. memcpy((char*)&x3 + 1, src+3*src_skip, 3);
  617. src += 4 * src_skip;
  618. const __m128i block_i = _mm_set_epi32(x3, x2, x1, x0);
  619. const __m128i shifted = _mm_srai_epi32(block_i, 8);
  620. const __m128 converted = _mm_cvtepi32_ps (shifted);
  621. const __m128 scaled = _mm_mul_ps(converted, scaling_block);
  622. _mm_storeu_ps(dst, scaled);
  623. dst += 4;
  624. nsamples -= 4;
  625. }
  626. #elif defined(__ARM_NEON__)
  627. // we shift 8 to the right by dividing by 256.0 -> no sign extra handling
  628. const float32x4_t vscaling = vdupq_n_f32(scaling/256.0);
  629. int32_t x[4];
  630. memset(x, 0, sizeof(x));
  631. unsigned long unrolled = nsamples / 4;
  632. while (unrolled--) {
  633. #if __BYTE_ORDER == __BIG_ENDIAN /* ARM big endian?? */
  634. // left aligned -> *256
  635. memcpy(&x[0], src, 3);
  636. memcpy(&x[1], src+src_skip, 3);
  637. memcpy(&x[2], src+2*src_skip, 3);
  638. memcpy(&x[3], src+3*src_skip, 3);
  639. #else
  640. memcpy(((char*)&x[0])+1, src, 3);
  641. memcpy(((char*)&x[1])+1, src+src_skip, 3);
  642. memcpy(((char*)&x[2])+1, src+2*src_skip, 3);
  643. memcpy(((char*)&x[3])+1, src+3*src_skip, 3);
  644. #endif
  645. src += 4 * src_skip;
  646. int32x4_t source = vld1q_s32(x);
  647. float32x4_t converted = vcvtq_f32_s32(source);
  648. float32x4_t scaled = vmulq_f32(converted, vscaling);
  649. vst1q_f32(dst, scaled);
  650. dst += 4;
  651. }
  652. nsamples = nsamples & 3;
  653. #endif
  654. while (nsamples--) {
  655. int x;
  656. #if __BYTE_ORDER == __LITTLE_ENDIAN
  657. memcpy((char*)&x + 1, src, 3);
  658. #elif __BYTE_ORDER == __BIG_ENDIAN
  659. memcpy(&x, src, 3);
  660. #endif
  661. x >>= 8;
  662. *dst = x * scaling;
  663. dst++;
  664. src += src_skip;
  665. }
  666. }
  667. void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  668. {
  669. #ifdef __ARM_NEON__
  670. unsigned long unrolled = nsamples / 4;
  671. nsamples = nsamples & 3;
  672. while (unrolled--) {
  673. float32x4_t samples = vld1q_f32(src);
  674. int16x4_t converted = float_16_neon(samples);
  675. converted = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(converted)));
  676. switch(dst_skip) {
  677. case 2:
  678. vst1_s16((int16_t*)dst, converted);
  679. break;
  680. default:
  681. vst1_lane_s16((int16_t*)(dst), converted, 0);
  682. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  683. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  684. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  685. break;
  686. }
  687. dst += 4*dst_skip;
  688. src+= 4;
  689. }
  690. #endif
  691. int16_t tmp;
  692. while (nsamples--) {
  693. // float_16 (*src, tmp);
  694. if (*src <= NORMALIZED_FLOAT_MIN) {
  695. tmp = SAMPLE_16BIT_MIN;
  696. } else if (*src >= NORMALIZED_FLOAT_MAX) {
  697. tmp = SAMPLE_16BIT_MAX;
  698. } else {
  699. tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
  700. }
  701. #if __BYTE_ORDER == __LITTLE_ENDIAN
  702. dst[0]=(char)(tmp>>8);
  703. dst[1]=(char)(tmp);
  704. #elif __BYTE_ORDER == __BIG_ENDIAN
  705. dst[0]=(char)(tmp);
  706. dst[1]=(char)(tmp>>8);
  707. #endif
  708. dst += dst_skip;
  709. src++;
  710. }
  711. }
  712. void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  713. {
  714. #ifdef __ARM_NEON__
  715. unsigned long unrolled = nsamples / 4;
  716. nsamples = nsamples & 3;
  717. while (unrolled--) {
  718. float32x4_t samples = vld1q_f32(src);
  719. int16x4_t converted = float_16_neon(samples);
  720. switch(dst_skip) {
  721. case 2:
  722. vst1_s16((int16_t*)dst, converted);
  723. break;
  724. default:
  725. vst1_lane_s16((int16_t*)(dst), converted, 0);
  726. vst1_lane_s16((int16_t*)(dst+dst_skip), converted, 1);
  727. vst1_lane_s16((int16_t*)(dst+2*dst_skip), converted, 2);
  728. vst1_lane_s16((int16_t*)(dst+3*dst_skip), converted, 3);
  729. break;
  730. }
  731. dst += 4*dst_skip;
  732. src+= 4;
  733. }
  734. #endif
  735. while (nsamples--) {
  736. float_16 (*src, *((int16_t*) dst));
  737. dst += dst_skip;
  738. src++;
  739. }
  740. }
  741. void sample_move_dither_rect_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  742. {
  743. jack_default_audio_sample_t val;
  744. int16_t tmp;
  745. while (nsamples--) {
  746. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float) UINT_MAX - 0.5f;
  747. float_16_scaled (val, tmp);
  748. #if __BYTE_ORDER == __LITTLE_ENDIAN
  749. dst[0]=(char)(tmp>>8);
  750. dst[1]=(char)(tmp);
  751. #elif __BYTE_ORDER == __BIG_ENDIAN
  752. dst[0]=(char)(tmp);
  753. dst[1]=(char)(tmp>>8);
  754. #endif
  755. dst += dst_skip;
  756. src++;
  757. }
  758. }
  759. void sample_move_dither_rect_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  760. {
  761. jack_default_audio_sample_t val;
  762. while (nsamples--) {
  763. val = (*src * SAMPLE_16BIT_SCALING) + fast_rand() / (float)UINT_MAX - 0.5f;
  764. float_16_scaled (val, *((int16_t*) dst));
  765. dst += dst_skip;
  766. src++;
  767. }
  768. }
  769. void sample_move_dither_tri_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  770. {
  771. jack_default_audio_sample_t val;
  772. int16_t tmp;
  773. while (nsamples--) {
  774. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  775. float_16_scaled (val, tmp);
  776. #if __BYTE_ORDER == __LITTLE_ENDIAN
  777. dst[0]=(char)(tmp>>8);
  778. dst[1]=(char)(tmp);
  779. #elif __BYTE_ORDER == __BIG_ENDIAN
  780. dst[0]=(char)(tmp);
  781. dst[1]=(char)(tmp>>8);
  782. #endif
  783. dst += dst_skip;
  784. src++;
  785. }
  786. }
  787. void sample_move_dither_tri_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  788. {
  789. jack_default_audio_sample_t val;
  790. while (nsamples--) {
  791. val = (*src * SAMPLE_16BIT_SCALING) + ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  792. float_16_scaled (val, *((int16_t*) dst));
  793. dst += dst_skip;
  794. src++;
  795. }
  796. }
  797. void sample_move_dither_shaped_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  798. {
  799. jack_default_audio_sample_t x;
  800. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  801. jack_default_audio_sample_t xp; /* x' */
  802. float r;
  803. float rm1 = state->rm1;
  804. unsigned int idx = state->idx;
  805. int16_t tmp;
  806. while (nsamples--) {
  807. x = *src * SAMPLE_16BIT_SCALING;
  808. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  809. /* Filter the error with Lipshitz's minimally audible FIR:
  810. [2.033 -2.165 1.959 -1.590 0.6149] */
  811. xe = x
  812. - state->e[idx] * 2.033f
  813. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  814. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  815. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  816. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  817. xp = xe + r - rm1;
  818. rm1 = r;
  819. float_16_scaled (xp, tmp);
  820. /* Intrinsic z^-1 delay */
  821. idx = (idx + 1) & DITHER_BUF_MASK;
  822. state->e[idx] = xp - xe;
  823. #if __BYTE_ORDER == __LITTLE_ENDIAN
  824. dst[0]=(char)(tmp>>8);
  825. dst[1]=(char)(tmp);
  826. #elif __BYTE_ORDER == __BIG_ENDIAN
  827. dst[0]=(char)(tmp);
  828. dst[1]=(char)(tmp>>8);
  829. #endif
  830. dst += dst_skip;
  831. src++;
  832. }
  833. state->rm1 = rm1;
  834. state->idx = idx;
  835. }
  836. void sample_move_dither_shaped_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
  837. {
  838. jack_default_audio_sample_t x;
  839. jack_default_audio_sample_t xe; /* the innput sample - filtered error */
  840. jack_default_audio_sample_t xp; /* x' */
  841. float r;
  842. float rm1 = state->rm1;
  843. unsigned int idx = state->idx;
  844. while (nsamples--) {
  845. x = *src * SAMPLE_16BIT_SCALING;
  846. r = ((float)fast_rand() + (float)fast_rand()) / (float)UINT_MAX - 1.0f;
  847. /* Filter the error with Lipshitz's minimally audible FIR:
  848. [2.033 -2.165 1.959 -1.590 0.6149] */
  849. xe = x
  850. - state->e[idx] * 2.033f
  851. + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
  852. - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
  853. + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
  854. - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
  855. xp = xe + r - rm1;
  856. rm1 = r;
  857. float_16_scaled (xp, *((int16_t*) dst));
  858. /* Intrinsic z^-1 delay */
  859. idx = (idx + 1) & DITHER_BUF_MASK;
  860. state->e[idx] = *((int16_t*) dst) - xe;
  861. dst += dst_skip;
  862. src++;
  863. }
  864. state->rm1 = rm1;
  865. state->idx = idx;
  866. }
  867. void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  868. {
  869. short z;
  870. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  871. #ifdef __ARM_NEON__
  872. const float32x4_t vscaling = vdupq_n_f32(scaling);
  873. unsigned long unrolled = nsamples / 4;
  874. while (unrolled--) {
  875. int16x4_t source16x4;
  876. switch(src_skip) {
  877. case 2:
  878. source16x4 = vld1_s16((int16_t*)src);
  879. break;
  880. case 4:
  881. source16x4 = vld2_s16((int16_t*)src).val[0];
  882. break;
  883. default:
  884. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  885. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  886. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  887. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  888. break;
  889. }
  890. source16x4 = vreinterpret_s16_u8(vrev16_u8(vreinterpret_u8_s16(source16x4)));
  891. int32x4_t source32x4 = vmovl_s16(source16x4);
  892. src += 4 * src_skip;
  893. float32x4_t converted = vcvtq_f32_s32(source32x4);
  894. float32x4_t scaled = vmulq_f32(converted, vscaling);
  895. vst1q_f32(dst, scaled);
  896. dst += 4;
  897. }
  898. nsamples = nsamples & 3;
  899. #endif
  900. /* ALERT: signed sign-extension portability !!! */
  901. while (nsamples--) {
  902. #if __BYTE_ORDER == __LITTLE_ENDIAN
  903. z = (unsigned char)(src[0]);
  904. z <<= 8;
  905. z |= (unsigned char)(src[1]);
  906. #elif __BYTE_ORDER == __BIG_ENDIAN
  907. z = (unsigned char)(src[1]);
  908. z <<= 8;
  909. z |= (unsigned char)(src[0]);
  910. #endif
  911. *dst = z * scaling;
  912. dst++;
  913. src += src_skip;
  914. }
  915. }
  916. void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
  917. {
  918. /* ALERT: signed sign-extension portability !!! */
  919. const jack_default_audio_sample_t scaling = 1.0/SAMPLE_16BIT_SCALING;
  920. #ifdef __ARM_NEON__
  921. const float32x4_t vscaling = vdupq_n_f32(scaling);
  922. unsigned long unrolled = nsamples / 4;
  923. while (unrolled--) {
  924. int16x4_t source16x4;
  925. switch(src_skip) {
  926. case 2:
  927. source16x4 = vld1_s16((int16_t*)src);
  928. break;
  929. case 4:
  930. source16x4 = vld2_s16((int16_t*)src).val[0];
  931. break;
  932. default:
  933. source16x4 = vld1_lane_s16((int16_t*)src, source16x4, 0);
  934. source16x4 = vld1_lane_s16((int16_t*)(src+src_skip), source16x4, 1);
  935. source16x4 = vld1_lane_s16((int16_t*)(src+2*src_skip), source16x4, 2);
  936. source16x4 = vld1_lane_s16((int16_t*)(src+3*src_skip), source16x4, 3);
  937. break;
  938. }
  939. int32x4_t source32x4 = vmovl_s16(source16x4);
  940. src += 4 * src_skip;
  941. float32x4_t converted = vcvtq_f32_s32(source32x4);
  942. float32x4_t scaled = vmulq_f32(converted, vscaling);
  943. vst1q_f32(dst, scaled);
  944. dst += 4;
  945. }
  946. nsamples = nsamples & 3;
  947. #endif
  948. while (nsamples--) {
  949. *dst = (*((short *) src)) * scaling;
  950. dst++;
  951. src += src_skip;
  952. }
  953. }
  954. void memset_interleave (char *dst, char val, unsigned long bytes,
  955. unsigned long unit_bytes,
  956. unsigned long skip_bytes)
  957. {
  958. switch (unit_bytes) {
  959. case 1:
  960. while (bytes--) {
  961. *dst = val;
  962. dst += skip_bytes;
  963. }
  964. break;
  965. case 2:
  966. while (bytes) {
  967. *((short *) dst) = (short) val;
  968. dst += skip_bytes;
  969. bytes -= 2;
  970. }
  971. break;
  972. case 4:
  973. while (bytes) {
  974. *((int *) dst) = (int) val;
  975. dst += skip_bytes;
  976. bytes -= 4;
  977. }
  978. break;
  979. default:
  980. while (bytes) {
  981. memset(dst, val, unit_bytes);
  982. dst += skip_bytes;
  983. bytes -= unit_bytes;
  984. }
  985. break;
  986. }
  987. }
  988. /* COPY FUNCTIONS: used to move data from an input channel to an
  989. output channel. Note that we assume that the skip distance
  990. is the same for both channels. This is completely fine
  991. unless the input and output were on different audio interfaces that
  992. were interleaved differently. We don't try to handle that.
  993. */
  994. void
  995. memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
  996. {
  997. memcpy (dst, src, src_bytes);
  998. }
  999. void
  1000. memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
  1001. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1002. {
  1003. while (src_bytes) {
  1004. *((short *) dst) = *((short *) src);
  1005. dst += dst_skip_bytes;
  1006. src += src_skip_bytes;
  1007. src_bytes -= 2;
  1008. }
  1009. }
  1010. void
  1011. memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
  1012. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1013. {
  1014. while (src_bytes) {
  1015. memcpy(dst, src, 3);
  1016. dst += dst_skip_bytes;
  1017. src += src_skip_bytes;
  1018. src_bytes -= 3;
  1019. }
  1020. }
  1021. void
  1022. memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
  1023. unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
  1024. {
  1025. while (src_bytes) {
  1026. *((int *) dst) = *((int *) src);
  1027. dst += dst_skip_bytes;
  1028. src += src_skip_bytes;
  1029. src_bytes -= 4;
  1030. }
  1031. }