efl/src/lib/evas/common/evas_cpu.c

115 lines
2.6 KiB
C
Raw Normal View History

#include "evas_common_private.h"
static int cpu_feature_mask = 0;
static Eina_Bool
_cpu_check(Eina_Cpu_Features f)
{
Eina_Cpu_Features features = eina_cpu_features_get();
return (features & f) == f;
}
evas: Rename EAPI macro to EVAS_API in Evas library evil: Rename EAPI macro to EVIL_API in Evil library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-09-13 06:14:31 -07:00
EVAS_API void
evas_common_cpu_init(void)
2002-11-08 00:02:15 -08:00
{
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
evas_cpu: Avoid SIGILL in evas startup on x86 Summary: To determine if a system supports SIMD instructions, the cpuid facility should be used. However, for 15+ years EFL has been trapping SIGILL, then attempting to execute these intstructions. Continuing after SIGILL is explicitly undefined behaviour and can never safely be relied upon - it is possible the CPU will respond to the unknown instruction in an upredictable way and the program will not continue correctly. Even if it hasn't caused problems before, there's no reason to believe a processor released in the future won't behave differently. Lately we've had a couple of bug tickets where SIGILL appears to cause problems at a system level as well, but there seems little point in chasing those problems down as we shouldn't even be doing this in the first place. ref T6711 ref T6989 We still rely on SIGILL in a few configurations where eina_cpu doesn't know how to query features properly (powerpc, sparc, and non linux ARM configurations). Hopefully someone with expertise on those platforms can follow up and we can remove this entirely. Note: MMX2 appears to not really be a thing, and is instead provided by both 3DNow! and SSE. We already conflate it with SSE in other parts of evas, so I've just used SSE here to test for its presence. Depends on D6313 Reviewers: devilhorns, zmike Reviewed By: zmike Subscribers: cedric, #committers, zmike Tags: #efl Maniphest Tasks: T6989, T6711 Differential Revision: https://phab.enlightenment.org/D6314
2018-06-18 13:12:41 -07:00
cpu_feature_mask |= _cpu_check(EINA_CPU_MMX) * CPU_FEATURE_MMX;
if (getenv("EVAS_CPU_NO_MMX2"))
cpu_feature_mask &= ~CPU_FEATURE_MMX2;
evas_cpu: Avoid SIGILL in evas startup on x86 Summary: To determine if a system supports SIMD instructions, the cpuid facility should be used. However, for 15+ years EFL has been trapping SIGILL, then attempting to execute these intstructions. Continuing after SIGILL is explicitly undefined behaviour and can never safely be relied upon - it is possible the CPU will respond to the unknown instruction in an upredictable way and the program will not continue correctly. Even if it hasn't caused problems before, there's no reason to believe a processor released in the future won't behave differently. Lately we've had a couple of bug tickets where SIGILL appears to cause problems at a system level as well, but there seems little point in chasing those problems down as we shouldn't even be doing this in the first place. ref T6711 ref T6989 We still rely on SIGILL in a few configurations where eina_cpu doesn't know how to query features properly (powerpc, sparc, and non linux ARM configurations). Hopefully someone with expertise on those platforms can follow up and we can remove this entirely. Note: MMX2 appears to not really be a thing, and is instead provided by both 3DNow! and SSE. We already conflate it with SSE in other parts of evas, so I've just used SSE here to test for its presence. Depends on D6313 Reviewers: devilhorns, zmike Reviewed By: zmike Subscribers: cedric, #committers, zmike Tags: #efl Maniphest Tasks: T6989, T6711 Differential Revision: https://phab.enlightenment.org/D6314
2018-06-18 13:12:41 -07:00
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
evas_cpu: Avoid SIGILL in evas startup on x86 Summary: To determine if a system supports SIMD instructions, the cpuid facility should be used. However, for 15+ years EFL has been trapping SIGILL, then attempting to execute these intstructions. Continuing after SIGILL is explicitly undefined behaviour and can never safely be relied upon - it is possible the CPU will respond to the unknown instruction in an upredictable way and the program will not continue correctly. Even if it hasn't caused problems before, there's no reason to believe a processor released in the future won't behave differently. Lately we've had a couple of bug tickets where SIGILL appears to cause problems at a system level as well, but there seems little point in chasing those problems down as we shouldn't even be doing this in the first place. ref T6711 ref T6989 We still rely on SIGILL in a few configurations where eina_cpu doesn't know how to query features properly (powerpc, sparc, and non linux ARM configurations). Hopefully someone with expertise on those platforms can follow up and we can remove this entirely. Note: MMX2 appears to not really be a thing, and is instead provided by both 3DNow! and SSE. We already conflate it with SSE in other parts of evas, so I've just used SSE here to test for its presence. Depends on D6313 Reviewers: devilhorns, zmike Reviewed By: zmike Subscribers: cedric, #committers, zmike Tags: #efl Maniphest Tasks: T6989, T6711 Differential Revision: https://phab.enlightenment.org/D6314
2018-06-18 13:12:41 -07:00
cpu_feature_mask |= _cpu_check(EINA_CPU_SSE) * CPU_FEATURE_SSE;
# ifdef BUILD_SSE3
if (getenv("EVAS_CPU_NO_SSE3"))
evas_cpu: Avoid SIGILL in evas startup on x86 Summary: To determine if a system supports SIMD instructions, the cpuid facility should be used. However, for 15+ years EFL has been trapping SIGILL, then attempting to execute these intstructions. Continuing after SIGILL is explicitly undefined behaviour and can never safely be relied upon - it is possible the CPU will respond to the unknown instruction in an upredictable way and the program will not continue correctly. Even if it hasn't caused problems before, there's no reason to believe a processor released in the future won't behave differently. Lately we've had a couple of bug tickets where SIGILL appears to cause problems at a system level as well, but there seems little point in chasing those problems down as we shouldn't even be doing this in the first place. ref T6711 ref T6989 We still rely on SIGILL in a few configurations where eina_cpu doesn't know how to query features properly (powerpc, sparc, and non linux ARM configurations). Hopefully someone with expertise on those platforms can follow up and we can remove this entirely. Note: MMX2 appears to not really be a thing, and is instead provided by both 3DNow! and SSE. We already conflate it with SSE in other parts of evas, so I've just used SSE here to test for its presence. Depends on D6313 Reviewers: devilhorns, zmike Reviewed By: zmike Subscribers: cedric, #committers, zmike Tags: #efl Maniphest Tasks: T6989, T6711 Differential Revision: https://phab.enlightenment.org/D6314
2018-06-18 13:12:41 -07:00
cpu_feature_mask &= ~CPU_FEATURE_SSE3;
else
evas_cpu: Avoid SIGILL in evas startup on x86 Summary: To determine if a system supports SIMD instructions, the cpuid facility should be used. However, for 15+ years EFL has been trapping SIGILL, then attempting to execute these intstructions. Continuing after SIGILL is explicitly undefined behaviour and can never safely be relied upon - it is possible the CPU will respond to the unknown instruction in an upredictable way and the program will not continue correctly. Even if it hasn't caused problems before, there's no reason to believe a processor released in the future won't behave differently. Lately we've had a couple of bug tickets where SIGILL appears to cause problems at a system level as well, but there seems little point in chasing those problems down as we shouldn't even be doing this in the first place. ref T6711 ref T6989 We still rely on SIGILL in a few configurations where eina_cpu doesn't know how to query features properly (powerpc, sparc, and non linux ARM configurations). Hopefully someone with expertise on those platforms can follow up and we can remove this entirely. Note: MMX2 appears to not really be a thing, and is instead provided by both 3DNow! and SSE. We already conflate it with SSE in other parts of evas, so I've just used SSE here to test for its presence. Depends on D6313 Reviewers: devilhorns, zmike Reviewed By: zmike Subscribers: cedric, #committers, zmike Tags: #efl Maniphest Tasks: T6989, T6711 Differential Revision: https://phab.enlightenment.org/D6314
2018-06-18 13:12:41 -07:00
cpu_feature_mask |= _cpu_check(EINA_CPU_SSE3) * CPU_FEATURE_SSE3;
# endif /* BUILD_SSE3 */
#endif /* BUILD_MMX */
#ifdef BUILD_ALTIVEC
if (getenv("EVAS_CPU_NO_ALTIVEC"))
cpu_feature_mask &= ~CPU_FEATURE_ALTIVEC;
else
cpu_feature_mask |= _cpu_check(CPU_FEATURE_ALTIVEC) * CPU_FEATURE_ALTIVEC;
#endif /* BUILD_ALTIVEC */
#if defined(__ARM_ARCH__)
# ifdef BUILD_NEON
if (getenv("EVAS_CPU_NO_NEON"))
cpu_feature_mask &= ~CPU_FEATURE_NEON;
else
cpu_feature_mask |= _cpu_check(EINA_CPU_NEON) * CPU_FEATURE_NEON;
# endif
#endif
#if defined(__aarch64__)
# ifdef BUILD_NEON
if (getenv("EVAS_CPU_NO_NEON"))
cpu_feature_mask &= ~CPU_FEATURE_NEON;
else
cpu_feature_mask |= CPU_FEATURE_NEON;
# endif
if (getenv("EVAS_CPU_NO_SVE"))
cpu_feature_mask &= ~CPU_FEATURE_SVE;
else
cpu_feature_mask |= _cpu_check(EINA_CPU_SVE) * CPU_FEATURE_SVE;
#endif
}
int
evas_common_cpu_has_feature(unsigned int feature)
{
return (cpu_feature_mask & feature);
2002-11-08 00:02:15 -08:00
}
int
evas_common_cpu_have_cpuid(void)
2002-11-08 00:02:15 -08:00
{
return 0;
2002-11-08 00:02:15 -08:00
}
evas: Rename EAPI macro to EVAS_API in Evas library evil: Rename EAPI macro to EVIL_API in Evil library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-09-13 06:14:31 -07:00
EVAS_API void
evas_common_cpu_can_do(int *mmx, int *sse, int *sse2)
2002-11-08 00:02:15 -08:00
{
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;
2002-11-08 00:02:15 -08:00
}
2002-11-08 00:02:15 -08:00
*mmx = do_mmx;
*sse = do_sse;
*sse2 = do_sse2;
}
#ifdef BUILD_MMX
evas: Rename EAPI macro to EVAS_API in Evas library evil: Rename EAPI macro to EVIL_API in Evil library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-09-13 06:14:31 -07:00
EVAS_API void
evas_common_cpu_end_opt(void)
2002-11-08 00:02:15 -08:00
{
if (cpu_feature_mask & (CPU_FEATURE_MMX | CPU_FEATURE_MMX2))
{
emms();
}
2002-11-08 00:02:15 -08:00
}
#else
evas: Rename EAPI macro to EVAS_API in Evas library evil: Rename EAPI macro to EVIL_API in Evil library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-09-13 06:14:31 -07:00
EVAS_API void
evas_common_cpu_end_opt(void)
2002-11-08 00:02:15 -08:00
{
}
#endif