AkPlatformFuncs.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/Tools/Common/AkAssert.h>
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  24. #include <cpuid.h>
  25. #endif
  26. #include <time.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <sched.h>
  30. #define AK_THREAD_INIT_CODE(_threadProperties) \
  31. if (_threadProperties.dwAffinityMask != AK_THREAD_AFFINITY_DEFAULT) \
  32. { \
  33. cpu_set_t affinity; \
  34. CPU_ZERO(&affinity); \
  35. for (AkUInt32 i = 0; i < 32; ++i) { \
  36. if (_threadProperties.dwAffinityMask & ( 1 << i )){\
  37. CPU_SET(i, &affinity); \
  38. } \
  39. } \
  40. sched_setaffinity(0, sizeof(cpu_set_t), &affinity); \
  41. }
  42. namespace AKPLATFORM
  43. {
  44. // Time functions
  45. // ------------------------------------------------------------------
  46. /// Platform Independent Helper
  47. inline void PerformanceCounter( AkInt64 * out_piLastTime )
  48. {
  49. struct timespec clockNow;
  50. clock_gettime(CLOCK_MONOTONIC, &clockNow);
  51. *out_piLastTime = ((clockNow.tv_sec + clockNow.tv_nsec/ 1000000000.0) * CLOCKS_PER_SEC);
  52. }
  53. /// Platform Independent Helper
  54. inline void PerformanceFrequency( AkInt64 * out_piFreq )
  55. {
  56. // TO DO ANDROID ... is there something better
  57. *out_piFreq = CLOCKS_PER_SEC;
  58. }
  59. template<class destType, class srcType>
  60. inline size_t AkSimpleConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *) )
  61. {
  62. size_t i;
  63. size_t lenToCopy = srcStrLen(in_pSrc);
  64. lenToCopy = (lenToCopy > in_MaxSize-1) ? in_MaxSize-1 : lenToCopy;
  65. for(i = 0; i < lenToCopy; i++)
  66. {
  67. in_pdDest[i] = (destType) in_pSrc[i];
  68. }
  69. in_pdDest[lenToCopy] = (destType)0;
  70. return lenToCopy;
  71. }
  72. #define CONVERT_UTF16_TO_CHAR( _astring_, _charstring_ ) \
  73. _charstring_ = (char*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(char) ); \
  74. AK_UTF16_TO_CHAR( _charstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
  75. #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  76. #define AK_UTF8_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, strlen )
  77. #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
  78. #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, wcslen, AKPLATFORM::AkUtf16StrLen )
  79. #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
  80. #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
  81. #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, wcslen )
  82. #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
  83. #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
  84. /// Stack allocations.
  85. #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
  86. #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
  87. // Once our minimum compiler version supports __get_cpuid_count, these asm blocks can be replaced
  88. #if defined(__i386__) && defined(__PIC__)
  89. // %ebx may be the PIC register.
  90. #define __ak_cpuid_count(level, count, a, b, c, d) \
  91. __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
  92. "cpuid\n\t" \
  93. "xchg{l}\t{%%}ebx, %k1\n\t" \
  94. : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
  95. : "0" (level), "2" (count))
  96. #elif defined(__x86_64__) && defined(__PIC__)
  97. // %rbx may be the PIC register.
  98. #define __ak_cpuid_count(level, count, a, b, c, d) \
  99. __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
  100. "cpuid\n\t" \
  101. "xchg{q}\t{%%}rbx, %q1\n\t" \
  102. : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
  103. : "0" (level), "2" (count))
  104. #else
  105. #define __ak_cpuid_count(level, count, a, b, c, d) \
  106. __asm__ ("cpuid\n\t" \
  107. : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
  108. : "0" (level), "2" (count))
  109. #endif
  110. static __inline int __ak_get_cpuid_count(unsigned int __leaf,
  111. unsigned int __subleaf,
  112. unsigned int *__eax, unsigned int *__ebx,
  113. unsigned int *__ecx, unsigned int *__edx)
  114. {
  115. unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
  116. if (__max_leaf == 0 || __max_leaf < __leaf)
  117. return 0;
  118. __ak_cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
  119. return 1;
  120. }
  121. /// Support to fetch the CPUID for the platform. Only valid for X86 targets
  122. /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
  123. /// as it will have already translated the feature bits into AK-relevant enums
  124. inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
  125. {
  126. __ak_get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
  127. &out_uCPUFeatures[0],
  128. &out_uCPUFeatures[1],
  129. &out_uCPUFeatures[2],
  130. &out_uCPUFeatures[3]);
  131. }
  132. #endif
  133. }