Version

menu_open
Attention : vous avez été redirigé vers la plus récente documentation correspondant à votre version générale ( 2023.1.7.8574 ). Si vous souhaitez accéder à la documentation de votre version précise, veuillez télécharger la documentation hors ligne depuis l'Audiokinetic Launcher et sélectionner l'option de documentation Offline dans l'application de création Wwise.
Wwise SDK 2023.1.7
AkBitFuncs.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 // AkBitFuncs.h
28 
29 /// \file
30 /// Functions for defining various bit-manipulation functions
31 
32 #pragma once
33 
35 
36 #if defined (AK_XBOX)
38 #endif
39 
40 // some platforms will define their own optimized bitscan functions
41 #if !defined(AK_BIT_SCAN_INSTRUCTIONS)
42 namespace AKPLATFORM
43 {
44  // AkPopCount64 returns how many set bits there are in the provided value
45  // e.g. 0b0000`1111`0000`0011 would return 6
46 #if defined(__clang__) || defined (__GNUC__)
48  {
49  return __builtin_popcountll(in_bits);
50  }
51 #else
53  {
54  AkUInt32 num = 0;
55  while (in_bits) { ++num; in_bits &= in_bits - 1; }
56  return num;
57  }
58 #endif
59 
60  // AkPopCount returns how many set bits there are in the provided value
61  // e.g. 0b0000`1111`0000`0011 would return 6
62 #if defined(__clang__) || defined (__GNUC__)
64  {
65  return __builtin_popcount(in_bits);
66  }
67 #else
69  {
70  AkUInt32 num = 0;
71  while (in_bits) { ++num; in_bits &= in_bits - 1; }
72  return num;
73  }
74 #endif
75 
76  // AkBitScanForward returns how many 0s there are until the least-significant-bit is set
77  // (or the length of the param if the value is zero)
78  // e.g. 0b0000`0000`0001`0000 would return 4
79 #if defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
81  {
82  unsigned long ret = 0;
83  _BitScanForward64(&ret, in_bits);
84  return in_bits ? ret : 64;
85  }
86 #elif defined(__clang__) || defined (__GNUC__)
88  {
89  return in_bits ? __builtin_ctzll(in_bits) : 64;
90  }
91 #else
93  {
94  if (in_bits == 0) return 64;
95  AkUInt32 ret = 0;
96  if ((in_bits & 0x00000000FFFFFFFFULL) == 0) { ret += 32; in_bits >>= 32; }
97  if ((in_bits & 0x000000000000FFFFULL) == 0) { ret += 16; in_bits >>= 16; }
98  if ((in_bits & 0x00000000000000FFULL) == 0) { ret += 8; in_bits >>= 8; }
99  if ((in_bits & 0x000000000000000FULL) == 0) { ret += 4; in_bits >>= 4; }
100  if ((in_bits & 0x0000000000000003ULL) == 0) { ret += 2; in_bits >>= 2; }
101  if ((in_bits & 0x0000000000000001ULL) == 0) { ret += 1; in_bits >>= 1; }
102  return ret;
103  }
104 #endif
105 
106 #if defined _MSC_VER
108  {
109  unsigned long ret = 0;
110  _BitScanForward(&ret, in_bits);
111  return in_bits ? ret : 32;
112  }
113 
114 #elif defined(__clang__) || defined (__GNUC__)
116  {
117  return in_bits ? __builtin_ctz(in_bits) : 32;
118  }
119 #else
121  {
122  if (in_bits == 0) return 32;
123  AkUInt32 ret = 0;
124  if ((in_bits & 0x0000FFFFULL) == 0) { ret += 16; in_bits >>= 16; }
125  if ((in_bits & 0x000000FFULL) == 0) { ret += 8; in_bits >>= 8; }
126  if ((in_bits & 0x0000000FULL) == 0) { ret += 4; in_bits >>= 4; }
127  if ((in_bits & 0x00000003ULL) == 0) { ret += 2; in_bits >>= 2; }
128  if ((in_bits & 0x00000001ULL) == 0) { ret += 1; in_bits >>= 1; }
129  return ret;
130  }
131 #endif
132 
133  // AkBitScanReverse returns how many 0s there are after the most-significant-bit is set
134  // (or the length of the param if the value is zero)
135  // e.g. 0b0000`0000`0001`0000 would return 11
136 #if defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
138  {
139  unsigned long ret = 0;
140  _BitScanReverse64(&ret, in_bits);
141  return in_bits ? 63 - ret : 64;
142  }
143 #elif defined(__clang__) || defined (__GNUC__)
145  {
146  return in_bits ? __builtin_clzll(in_bits) : 64;
147  }
148 #else
150  {
151  if (in_bits == 0) return 64;
152  int ret = 0;
153  if ((in_bits & 0xFFFFFFFF00000000ULL) == 0) { ret += 32; in_bits <<= 32; }
154  if ((in_bits & 0xFFFF000000000000ULL) == 0) { ret += 16; in_bits <<= 16; }
155  if ((in_bits & 0xFF00000000000000ULL) == 0) { ret += 8; in_bits <<= 8; }
156  if ((in_bits & 0xF000000000000000ULL) == 0) { ret += 4; in_bits <<= 4; }
157  if ((in_bits & 0xC000000000000000ULL) == 0) { ret += 2; in_bits <<= 2; }
158  if ((in_bits & 0x8000000000000000ULL) == 0) { ret += 1; in_bits <<= 1; }
159  return ret;
160  }
161 #endif
162 
163 #if defined _MSC_VER
165  {
166  unsigned long ret = 0;
167  _BitScanReverse(&ret, in_bits);
168  return in_bits ? 31 - ret : 32;
169  }
170 #elif defined(__clang__) || defined (__GNUC__)
172  {
173  return in_bits ? __builtin_clz(in_bits) : 32;
174  }
175 #else
177  {
178  if (in_bits == 0) return 32;
179  int ret = 0;
180  if ((in_bits & 0xFFFF0000ULL) == 0) { ret += 16; in_bits <<= 16; }
181  if ((in_bits & 0xFF000000ULL) == 0) { ret += 8; in_bits <<= 8; }
182  if ((in_bits & 0xF0000000ULL) == 0) { ret += 4; in_bits <<= 4; }
183  if ((in_bits & 0xC0000000ULL) == 0) { ret += 2; in_bits <<= 2; }
184  if ((in_bits & 0x80000000ULL) == 0) { ret += 1; in_bits <<= 1; }
185  return ret;
186  }
187 #endif
188 
189 }
190 #endif // defined(AK_BIT_SCAN_INSTRUCTIONS)
191 
192 /// Utility functions
193 namespace AK
194 {
195  /// Count non-zero bits, e.g. to count # of channels in a channelmask
196  /// \return Number of channels.
198  {
199  return AKPLATFORM::AkPopCount(in_uWord);
200  }
201 
202  /// Computes the next power of two given a value.
203  /// \return next power of two.
205  {
206  in_uValue--;
207  in_uValue |= in_uValue >> 1;
208  in_uValue |= in_uValue >> 2;
209  in_uValue |= in_uValue >> 4;
210  in_uValue |= in_uValue >> 8;
211  in_uValue |= in_uValue >> 16;
212  in_uValue++;
213  return in_uValue;
214  }
215 
217  {
218  return (x << r) | (x >> (32 - r));
219  }
220 
222  {
223  return (x << r) | (x >> (64 - r));
224  }
225 }
Audiokinetic namespace.
AkForceInline AkUInt32 AkBitScanReverse64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:149
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
AkForceInline AkUInt32 ROTL32(AkUInt32 x, AkUInt32 r)
Definition: AkBitFuncs.h:216
AkForceInline AkUInt32 AkPopCount(AkUInt32 in_bits)
Definition: AkBitFuncs.h:68
AkForceInline AkUInt32 AkBitScanForward64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:92
AkForceInline AkUInt32 AkBitScanReverse(AkUInt32 in_bits)
Definition: AkBitFuncs.h:176
AkForceInline AkUInt32 GetNextPowerOfTwo(AkUInt32 in_uValue)
Definition: AkBitFuncs.h:204
AkForceInline AkUInt32 AkBitScanForward(AkUInt32 in_bits)
Definition: AkBitFuncs.h:120
AkForceInline AkUInt32 AkPopCount64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:52
AkForceInline AkUInt32 GetNumNonZeroBits(AkUInt32 in_uWord)
Definition: AkBitFuncs.h:197
uint64_t AkUInt64
Unsigned 64-bit integer.
AkForceInline AkUInt64 ROTL64(AkUInt64 x, AkUInt64 r)
Definition: AkBitFuncs.h:221
uint32_t AkUInt32
Unsigned 32-bit integer.
#define AkForceInline
Definition: AkTypes.h:63

Cette page a-t-elle été utile ?

Besoin d'aide ?

Des questions ? Des problèmes ? Besoin de plus d'informations ? Contactez-nous, nous pouvons vous aider !

Visitez notre page d'Aide

Décrivez-nous de votre projet. Nous sommes là pour vous aider.

Enregistrez votre projet et nous vous aiderons à démarrer sans aucune obligation !

Partir du bon pied avec Wwise