ImfSystemSpecific.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2009-2014 DreamWorks Animation LLC.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of DreamWorks Animation nor the names of
  17. // its contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////
  33. #include "ImfSimd.h"
  34. #include "ImfSystemSpecific.h"
  35. #include "ImfNamespace.h"
  36. #include "OpenEXRConfig.h"
  37. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  38. namespace {
  39. #if defined(IMF_HAVE_SSE2) && defined(__GNUC__) && !defined(__ANDROID__)
  40. // Helper functions for gcc + SSE enabled
  41. void cpuid(int n, int &eax, int &ebx, int &ecx, int &edx)
  42. {
  43. __asm__ __volatile__ (
  44. "cpuid"
  45. : /* Output */ "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  46. : /* Input */ "a"(n)
  47. : /* Clobber */);
  48. }
  49. #else // IMF_HAVE_SSE2 && __GNUC__
  50. // Helper functions for generic compiler - all disabled
  51. void cpuid(int n, int &eax, int &ebx, int &ecx, int &edx)
  52. {
  53. eax = ebx = ecx = edx = 0;
  54. }
  55. #endif // IMF_HAVE_SSE2 && __GNUC__
  56. #ifdef OPENEXR_IMF_HAVE_GCC_INLINE_ASM_AVX
  57. void xgetbv(int n, int &eax, int &edx)
  58. {
  59. __asm__ __volatile__ (
  60. "xgetbv"
  61. : /* Output */ "=a"(eax), "=d"(edx)
  62. : /* Input */ "c"(n)
  63. : /* Clobber */);
  64. }
  65. #else // OPENEXR_IMF_HAVE_GCC_INLINE_ASM_AVX
  66. void xgetbv(int n, int &eax, int &edx)
  67. {
  68. eax = edx = 0;
  69. }
  70. #endif // OPENEXR_IMF_HAVE_GCC_INLINE_ASM_AVX
  71. } // namespace
  72. CpuId::CpuId():
  73. sse2(false),
  74. sse3(false),
  75. ssse3(false),
  76. sse4_1(false),
  77. sse4_2(false),
  78. avx(false),
  79. f16c(false)
  80. {
  81. bool osxsave = false;
  82. int max = 0;
  83. int eax, ebx, ecx, edx;
  84. cpuid(0, max, ebx, ecx, edx);
  85. if (max > 0)
  86. {
  87. cpuid(1, eax, ebx, ecx, edx);
  88. sse2 = ( edx & (1<<26) );
  89. sse3 = ( ecx & (1<< 0) );
  90. ssse3 = ( ecx & (1<< 9) );
  91. sse4_1 = ( ecx & (1<<19) );
  92. sse4_2 = ( ecx & (1<<20) );
  93. osxsave = ( ecx & (1<<27) );
  94. avx = ( ecx & (1<<28) );
  95. f16c = ( ecx & (1<<29) );
  96. if (!osxsave)
  97. {
  98. avx = f16c = false;
  99. }
  100. else
  101. {
  102. xgetbv(0, eax, edx);
  103. // eax bit 1 - SSE managed, bit 2 - AVX managed
  104. if ((eax & 6) != 6)
  105. {
  106. avx = f16c = false;
  107. }
  108. }
  109. }
  110. #if defined(IMF_HAVE_SSE2) && defined(__ANDROID__)
  111. sse2 = true;
  112. sse3 = true;
  113. #ifdef __x86_64__
  114. ssse3 = true;
  115. sse4_1 = true;
  116. #endif
  117. #endif
  118. }
  119. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT