1 /*
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 
17 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
18 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
19 
20 /* Declare function pointers. */
21 MaxAbsValueW16 WebRtcSpl_MaxAbsValueW16;
22 MaxAbsValueW32 WebRtcSpl_MaxAbsValueW32;
23 MaxValueW16 WebRtcSpl_MaxValueW16;
24 MaxValueW32 WebRtcSpl_MaxValueW32;
25 MinValueW16 WebRtcSpl_MinValueW16;
26 MinValueW32 WebRtcSpl_MinValueW32;
27 CrossCorrelation WebRtcSpl_CrossCorrelation;
28 DownsampleFast WebRtcSpl_DownsampleFast;
29 ScaleAndAddVectorsWithRound WebRtcSpl_ScaleAndAddVectorsWithRound;
30 
31 #if (defined(WEBRTC_DETECT_ARM_NEON) || !defined(WEBRTC_ARCH_ARM_NEON)) && \
32      !defined(MIPS32_LE)
33 /* Initialize function pointers to the generic C version. */
InitPointersToC()34 static void InitPointersToC() {
35   WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16C;
36   WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C;
37   WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16C;
38   WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32C;
39   WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16C;
40   WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32C;
41   WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC;
42   WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastC;
43   WebRtcSpl_ScaleAndAddVectorsWithRound =
44       WebRtcSpl_ScaleAndAddVectorsWithRoundC;
45 }
46 #endif
47 
48 #if defined(WEBRTC_DETECT_ARM_NEON) || defined(WEBRTC_ARCH_ARM_NEON)
49 /* Initialize function pointers to the Neon version. */
InitPointersToNeon()50 static void InitPointersToNeon() {
51   WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16Neon;
52   WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32Neon;
53   WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16Neon;
54   WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32Neon;
55   WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16Neon;
56   WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32Neon;
57   WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationNeon;
58   WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastNeon;
59   /* TODO(henrik.lundin): re-enable NEON when the crash from bug 3243 is
60      understood. */
61   WebRtcSpl_ScaleAndAddVectorsWithRound =
62       WebRtcSpl_ScaleAndAddVectorsWithRoundC;
63 }
64 #endif
65 
66 #if defined(MIPS32_LE)
67 /* Initialize function pointers to the MIPS version. */
InitPointersToMIPS()68 static void InitPointersToMIPS() {
69   WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16_mips;
70   WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16_mips;
71   WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32_mips;
72   WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16_mips;
73   WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32_mips;
74   WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelation_mips;
75   WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFast_mips;
76 #if defined(MIPS_DSP_R1_LE)
77   WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32_mips;
78   WebRtcSpl_ScaleAndAddVectorsWithRound =
79       WebRtcSpl_ScaleAndAddVectorsWithRound_mips;
80 #else
81   WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C;
82   WebRtcSpl_ScaleAndAddVectorsWithRound =
83       WebRtcSpl_ScaleAndAddVectorsWithRoundC;
84 #endif
85 }
86 #endif
87 
InitFunctionPointers(void)88 static void InitFunctionPointers(void) {
89 #if defined(WEBRTC_DETECT_ARM_NEON)
90   if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) {
91     InitPointersToNeon();
92   } else {
93     InitPointersToC();
94   }
95 #elif defined(WEBRTC_ARCH_ARM_NEON)
96   InitPointersToNeon();
97 #elif defined(MIPS32_LE)
98   InitPointersToMIPS();
99 #else
100   InitPointersToC();
101 #endif  /* WEBRTC_DETECT_ARM_NEON */
102 }
103 
104 #if defined(WEBRTC_POSIX)
105 #include <pthread.h>
106 
once(void (* func)(void))107 static void once(void (*func)(void)) {
108   static pthread_once_t lock = PTHREAD_ONCE_INIT;
109   pthread_once(&lock, func);
110 }
111 
112 #elif defined(_WIN32)
113 #include <windows.h>
114 
once(void (* func)(void))115 static void once(void (*func)(void)) {
116   /* Didn't use InitializeCriticalSection() since there's no race-free context
117    * in which to execute it.
118    *
119    * TODO(kma): Change to different implementation (e.g.
120    * InterlockedCompareExchangePointer) to avoid issues similar to
121    * http://code.google.com/p/webm/issues/detail?id=467.
122    */
123   static CRITICAL_SECTION lock = {(void *)((size_t)-1), -1, 0, 0, 0, 0};
124   static int done = 0;
125 
126   EnterCriticalSection(&lock);
127   if (!done) {
128     func();
129     done = 1;
130   }
131   LeaveCriticalSection(&lock);
132 }
133 
134 /* There's no fallback version as an #else block here to ensure thread safety.
135  * In case of neither pthread for WEBRTC_POSIX nor _WIN32 is present, build
136  * system should pick it up.
137  */
138 #endif  /* WEBRTC_POSIX */
139 
WebRtcSpl_Init()140 void WebRtcSpl_Init() {
141   once(InitFunctionPointers);
142 }
143