Debian Codegraph @

include/cpu.h

raw
1 /*
2 * Copyright 2016 Knowledge Economy Developments Ltd
3 *
4 * Henry Gomersall
5 * [email protected]
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * * Neither the name of the copyright holder nor the names of its contributors
20 * may be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /* Small utilities for inspecting the CPU */
37
38 #ifndef CPU_H
39 #define CPU_H
40
41 #if __STDC_VERSION__ >= 199901L
42 /* "inline" is a keyword */
43 #else
44 # define inline
45 #endif
46
47 #if defined(__amd64__) || defined (_M_X64) || defined(__i386__) || defined(_M_IX86) || defined(_X86_)
48
49 #define AVX_WORD 2
50 #define AVX_BIT 28
51 #define SSE_WORD 3
52 #define SSE_BIT 25
53
54 #ifdef _MSC_VER
55 /* Visual Studio Code */
56 #include <intrin.h>
57 #define cpuid(func, cpuinfo)\
58 __cpuid(cpuinfo, func);
59
60 #else
61 /* generic x86 Assembly code (based on wikipedia example)
62 * Firstly it's necessary to move ebx into an interim
63 * register to protect it (cpuid clobbers eax, ebx ecx and edx)
64 * */
65 #define cpuid(func, cpuinfo)\
66 cpuinfo[0] = func; /* Load the first entry with the func id */\
67 __asm__ __volatile__ \
68 ("mov %%ebx, %%edi;" /* 32bit PIC: don't clobber ebx */ \
69 "cpuid;" \
70 "mov %%ebx, %%esi;" \
71 "mov %%edi, %%ebx;" \
72 :"+a" (cpuinfo[0]), "=S" (cpuinfo[1]), /* eax rw, esi read */ \
73 "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) /* ecx read, edx read */\
74 : :"edi")
75
76 #endif
77
78 /* Returns the byte alignment for optimum simd operations */
79 static inline int simd_alignment(void){
80 int cpuinfo[4];
81
82 /* This gets the cpuinfo (set by 1)*/
83 cpuid(1, cpuinfo);
84
85 if (cpuinfo[AVX_WORD] & (1<<AVX_BIT)) /* AVX */
86 return 32;
87 else if (cpuinfo[SSE_WORD] & (1<<SSE_BIT)) /* SSE */
88 return 16;
89 else /* No SIMD */
90 return 4;
91 }
92
93 #else
94
95 static inline int simd_alignment(void){
96 return 4;
97 }
98 #endif
99
100 #endif /* Header guard */