Version

menu_open
Wwise SDK 2024.1.4
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) 2025 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(__clang__) || defined(__GNUC__)
81  {
82  return in_bits ? __builtin_ctzll(in_bits) : 64;
83  }
84 #elif defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
86  {
87  unsigned long ret = 0;
88  _BitScanForward64(&ret, in_bits);
89  return in_bits ? ret : 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(__clang__) || defined (__GNUC__)
108  {
109  return in_bits ? __builtin_ctz(in_bits) : 32;
110  }
111 #elif defined _MSC_VER
113  {
114  unsigned long ret = 0;
115  _BitScanForward(&ret, in_bits);
116  return in_bits ? ret : 32;
117  }
118 #else
120  {
121  if (in_bits == 0) return 32;
122  AkUInt32 ret = 0;
123  if ((in_bits & 0x0000FFFFULL) == 0) { ret += 16; in_bits >>= 16; }
124  if ((in_bits & 0x000000FFULL) == 0) { ret += 8; in_bits >>= 8; }
125  if ((in_bits & 0x0000000FULL) == 0) { ret += 4; in_bits >>= 4; }
126  if ((in_bits & 0x00000003ULL) == 0) { ret += 2; in_bits >>= 2; }
127  if ((in_bits & 0x00000001ULL) == 0) { ret += 1; in_bits >>= 1; }
128  return ret;
129  }
130 #endif
131 
132  // AkBitScanReverse returns how many 0s there are after the most-significant-bit is set
133  // (or the length of the param if the value is zero)
134  // e.g. 0b0000`0000`0001`0000 would return 11
135 #if defined(__clang__) || defined (__GNUC__)
137  {
138  return in_bits ? __builtin_clzll(in_bits) : 64;
139  }
140 #elif defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
142  {
143  unsigned long ret = 0;
144  _BitScanReverse64(&ret, in_bits);
145  return in_bits ? 63 - ret : 64;
146  }
147 #else
149  {
150  if (in_bits == 0) return 64;
151  int ret = 0;
152  if ((in_bits & 0xFFFFFFFF00000000ULL) == 0) { ret += 32; in_bits <<= 32; }
153  if ((in_bits & 0xFFFF000000000000ULL) == 0) { ret += 16; in_bits <<= 16; }
154  if ((in_bits & 0xFF00000000000000ULL) == 0) { ret += 8; in_bits <<= 8; }
155  if ((in_bits & 0xF000000000000000ULL) == 0) { ret += 4; in_bits <<= 4; }
156  if ((in_bits & 0xC000000000000000ULL) == 0) { ret += 2; in_bits <<= 2; }
157  if ((in_bits & 0x8000000000000000ULL) == 0) { ret += 1; in_bits <<= 1; }
158  return ret;
159  }
160 #endif
161 
162 #if defined(__clang__) || defined (__GNUC__)
164  {
165  return in_bits ? __builtin_clz(in_bits) : 32;
166  }
167 #elif defined _MSC_VER
169  {
170  unsigned long ret = 0;
171  _BitScanReverse(&ret, in_bits);
172  return in_bits ? 31 - ret : 32;
173  }
174 #else
176  {
177  if (in_bits == 0) return 32;
178  int ret = 0;
179  if ((in_bits & 0xFFFF0000ULL) == 0) { ret += 16; in_bits <<= 16; }
180  if ((in_bits & 0xFF000000ULL) == 0) { ret += 8; in_bits <<= 8; }
181  if ((in_bits & 0xF0000000ULL) == 0) { ret += 4; in_bits <<= 4; }
182  if ((in_bits & 0xC0000000ULL) == 0) { ret += 2; in_bits <<= 2; }
183  if ((in_bits & 0x80000000ULL) == 0) { ret += 1; in_bits <<= 1; }
184  return ret;
185  }
186 #endif
187 
188 }
189 #endif // defined(AK_BIT_SCAN_INSTRUCTIONS)
190 
191 /// Utility functions
192 namespace AK
193 {
194  /// Count non-zero bits, e.g. to count # of channels in a channelmask
195  /// \return Number of channels.
197  {
198  return AKPLATFORM::AkPopCount(in_uWord);
199  }
200 
201  /// Computes the next power of two given a value.
202  /// \return next power of two.
204  {
205  in_uValue--;
206  in_uValue |= in_uValue >> 1;
207  in_uValue |= in_uValue >> 2;
208  in_uValue |= in_uValue >> 4;
209  in_uValue |= in_uValue >> 8;
210  in_uValue |= in_uValue >> 16;
211  in_uValue++;
212  return in_uValue;
213  }
214 
216  {
217  return (x << r) | (x >> (32 - r));
218  }
219 
221  {
222  return (x << r) | (x >> (64 - r));
223  }
224 }
Definition of data structures for AkAudioObject.
AkForceInline AkUInt32 AkBitScanReverse64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:148
Platform-dependent helpers.
AkForceInline AkUInt32 ROTL32(AkUInt32 x, AkUInt32 r)
Definition: AkBitFuncs.h:215
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:175
AkForceInline AkUInt32 GetNextPowerOfTwo(AkUInt32 in_uValue)
Definition: AkBitFuncs.h:203
AkForceInline AkUInt32 AkBitScanForward(AkUInt32 in_bits)
Definition: AkBitFuncs.h:119
AkForceInline AkUInt32 AkPopCount64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:52
AkForceInline AkUInt32 GetNumNonZeroBits(AkUInt32 in_uWord)
Definition: AkBitFuncs.h:196
uint64_t AkUInt64
Unsigned 64-bit integer.
AkForceInline AkUInt64 ROTL64(AkUInt64 x, AkUInt64 r)
Definition: AkBitFuncs.h:220
uint32_t AkUInt32
Unsigned 32-bit integer.
#define AkForceInline
Definition: AkTypes.h:63

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