1/***
2  This file is part of PulseAudio.
3
4  Copyright 2012 Peter Meerwald <p.meerwald@bct-electronic.com>
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15***/
16
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include <pulsecore/macro.h>
22#include <pulsecore/endianmacros.h>
23
24#include "cpu-arm.h"
25#include "sconv.h"
26
27#include <math.h>
28#include <arm_neon.h>
29
30static void pa_sconv_s16le_from_f32ne_neon(unsigned n, const float *src, int16_t *dst) {
31    unsigned i = n & 3;
32
33    __asm__ __volatile__ (
34        "movs       %[n], %[n], lsr #2      \n\t"
35        "beq        2f                      \n\t"
36
37        "1:                                 \n\t"
38        "vld1.32    {q0}, [%[src]]!         \n\t"
39        "vcvt.s32.f32 q0, q0, #31           \n\t" /* s32<-f32 as 16:16 fixed-point, with implicit multiplication by 32768 */
40        "vqrshrn.s32 d0, q0, #16            \n\t" /* shift, round, narrow */
41        "subs       %[n], %[n], #1          \n\t"
42        "vst1.16    {d0}, [%[dst]]!         \n\t"
43        "bgt        1b                      \n\t"
44
45        "2:                                 \n\t"
46
47        : [dst] "+r" (dst), [src] "+r" (src), [n] "+r" (n) /* output operands (or input operands that get modified) */
48        : /* input operands */
49        : "memory", "cc", "q0" /* clobber list */
50    );
51
52    /* leftovers */
53    while (i--) {
54        *dst++ = (int16_t) PA_CLAMP_UNLIKELY(lrintf(*src * (1 << 15)), -0x8000, 0x7FFF);
55        src++;
56    }
57}
58
59static void pa_sconv_s16le_to_f32ne_neon(unsigned n, const int16_t *src, float *dst) {
60    unsigned i = n & 3;
61    const float invscale = 1.0f / (1 << 15);
62
63    __asm__ __volatile__ (
64        "movs       %[n], %[n], lsr #2      \n\t"
65        "beq        2f                      \n\t"
66
67        "1:                                 \n\t"
68        "vld1.16    {d0}, [%[src]]!         \n\t"
69        "vmovl.s16  q0, d0                  \n\t" /* widen */
70        "vcvt.f32.s32 q0, q0, #15           \n\t" /* f32<-s32 and divide by (1<<15) */
71        "subs       %[n], %[n], #1          \n\t"
72        "vst1.32    {q0}, [%[dst]]!         \n\t"
73        "bgt        1b                      \n\t"
74
75        "2:                                 \n\t"
76
77        : [dst] "+r" (dst), [src] "+r" (src), [n] "+r" (n) /* output operands (or input operands that get modified) */
78        : /* input operands */
79        : "memory", "cc", "q0" /* clobber list */
80    );
81
82    /* leftovers */
83    while (i--) {
84        *dst++ = *src++ * invscale;
85    }
86}
87
88void pa_convert_func_init_neon(pa_cpu_arm_flag_t flags) {
89    pa_log_info("Initialising ARM NEON optimized conversions.");
90    pa_set_convert_from_float32ne_function(PA_SAMPLE_S16LE, (pa_convert_func_t) pa_sconv_s16le_from_f32ne_neon);
91    pa_set_convert_to_float32ne_function(PA_SAMPLE_S16LE, (pa_convert_func_t) pa_sconv_s16le_to_f32ne_neon);
92#ifndef WORDS_BIGENDIAN
93    pa_set_convert_from_s16ne_function(PA_SAMPLE_FLOAT32LE, (pa_convert_func_t) pa_sconv_s16le_to_f32ne_neon);
94    pa_set_convert_to_s16ne_function(PA_SAMPLE_FLOAT32LE, (pa_convert_func_t) pa_sconv_s16le_from_f32ne_neon);
95#endif
96}
97