summaryrefslogtreecommitdiff
path: root/src/lib/evas/common/evas_cpu.c
blob: d9215e11408b0ca15aab93324baf1137c169e060 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "evas_common_private.h"
#ifdef BUILD_NEON
# ifdef BUILD_NEON_INTRINSICS
#  include <arm_neon.h>
# endif
#endif

static int cpu_feature_mask = 0;

#ifdef BUILD_ALTIVEC
# ifdef __POWERPC__
#  ifdef __VEC__
#   define NEED_FEATURE_TEST
#  endif
# endif
#endif

#ifdef __SPARC__
#   define NEED_FEATURE_TEST
#endif

#if defined(__ARM_ARCH__)
# ifdef BUILD_NEON
#  define NEED_FEATURE_TEST
# endif
#endif

#if defined(__aarch64__)
# ifdef BUILD_NEON
#  define NEED_FEATURE_TEST
# endif
#endif

#ifdef NEED_FEATURE_TEST
# ifdef HAVE_SIGLONGJMP
#  include <signal.h>
#  include <setjmp.h>
#  include <errno.h>

static sigjmp_buf detect_buf;
static void evas_common_cpu_catch_ill(int sig);
static void evas_common_cpu_catch_segv(int sig);

static void
evas_common_cpu_catch_ill(int sig EINA_UNUSED)
{
   siglongjmp(detect_buf, 1);
}

static void
evas_common_cpu_catch_segv(int sig EINA_UNUSED)
{
   siglongjmp(detect_buf, 1);
}
# endif

# ifdef BUILD_ALTIVEC
void
evas_common_cpu_altivec_test(void)
{
#  ifdef __POWERPC__
#   ifdef __VEC__
   vector unsigned int zero;

   zero = vec_splat_u32(0);
#   endif /* __VEC__ */
#  endif /* __POWERPC__ */
}
# endif /* BUILD_ALTIVEC */

void
evas_common_cpu_neon_test(void)
{
//#if defined(__ARM_ARCH__) && (__ARM_ARCH__ >= 70)
# ifdef BUILD_NEON
#  ifdef BUILD_NEON_INTRINSICS
   volatile uint32x4_t temp = vdupq_n_u32(0x1);
   vaddq_u32(temp, temp);
#  else
   asm volatile (
		".fpu neon	     \n\t"
                 "vqadd.u8 d0, d1, d0\n"
                 : /* Out */
                 : /* In */
                 : /* Clobbered */
                 "d0", "d1"
                 );
#  endif
# endif
//#endif
}

void
evas_common_cpu_sve_test(void)
{
#if defined(__aarch64__)
   volatile int result = 123;
   asm("movz %w[res], #10" : [res] "=r" (result));
#endif
}

void
evas_common_cpu_vis_test(void)
{
# ifdef __SPARC__
# endif /* __SPARC__ */
}
#endif /* NEED_FEATURE_TEST */


#if defined(BUILD_MMX) || defined(BUILD_SSE3)
# define NEED_CPU_CHECK
#endif

#ifdef NEED_FEATURE_TEST
# ifndef HAVE_SIGLONGJMP
#  undef NEED_CPU_CHECK
#  define NEED_CPU_CHECK
# endif
#endif

#ifdef NEED_CPU_CHECK
static Eina_Bool
_cpu_check(Eina_Cpu_Features f)
{
   Eina_Cpu_Features features;

   features = eina_cpu_features_get();
   return (features & f) == f;
}
#endif

#ifdef NEED_FEATURE_TEST
int
evas_common_cpu_feature_test(void (*feature)(void))
{
# ifdef HAVE_SIGLONGJMP
   int enabled = 1;
   struct sigaction act, oact, oact2;

   act.sa_handler = evas_common_cpu_catch_ill;
   act.sa_flags = SA_RESTART;
   sigemptyset(&act.sa_mask);
   sigaction(SIGILL, &act, &oact);

   act.sa_handler = evas_common_cpu_catch_segv;
   act.sa_flags = SA_RESTART;
   sigemptyset(&act.sa_mask);
   sigaction(SIGSEGV, &act, &oact2);

   if (sigsetjmp(detect_buf, 1))
     {
        sigaction(SIGILL, &oact, NULL);
        sigaction(SIGSEGV, &oact2, NULL);
        return 0;
     }

   feature();

   sigaction(SIGILL, &oact, NULL);
   sigaction(SIGSEGV, &oact2, NULL);
   return enabled;
# else
#  ifdef BUILD_MMX
   if (feature == evas_common_cpu_mmx_test)
     return _cpu_check(EINA_CPU_MMX);
   /* no mmx2 support in eina */
   if (feature == evas_common_cpu_sse_test)
     return _cpu_check(EINA_CPU_SSE);
#  endif
#  ifdef BUILD_SSE3
   if (feature == evas_common_cpu_sse3_test)
     return _cpu_check(EINA_CPU_SSE3);
#  endif
#  ifdef BUILD_ALTIVEC
   if (feature == evas_common_cpu_altivec_test)
     return _cpu_check(CPU_FEATURE_ALTIVEC);
#  endif
#  ifdef BUILD_NEON
   if (feature == evas_common_cpu_neon_test)
     return _cpu_check(EINA_CPU_NEON);
#  endif
#  if defined(__aarch64__)
   if (feature == evas_common_cpu_sve_test)
     return _cpu_check(EINA_CPU_SVE);
#  endif
   return 0;
# endif
}
#endif

EAPI void
evas_common_cpu_init(void)
{
   static int called = 0;

   if (called) return;
   called = 1;
#ifdef BUILD_MMX
   if (getenv("EVAS_CPU_NO_MMX"))
     cpu_feature_mask &= ~CPU_FEATURE_MMX;
   else
     cpu_feature_mask |= _cpu_check(EINA_CPU_MMX) * CPU_FEATURE_MMX;
   if (getenv("EVAS_CPU_NO_MMX2"))
     cpu_feature_mask &= ~CPU_FEATURE_MMX2;
   else /* It seems "MMX2" is actually part of SSE (and 3DNow)? */
     cpu_feature_mask |= _cpu_check(EINA_CPU_SSE) * CPU_FEATURE_MMX2;
   if (getenv("EVAS_CPU_NO_SSE"))
     cpu_feature_mask &= ~CPU_FEATURE_SSE;
   else
     cpu_feature_mask |= _cpu_check(EINA_CPU_SSE) * CPU_FEATURE_SSE;
# ifdef BUILD_SSE3
   if (getenv("EVAS_CPU_NO_SSE3"))
     cpu_feature_mask &= ~CPU_FEATURE_SSE3;
   else
     cpu_feature_mask |= _cpu_check(EINA_CPU_SSE3) * CPU_FEATURE_SSE3;
# endif /* BUILD_SSE3 */
#endif /* BUILD_MMX */

#ifdef BUILD_ALTIVEC
# ifdef __POWERPC__
#  ifdef __VEC__
   if (getenv("EVAS_CPU_NO_ALTIVEC"))
     cpu_feature_mask &= ~CPU_FEATURE_ALTIVEC;
   else
     {
        cpu_feature_mask |= CPU_FEATURE_ALTIVEC *
          evas_common_cpu_feature_test(evas_common_cpu_altivec_test);
        evas_common_cpu_end_opt();
     }
#  endif /* __VEC__ */
# endif /* __POWERPC__ */
#endif /* BUILD_ALTIVEC */

#ifdef __SPARC__
   if (getenv("EVAS_CPU_NO_VIS"))
     cpu_feature_mask &= ~CPU_FEATURE_VIS;
   else
     {
        cpu_feature_mask |= CPU_FEATURE_VIS *
          evas_common_cpu_feature_test(evas_common_cpu_vis_test);
        evas_common_cpu_end_opt();
     }
#endif /* __SPARC__ */

#if defined(__ARM_ARCH__)
# ifdef BUILD_NEON
   if (getenv("EVAS_CPU_NO_NEON"))
     cpu_feature_mask &= ~CPU_FEATURE_NEON;
   else
     {
        /* On linux eina_cpu sets this up with getauxval() */
#  if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H) && defined(__arm__) && defined(__linux__)
        cpu_feature_mask |= CPU_FEATURE_NEON *
          !!(eina_cpu_features_get() & EINA_CPU_NEON);
#  else
        cpu_feature_mask |= CPU_FEATURE_NEON *
          evas_common_cpu_feature_test(evas_common_cpu_neon_test);
        evas_common_cpu_end_opt();
#  endif
     }
# endif
#endif

#if defined(__aarch64__)
   if (getenv("EVAS_CPU_NO_NEON"))
     cpu_feature_mask &= ~CPU_FEATURE_NEON;
   else
     cpu_feature_mask |= CPU_FEATURE_NEON;
#endif

#if defined(__aarch64__)
   if (getenv("EVAS_CPU_NO_SVE"))
     cpu_feature_mask &= ~CPU_FEATURE_SVE;
   else
     {
#  if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ASM_HWCAP_H) && defined(__arm__) && defined(__linux__)
#error "xx"
        cpu_feature_mask |= CPU_FEATURE_SVE *
          !!(eina_cpu_features_get() & EINA_CPU_SVE);
#  else
        cpu_feature_mask |= CPU_FEATURE_SVE *
          evas_common_cpu_feature_test(evas_common_cpu_sve_test);
        evas_common_cpu_end_opt();
#  endif
     }
#endif
}

int
evas_common_cpu_has_feature(unsigned int feature)
{
   return (cpu_feature_mask & feature);
}

int
evas_common_cpu_have_cpuid(void)
{
   return 0;
/*
#ifdef BUILD_MMX
   unsigned int have_cpu_id;

   have_cpu_id = 0;
   have_cpuid(have_cpu_id);
   return have_cpu_id;
#else
   return 0;
#endif
 */
}

EAPI void
evas_common_cpu_can_do(int *mmx, int *sse, int *sse2)
{
   static int do_mmx = 0, do_sse = 0, do_sse2 = 0, done = 0;

   if (!done)
     {
        if (cpu_feature_mask & CPU_FEATURE_MMX) do_mmx = 1;
        if (cpu_feature_mask & CPU_FEATURE_MMX2) do_sse = 1;
        if (cpu_feature_mask & CPU_FEATURE_SSE) do_sse = 1;
        done = 1;
     }

   *mmx = do_mmx;
   *sse = do_sse;
   *sse2 = do_sse2;
}

#ifdef BUILD_MMX
EAPI void
evas_common_cpu_end_opt(void)
{
   if (cpu_feature_mask & (CPU_FEATURE_MMX | CPU_FEATURE_MMX2))
     {
	emms();
     }
}
#else
EAPI void
evas_common_cpu_end_opt(void)
{
}
#endif