Version
menu_open
Target Platform(s):
Wwise SDK 2023.1.5
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  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
31 
32 #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
33 #include <cpuid.h>
34 #endif
35 #include <time.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <sched.h>
39 
40 #define AK_SEC_TO_NANOSEC 1000000000ULL
41 
42 #define AK_THREAD_INIT_CODE(_threadProperties) \
43  if (_threadProperties.dwAffinityMask != AK_THREAD_AFFINITY_DEFAULT) \
44  { \
45  cpu_set_t affinity; \
46  CPU_ZERO(&affinity); \
47  for (AkUInt32 i = 0; i < 32; ++i) { \
48  if (_threadProperties.dwAffinityMask & ( 1 << i )){\
49  CPU_SET(i, &affinity); \
50  } \
51  } \
52  sched_setaffinity(0, sizeof(cpu_set_t), &affinity); \
53  }
54 
55 namespace AKPLATFORM
56 {
57  // Time functions
58  // ------------------------------------------------------------------
59 
60  /// Platform Independent Helper
61  inline void PerformanceCounter( AkInt64 * out_piLastTime )
62  {
63  struct timespec clockNow;
64  clock_gettime(CLOCK_MONOTONIC, &clockNow);
65  //This give the wallclock time in NS
66  *out_piLastTime = clockNow.tv_sec*AK_SEC_TO_NANOSEC + clockNow.tv_nsec;
67  }
68 
69  /// Platform Independent Helper
70  inline void PerformanceFrequency( AkInt64 * out_piFreq )
71  {
72  //Since Wall Clock is used, 1 NS is the frequency independent of the clock resolution
73  *out_piFreq = AK_SEC_TO_NANOSEC;
74  }
75 
76  template<class destType, class srcType>
77  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 *) )
78  {
79  size_t i;
80  size_t lenToCopy = srcStrLen(in_pSrc);
81 
82  lenToCopy = (lenToCopy > in_MaxSize-1) ? in_MaxSize-1 : lenToCopy;
83  for(i = 0; i < lenToCopy; i++)
84  {
85  in_pdDest[i] = (destType) in_pSrc[i];
86  }
87  in_pdDest[lenToCopy] = (destType)0;
88 
89  return lenToCopy;
90  }
91 
92  #define CONVERT_UTF16_TO_CHAR( _astring_, _charstring_ ) \
93  _charstring_ = (char*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(char) ); \
94  AK_UTF16_TO_CHAR( _charstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
95 
96  #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
97  #define AK_UTF8_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, strlen )
98  #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
99  #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, wcslen, AKPLATFORM::AkUtf16StrLen )
100  #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
101  #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
102  #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, wcslen )
103 
104  #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
105  #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
106 
107  /// Stack allocations.
108  #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
109 
110 #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
111  // Once our minimum compiler version supports __get_cpuid_count, these asm blocks can be replaced
112  #if defined(__i386__) && defined(__PIC__)
113  // %ebx may be the PIC register.
114  #define __ak_cpuid_count(level, count, a, b, c, d) \
115  __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
116  "cpuid\n\t" \
117  "xchg{l}\t{%%}ebx, %k1\n\t" \
118  : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
119  : "0" (level), "2" (count))
120  #elif defined(__x86_64__) && defined(__PIC__)
121  // %rbx may be the PIC register.
122  #define __ak_cpuid_count(level, count, a, b, c, d) \
123  __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
124  "cpuid\n\t" \
125  "xchg{q}\t{%%}rbx, %q1\n\t" \
126  : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
127  : "0" (level), "2" (count))
128  #else
129  #define __ak_cpuid_count(level, count, a, b, c, d) \
130  __asm__ ("cpuid\n\t" \
131  : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
132  : "0" (level), "2" (count))
133  #endif
134 
135  static __inline int __ak_get_cpuid_count(unsigned int __leaf,
136  unsigned int __subleaf,
137  unsigned int *__eax, unsigned int *__ebx,
138  unsigned int *__ecx, unsigned int *__edx)
139  {
140  unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
141 
142  if (__max_leaf == 0 || __max_leaf < __leaf)
143  return 0;
144 
145  __ak_cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
146  return 1;
147  }
148 
149  /// Support to fetch the CPUID for the platform. Only valid for X86 targets
150  /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
151  /// as it will have already translated the feature bits into AK-relevant enums
152  inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
153  {
154  __ak_get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
155  &out_uCPUFeatures[0],
156  &out_uCPUFeatures[1],
157  &out_uCPUFeatures[2],
158  &out_uCPUFeatures[3]);
159  }
160 #endif
161 }
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)
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
#define AK_SEC_TO_NANOSEC
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
#define __ak_cpuid_count(level, count, a, b, c, d)
int64_t AkInt64
Signed 64-bit integer.
void PerformanceFrequency(AkInt64 *out_piFreq)
Platform Independent Helper.
uint32_t AkUInt32
Unsigned 32-bit integer.
size_t AkSimpleConvertString(destType *in_pdDest, const srcType *in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *))

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