Version
menu_open
link
Target Platform(s):
Wwise SDK 2019.2.15
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: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> 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_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
96  #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, wcslen, AKPLATFORM::AkUtf16StrLen )
97  #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
98  #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
99  #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, wcslen )
100 
101  /// Stack allocations.
102  #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
103 
104 #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
105  // Once our minimum compiler version supports __get_cpuid_count, these asm blocks can be replaced
106  #if defined(__i386__) && defined(__PIC__)
107  // %ebx may be the PIC register.
108  #define __ak_cpuid_count(level, count, a, b, c, d) \
109  __asm__ ("xchg{l}\t{%%}ebx, %k1\n\t" \
110  "cpuid\n\t" \
111  "xchg{l}\t{%%}ebx, %k1\n\t" \
112  : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
113  : "0" (level), "2" (count))
114  #elif defined(__x86_64__) && defined(__PIC__)
115  // %rbx may be the PIC register.
116  #define __ak_cpuid_count(level, count, a, b, c, d) \
117  __asm__ ("xchg{q}\t{%%}rbx, %q1\n\t" \
118  "cpuid\n\t" \
119  "xchg{q}\t{%%}rbx, %q1\n\t" \
120  : "=a" (a), "=&r" (b), "=c" (c), "=d" (d) \
121  : "0" (level), "2" (count))
122  #else
123  #define __ak_cpuid_count(level, count, a, b, c, d) \
124  __asm__ ("cpuid\n\t" \
125  : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
126  : "0" (level), "2" (count))
127  #endif
128 
129  static __inline int __ak_get_cpuid_count(unsigned int __leaf,
130  unsigned int __subleaf,
131  unsigned int *__eax, unsigned int *__ebx,
132  unsigned int *__ecx, unsigned int *__edx)
133  {
134  unsigned int __max_leaf = __get_cpuid_max(__leaf & 0x80000000, 0);
135 
136  if (__max_leaf == 0 || __max_leaf < __leaf)
137  return 0;
138 
139  __ak_cpuid_count(__leaf, __subleaf, *__eax, *__ebx, *__ecx, *__edx);
140  return 1;
141  }
142 
143  /// Support to fetch the CPUID for the platform. Only valid for X86 targets
144  /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
145  /// as it will have already translated the feature bits into AK-relevant enums
146  inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
147  {
148  __ak_get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
149  &out_uCPUFeatures[0],
150  &out_uCPUFeatures[1],
151  &out_uCPUFeatures[2],
152  &out_uCPUFeatures[3]);
153  }
154 #endif
155 }
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:217
void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
Definition: AkPlatformFuncs.h:234
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:82
int64_t AkInt64
Signed 64-bit integer.
Definition: AkTypes.h:99
#define __ak_cpuid_count(level, count, a, b, c, d)
Definition: AkPlatformFuncs.h:123
void PerformanceFrequency(AkInt64 *out_piFreq)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:46
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:92
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:85

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