Add packaging files for Tizen
[profile/ivi/libvpx.git] / vpx_ports / arm_cpudetect.c
1 /*
2  *  Copyright (c) 2010 The WebM 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 #include <stdlib.h>
12 #include <string.h>
13 #include "arm.h"
14
15 static int arm_cpu_env_flags(int *flags)
16 {
17     char *env;
18     env = getenv("VPX_SIMD_CAPS");
19     if (env && *env)
20     {
21         *flags = (int)strtol(env, NULL, 0);
22         return 0;
23     }
24     *flags = 0;
25     return -1;
26 }
27
28 static int arm_cpu_env_mask(void)
29 {
30     char *env;
31     env = getenv("VPX_SIMD_CAPS_MASK");
32     return env && *env ? (int)strtol(env, NULL, 0) : ~0;
33 }
34
35
36 #if defined(_MSC_VER)
37 /*For GetExceptionCode() and EXCEPTION_ILLEGAL_INSTRUCTION.*/
38 #define WIN32_LEAN_AND_MEAN
39 #define WIN32_EXTRA_LEAN
40 #include <windows.h>
41
42 int arm_cpu_caps(void)
43 {
44     int flags;
45     int mask;
46     if (!arm_cpu_env_flags(&flags))
47     {
48         return flags;
49     }
50     mask = arm_cpu_env_mask();
51     /* MSVC has no inline __asm support for ARM, but it does let you __emit
52      *  instructions via their assembled hex code.
53      * All of these instructions should be essentially nops.
54      */
55 #if defined(HAVE_EDSP)
56     if (mask & HAS_EDSP)
57     {
58         __try
59         {
60             /*PLD [r13]*/
61             __emit(0xF5DDF000);
62             flags |= HAS_EDSP;
63         }
64         __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION)
65         {
66             /*Ignore exception.*/
67         }
68     }
69 #if defined(HAVE_MEDIA)
70     if (mask & HAS_MEDIA)
71         __try
72         {
73             /*SHADD8 r3,r3,r3*/
74             __emit(0xE6333F93);
75             flags |= HAS_MEDIA;
76         }
77         __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION)
78         {
79             /*Ignore exception.*/
80         }
81     }
82 #if defined(HAVE_NEON)
83     if (mask & HAS_NEON)
84     {
85         __try
86         {
87             /*VORR q0,q0,q0*/
88             __emit(0xF2200150);
89             flags |= HAS_NEON;
90         }
91         __except(GetExceptionCode() == EXCEPTION_ILLEGAL_INSTRUCTION)
92         {
93             /*Ignore exception.*/
94         }
95     }
96 #endif
97 #endif
98 #endif
99     return flags & mask;
100 }
101
102 #elif defined(__linux__)
103 #if defined(__ANDROID__)
104 #include <cpu-features.h>
105
106 int arm_cpu_caps(void)
107 {
108     int flags;
109     int mask;
110     uint64_t features;
111     if (!arm_cpu_env_flags(&flags))
112     {
113         return flags;
114     }
115     mask = arm_cpu_env_mask();
116     features = android_getCpuFeatures();
117
118 #if defined(HAVE_EDSP)
119     flags |= HAS_EDSP;
120 #endif
121 #if defined(HAVE_MEDIA)
122     flags |= HAS_MEDIA;
123 #endif
124 #if defined(HAVE_NEON)
125     if (features & ANDROID_CPU_ARM_FEATURE_NEON)
126         flags |= HAS_NEON;
127 #endif
128     return flags & mask;
129 }
130 #else // !defined(__ANDROID__)
131 #include <stdio.h>
132
133 int arm_cpu_caps(void)
134 {
135     FILE *fin;
136     int flags;
137     int mask;
138     if (!arm_cpu_env_flags(&flags))
139     {
140         return flags;
141     }
142     mask = arm_cpu_env_mask();
143     /* Reading /proc/self/auxv would be easier, but that doesn't work reliably
144      *  on Android.
145      * This also means that detection will fail in Scratchbox.
146      */
147     fin = fopen("/proc/cpuinfo","r");
148     if(fin != NULL)
149     {
150         /* 512 should be enough for anybody (it's even enough for all the flags
151          * that x86 has accumulated... so far).
152          */
153         char buf[512];
154         while (fgets(buf, 511, fin) != NULL)
155         {
156 #if defined(HAVE_EDSP) || defined(HAVE_NEON)
157             if (memcmp(buf, "Features", 8) == 0)
158             {
159                 char *p;
160 #if defined(HAVE_EDSP)
161                 p=strstr(buf, " edsp");
162                 if (p != NULL && (p[5] == ' ' || p[5] == '\n'))
163                 {
164                     flags |= HAS_EDSP;
165                 }
166 #if defined(HAVE_NEON)
167                 p = strstr(buf, " neon");
168                 if (p != NULL && (p[5] == ' ' || p[5] == '\n'))
169                 {
170                     flags |= HAS_NEON;
171                 }
172 #endif
173 #endif
174             }
175 #endif
176 #if defined(HAVE_MEDIA)
177             if (memcmp(buf, "CPU architecture:",17) == 0){
178                 int version;
179                 version = atoi(buf+17);
180                 if (version >= 6)
181                 {
182                     flags |= HAS_MEDIA;
183                 }
184             }
185 #endif
186         }
187         fclose(fin);
188     }
189     return flags & mask;
190 }
191 #endif // defined(__linux__)
192 #elif !CONFIG_RUNTIME_CPU_DETECT
193
194 int arm_cpu_caps(void)
195 {
196     int flags;
197     int mask;
198     if (!arm_cpu_env_flags(&flags))
199     {
200         return flags;
201     }
202     mask = arm_cpu_env_mask();
203 #if defined(HAVE_EDSP)
204     flags |= HAS_EDSP;
205 #endif
206 #if defined(HAVE_MEDIA)
207     flags |= HAS_MEDIA;
208 #endif
209 #if defined(HAVE_NEON)
210     flags |= HAS_NEON;
211 #endif
212     return flags & mask;
213 }
214
215 #else
216 #error "--enable-runtime-cpu-detect selected, but no CPU detection method " \
217  "available for your platform. Reconfigure without --enable-runtime-cpu-detect."
218 #endif