1/***
2  This file is part of PulseAudio.
3
4  Copyright 2004-2006 Lennart Poettering
5  Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk>
6
7  PulseAudio is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 2.1 of the License,
10  or (at your option) any later version.
11
12  PulseAudio is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16
17  You should have received a copy of the GNU Lesser General Public License
18  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
19***/
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdint.h>
26
27#ifdef HAVE_CPUID_H
28#include <cpuid.h>
29#endif
30
31#include <pulsecore/log.h>
32
33#include "cpu-x86.h"
34
35void pa_cpu_get_x86_flags(pa_cpu_x86_flag_t *flags) {
36#if (defined(__i386__) || defined(__amd64__)) && defined(HAVE_CPUID_H)
37    uint32_t eax, ebx, ecx, edx;
38    uint32_t level;
39
40    *flags = 0;
41
42    /* get standard level */
43    if (__get_cpuid(0x00000000, &level, &ebx, &ecx, &edx) == 0)
44        goto finish;
45
46    if (level >= 1) {
47        if (__get_cpuid(0x00000001, &eax, &ebx, &ecx, &edx) == 0)
48            goto finish;
49
50        if (edx & (1<<15))
51          *flags |= PA_CPU_X86_CMOV;
52
53        if (edx & (1<<23))
54          *flags |= PA_CPU_X86_MMX;
55
56        if (edx & (1<<25))
57          *flags |= PA_CPU_X86_SSE;
58
59        if (edx & (1<<26))
60          *flags |= PA_CPU_X86_SSE2;
61
62        if (ecx & (1<<0))
63          *flags |= PA_CPU_X86_SSE3;
64
65        if (ecx & (1<<9))
66          *flags |= PA_CPU_X86_SSSE3;
67
68        if (ecx & (1<<19))
69          *flags |= PA_CPU_X86_SSE4_1;
70
71        if (ecx & (1<<20))
72          *flags |= PA_CPU_X86_SSE4_2;
73    }
74
75    /* get extended level */
76    if (__get_cpuid(0x80000000, &level, &ebx, &ecx, &edx) == 0)
77        goto finish;
78
79    if (level >= 0x80000001) {
80        if (__get_cpuid(0x80000001, &eax, &ebx, &ecx, &edx) == 0)
81            goto finish;
82
83        if (edx & (1<<22))
84          *flags |= PA_CPU_X86_MMXEXT;
85
86        if (edx & (1<<23))
87          *flags |= PA_CPU_X86_MMX;
88
89        if (edx & (1<<30))
90          *flags |= PA_CPU_X86_3DNOWEXT;
91
92        if (edx & (1<<31))
93          *flags |= PA_CPU_X86_3DNOW;
94    }
95
96finish:
97    pa_log_info("CPU flags: %s%s%s%s%s%s%s%s%s%s%s",
98    (*flags & PA_CPU_X86_CMOV) ? "CMOV " : "",
99    (*flags & PA_CPU_X86_MMX) ? "MMX " : "",
100    (*flags & PA_CPU_X86_SSE) ? "SSE " : "",
101    (*flags & PA_CPU_X86_SSE2) ? "SSE2 " : "",
102    (*flags & PA_CPU_X86_SSE3) ? "SSE3 " : "",
103    (*flags & PA_CPU_X86_SSSE3) ? "SSSE3 " : "",
104    (*flags & PA_CPU_X86_SSE4_1) ? "SSE4_1 " : "",
105    (*flags & PA_CPU_X86_SSE4_2) ? "SSE4_2 " : "",
106    (*flags & PA_CPU_X86_MMXEXT) ? "MMXEXT " : "",
107    (*flags & PA_CPU_X86_3DNOW) ? "3DNOW " : "",
108    (*flags & PA_CPU_X86_3DNOWEXT) ? "3DNOWEXT " : "");
109#endif /* (defined(__i386__) || defined(__amd64__)) && defined(HAVE_CPUID_H) */
110}
111
112bool pa_cpu_init_x86(pa_cpu_x86_flag_t *flags) {
113#if defined (__i386__) || defined (__amd64__)
114    pa_cpu_get_x86_flags(flags);
115
116    /* activate various optimisations */
117#ifdef HAVE_MMX
118    if (*flags & PA_CPU_X86_MMX) {
119        pa_volume_func_init_mmx(*flags);
120        pa_remap_func_init_mmx(*flags);
121    }
122#endif
123
124#ifdef HAVE_SSE
125    if (*flags & (PA_CPU_X86_SSE | PA_CPU_X86_SSE2)) {
126        pa_volume_func_init_sse(*flags);
127        pa_remap_func_init_sse(*flags);
128        pa_convert_func_init_sse(*flags);
129    }
130#endif
131
132    return true;
133#else /* defined (__i386__) || defined (__amd64__) */
134    return false;
135#endif /* defined (__i386__) || defined (__amd64__) */
136}
137