blob: 5d243bb8d51ee86327e5489a75578175516eb045 [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
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000017#include "common_audio/signal_processing/include/real_fft.h"
kma@webrtc.org0221b782012-09-08 00:09:26 +000018#include "common_audio/signal_processing/include/signal_processing_library.h"
19#include "system_wrappers/interface/cpu_features_wrapper.h"
20
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
wu@webrtc.org7aa11892012-10-10 20:59:35 +000034#if defined(WEBRTC_DETECT_ARM_NEON) || !defined(WEBRTC_ARCH_ARM_NEON)
kma@webrtc.org0221b782012-09-08 00:09:26 +000035/* Initialize function pointers to the generic C version. */
36static void InitPointersToC() {
37 WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16C;
38 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C;
39 WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16C;
40 WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32C;
41 WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16C;
42 WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32C;
43 WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC;
44 WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastC;
45 WebRtcSpl_ScaleAndAddVectorsWithRound =
46 WebRtcSpl_ScaleAndAddVectorsWithRoundC;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000047 WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTC;
48 WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTC;
kma@webrtc.org0221b782012-09-08 00:09:26 +000049}
wu@webrtc.org7aa11892012-10-10 20:59:35 +000050#endif
kma@webrtc.org0221b782012-09-08 00:09:26 +000051
52#if defined(WEBRTC_DETECT_ARM_NEON) || defined(WEBRTC_ARCH_ARM_NEON)
53/* Initialize function pointers to the Neon version. */
54static void InitPointersToNeon() {
55 WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16Neon;
56 WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32Neon;
57 WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16Neon;
58 WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32Neon;
59 WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16Neon;
60 WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32Neon;
61 WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationNeon;
62 WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastNeon;
63 WebRtcSpl_ScaleAndAddVectorsWithRound =
64 WebRtcSpl_ScaleAndAddVectorsWithRoundNeon;
kma@webrtc.orgf9e6cc22012-09-21 18:51:12 +000065 WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTNeon;
66 WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTNeon;
kma@webrtc.org0221b782012-09-08 00:09:26 +000067}
68#endif
69
70static void InitFunctionPointers(void) {
71#if defined(WEBRTC_DETECT_ARM_NEON)
72 if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) {
73 InitPointersToNeon();
74 } else {
75 InitPointersToC();
76 }
77#elif defined(WEBRTC_ARCH_ARM_NEON)
78 InitPointersToNeon();
79#else
80 InitPointersToC();
81#endif /* WEBRTC_DETECT_ARM_NEON */
82}
83
kma@webrtc.org0221b782012-09-08 00:09:26 +000084#if defined(WEBRTC_POSIX)
85#include <pthread.h>
86
87static void once(void (*func)(void)) {
88 static pthread_once_t lock = PTHREAD_ONCE_INIT;
89 pthread_once(&lock, func);
90}
91
92#elif defined(_WIN32)
93#include <windows.h>
94
95static void once(void (*func)(void)) {
96 /* Didn't use InitializeCriticalSection() since there's no race-free context
97 * in which to execute it.
98 *
99 * TODO(kma): Change to different implementation (e.g.
100 * InterlockedCompareExchangePointer) to avoid issues similar to
101 * http://code.google.com/p/webm/issues/detail?id=467.
102 */
andrew@webrtc.org63e09642013-01-29 06:45:22 +0000103 static CRITICAL_SECTION lock = {(void *)((size_t)-1), -1, 0, 0, 0, 0};
kma@webrtc.org0221b782012-09-08 00:09:26 +0000104 static int done = 0;
105
106 EnterCriticalSection(&lock);
107 if (!done) {
108 func();
109 done = 1;
110 }
111 LeaveCriticalSection(&lock);
112}
113
114/* There's no fallback version as an #else block here to ensure thread safety.
115 * In case of neither pthread for WEBRTC_POSIX nor _WIN32 is present, build
116 * system should pick it up.
117 */
118#endif /* WEBRTC_POSIX */
119
120void WebRtcSpl_Init() {
121 once(InitFunctionPointers);
122}