Skip to content

Commit 51d78af

Browse files
author
Benjamin Moody
committed
Enable CPU optimizations for x86 and arm64.
Some libFLAC functions can be optimized by using architecture-specific functions in place of the generic (standard C) implementations. Enable these optimizations if supported by the compiler.
1 parent 2651f63 commit 51d78af

6 files changed

Lines changed: 57 additions & 1 deletion

File tree

setup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ def try_compile(src_name, extra_compile_args=[], trap_errors=True):
110110

111111
if try_compile('conftest_fseeko.c'):
112112
define_macros += [('PLIBFLAC_HAVE_FSEEKO', '1')]
113+
if try_compile('conftest_cpuid_h.c'):
114+
define_macros += [('PLIBFLAC_HAVE_CPUID_H', '1')]
115+
116+
machine = platform.machine().lower()
117+
if re.match('i[3-6]86', machine):
118+
define_macros += [('FLAC__CPU_IA32', '1'),
119+
('FLAC__USE_AVX', '1'),
120+
('FLAC__ALIGN_MALLOC_DATA', '1')]
121+
if try_compile('conftest_x86intrin.c'):
122+
define_macros += [('FLAC__HAS_X86INTRIN', '1')]
123+
if re.match('x86_64|amd64', machine):
124+
define_macros += [('FLAC__CPU_X86_64', '1'),
125+
('FLAC__HAS_X86INTRIN', '1'),
126+
('FLAC__USE_AVX', '1'),
127+
('FLAC__ALIGN_MALLOC_DATA', '1')]
128+
if try_compile('conftest_x86intrin.c'):
129+
define_macros += [('FLAC__HAS_X86INTRIN', '1')]
130+
if re.match('arm64|aarch64', machine):
131+
define_macros += [('FLAC__CPU_ARM64', '1')]
132+
if try_compile('conftest_a64neonintrin.c'):
133+
define_macros += [('FLAC__HAS_NEONINTRIN', '1'),
134+
('FLAC__HAS_A64NEONINTRIN', '1')]
135+
if try_compile('conftest_neonintrin.c'):
136+
define_macros += [('FLAC__HAS_NEONINTRIN', '1')]
113137

114138
return {
115139
'sources': sources,

src/conf/conftest_a64neonintrin.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Test for FLAC__HAS_A64NEONINTRIN */
2+
#include <arm_neon.h>
3+
int main()
4+
{
5+
float64x2_t tmp;
6+
tmp = vdupq_n_f64(0.0f);
7+
return 0;
8+
}

src/conf/conftest_cpuid_h.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Test for HAVE_CPUID_H */
2+
#include <cpuid.h>
3+
int main()
4+
{
5+
return 0;
6+
}

src/conf/conftest_neonintrin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Test for FLAC__HAS_NEONINTRIN */
2+
#include <arm_neon.h>
3+
int main()
4+
{
5+
return 0;
6+
}

src/conf/conftest_x86intrin.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Test for FLAC__HAS_X86INTRIN */
2+
#ifdef _MSC_VER
3+
# include <intrin.h>
4+
#else
5+
# include <x86intrin.h>
6+
#endif
7+
int main()
8+
{
9+
return 0;
10+
}

src/flac/config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
//#cmakedefine HAVE_CLOCK_GETTIME
6666

6767
/* Define to 1 if you have the <cpuid.h> header file. */
68-
//#cmakedefine HAVE_CPUID_H
68+
#ifdef PLIBFLAC_HAVE_CPUID_H
69+
# define HAVE_CPUID_H 1
70+
#endif
6971

7072
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
7173
#ifdef PLIBFLAC_HAVE_FSEEKO

0 commit comments

Comments
 (0)