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

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise