blob: fdb77ef1028d9ed5f62476c8f0f515c1318da196 [file] [log] [blame]
kma@webrtc.org0221b782012-09-08 00:09:26 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/* The global function contained in this file initializes SPL function
12 * pointers, currently only for ARM platforms.
13 *
14 * Some code came from common/rtcd.c in the WebM project.
15 */
16
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000017#include "webrtc/common_audio/signal_processing/include/real_fft.h"
18#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
19#include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
kma@webrtc.org0221b782012-09-08 00:09:26 +000020
21/* Declare function pointers. */
22MaxAbsValueW16 WebRtcSpl_MaxAbsValueW16;
23MaxAbsValueW32 WebRtcSpl_MaxAbsValueW32;
24MaxValueW16 WebRtcSpl_MaxValueW16;
25MaxValueW32 WebRtcSpl_MaxValueW32;
26MinValueW16 WebRtcSpl_MinValueW16;
27MinValueW32 WebRtcSpl_MinValueW32;
28CrossCorrelation WebRtcSpl_CrossCorrelation;
29DownsampleFast WebRtcSpl_DownsampleFast;
30ScaleAndAddVectorsWithRound WebRtcSpl_ScaleAndAddVectorsWithRound;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000031RealForwardFFT WebRtcSpl_RealForwardFFT;
32RealInverseFFT WebRtcSpl_RealInverseFFT;
kma@webrtc.org0221b782012-09-08 00:09:26 +000033
andrew@webrtc.org5140e242013-02-21 20:12:21 +000034#if (defined(WEBRTC_DETECT_ARM_NEON) || !defined(WEBRTC_ARCH_ARM_NEON)) && \
35 !defined(MIPS32_LE)
kma@webrtc.org0221b782012-09-08 00:09:26 +000036/* Initialize function pointers to the generic C version. */
37static void InitPointersToC() {
38 WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16C;
39 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C;
40 WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16C;
41 WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32C;
42 WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16C;
43 WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32C;
44 WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC;
45 WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastC;
46 WebRtcSpl_ScaleAndAddVectorsWithRound =
47 WebRtcSpl_ScaleAndAddVectorsWithRoundC;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000048 WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTC;
49 WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTC;
kma@webrtc.org0221b782012-09-08 00:09:26 +000050}
wu@webrtc.org7aa11892012-10-10 20:59:35 +000051#endif
kma@webrtc.org0221b782012-09-08 00:09:26 +000052
53#if defined(WEBRTC_DETECT_ARM_NEON) || defined(WEBRTC_ARCH_ARM_NEON)
54/* Initialize function pointers to the Neon version. */
55static void InitPointersToNeon() {
56 WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16Neon;
57 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32Neon;
58 WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16Neon;
59 WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32Neon;
60 WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16Neon;
61 WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32Neon;
62 WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationNeon;
63 WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastNeon;
64 WebRtcSpl_ScaleAndAddVectorsWithRound =
65 WebRtcSpl_ScaleAndAddVectorsWithRoundNeon;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000066 WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTNeon;
67 WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTNeon;
kma@webrtc.org0221b782012-09-08 00:09:26 +000068}
69#endif
70
andrew@webrtc.org5140e242013-02-21 20:12:21 +000071#if defined(MIPS32_LE)
72/* Initialize function pointers to the MIPS version. */
73static void InitPointersToMIPS() {
74 WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16_mips;
75 WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16_mips;
76 WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32_mips;
77 WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16_mips;
78 WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32_mips;
79 WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC;
80 WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastC;
81 WebRtcSpl_ScaleAndAddVectorsWithRound =
82 WebRtcSpl_ScaleAndAddVectorsWithRoundC;
83 WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTC;
84 WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTC;
85#if defined(MIPS_DSP_R1_LE)
86 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32_mips;
87#else
88 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C;
89#endif
90}
91#endif
92
kma@webrtc.org0221b782012-09-08 00:09:26 +000093static void InitFunctionPointers(void) {
94#if defined(WEBRTC_DETECT_ARM_NEON)
95 if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) {
96 InitPointersToNeon();
97 } else {
98 InitPointersToC();
99 }
100#elif defined(WEBRTC_ARCH_ARM_NEON)
101 InitPointersToNeon();
andrew@webrtc.org5140e242013-02-21 20:12:21 +0000102#elif defined(MIPS32_LE)
103 InitPointersToMIPS();
kma@webrtc.org0221b782012-09-08 00:09:26 +0000104#else
105 InitPointersToC();
106#endif /* WEBRTC_DETECT_ARM_NEON */
107}
108
kma@webrtc.org0221b782012-09-08 00:09:26 +0000109#if defined(WEBRTC_POSIX)
110#include <pthread.h>
111
112static void once(void (*func)(void)) {
113 static pthread_once_t lock = PTHREAD_ONCE_INIT;
114 pthread_once(&lock, func);
115}
116
117#elif defined(_WIN32)
118#include <windows.h>
119
120static void once(void (*func)(void)) {
121 /* Didn't use InitializeCriticalSection() since there's no race-free context
122 * in which to execute it.
123 *
124 * TODO(kma): Change to different implementation (e.g.
125 * InterlockedCompareExchangePointer) to avoid issues similar to
126 * http://code.google.com/p/webm/issues/detail?id=467.
127 */
andrew@webrtc.org63e09642013-01-29 06:45:22 +0000128 static CRITICAL_SECTION lock = {(void *)((size_t)-1), -1, 0, 0, 0, 0};
kma@webrtc.org0221b782012-09-08 00:09:26 +0000129 static int done = 0;
130
131 EnterCriticalSection(&lock);
132 if (!done) {
133 func();
134 done = 1;
135 }
136 LeaveCriticalSection(&lock);
137}
138
139/* There's no fallback version as an #else block here to ensure thread safety.
140 * In case of neither pthread for WEBRTC_POSIX nor _WIN32 is present, build
141 * system should pick it up.
142 */
143#endif /* WEBRTC_POSIX */
144
145void WebRtcSpl_Init() {
146 once(InitFunctionPointers);
147}