Version
menu_open
Wwise SDK 2023.1.5
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  // AkPopCount returns how many set bits there are in the provided value
45  // e.g. 0b0000`1111`0000`0011 would return 6
46 #if __clang__ || defined __GNUC__
48  {
49  return __builtin_popcount(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  // AkBitScanForward returns how many 0s there are until the least-significant-bit is set
61  // (or the length of the param if the value is zero)
62  // e.g. 0b0000`0000`0001`0000 would return 4
63 #if defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
65  {
66  unsigned long ret = 0;
67  _BitScanForward64(&ret, in_bits);
68  return in_bits ? ret : 64;
69  }
70 #elif __clang__ || defined __GNUC__
72  {
73  return in_bits ? __builtin_ctzll(in_bits) : 64;
74  }
75 #else
77  {
78  if (in_bits == 0) return 64;
79  AkUInt32 ret = 0;
80  if ((in_bits & 0x00000000FFFFFFFFULL) == 0) { ret += 32; in_bits >>= 32; }
81  if ((in_bits & 0x000000000000FFFFULL) == 0) { ret += 16; in_bits >>= 16; }
82  if ((in_bits & 0x00000000000000FFULL) == 0) { ret += 8; in_bits >>= 8; }
83  if ((in_bits & 0x000000000000000FULL) == 0) { ret += 4; in_bits >>= 4; }
84  if ((in_bits & 0x0000000000000003ULL) == 0) { ret += 2; in_bits >>= 2; }
85  if ((in_bits & 0x0000000000000001ULL) == 0) { ret += 1; in_bits >>= 1; }
86  return ret;
87  }
88 #endif
89 
90 #if defined _MSC_VER
92  {
93  unsigned long ret = 0;
94  _BitScanForward(&ret, in_bits);
95  return in_bits ? ret : 32;
96  }
97 
98 #elif __clang__ || defined __GNUC__
100  {
101  return in_bits ? __builtin_ctz(in_bits) : 32;
102  }
103 #else
105  {
106  if (in_bits == 0) return 32;
107  AkUInt32 ret = 0;
108  if ((in_bits & 0x0000FFFFULL) == 0) { ret += 16; in_bits >>= 16; }
109  if ((in_bits & 0x000000FFULL) == 0) { ret += 8; in_bits >>= 8; }
110  if ((in_bits & 0x0000000FULL) == 0) { ret += 4; in_bits >>= 4; }
111  if ((in_bits & 0x00000003ULL) == 0) { ret += 2; in_bits >>= 2; }
112  if ((in_bits & 0x00000001ULL) == 0) { ret += 1; in_bits >>= 1; }
113  return ret;
114  }
115 #endif
116 
117  // AkBitScanReverse returns how many 0s there are after the most-significant-bit is set
118  // (or the length of the param if the value is zero)
119  // e.g. 0b0000`0000`0001`0000 would return 11
120 #if defined _MSC_VER && (defined AK_CPU_X86_64 || defined AK_CPU_ARM_64)
122  {
123  unsigned long ret = 0;
124  _BitScanReverse64(&ret, in_bits);
125  return in_bits ? 63 - ret : 64;
126  }
127 #elif __clang__ || defined __GNUC__
129  {
130  return in_bits ? __builtin_clzll(in_bits) : 64;
131  }
132 #else
134  {
135  if (in_bits == 0) return 64;
136  int ret = 0;
137  if ((in_bits & 0xFFFFFFFF00000000ULL) == 0) { ret += 32; in_bits <<= 32; }
138  if ((in_bits & 0xFFFF000000000000ULL) == 0) { ret += 16; in_bits <<= 16; }
139  if ((in_bits & 0xFF00000000000000ULL) == 0) { ret += 8; in_bits <<= 8; }
140  if ((in_bits & 0xF000000000000000ULL) == 0) { ret += 4; in_bits <<= 4; }
141  if ((in_bits & 0xC000000000000000ULL) == 0) { ret += 2; in_bits <<= 2; }
142  if ((in_bits & 0x8000000000000000ULL) == 0) { ret += 1; in_bits <<= 1; }
143  return ret;
144  }
145 #endif
146 
147 #if defined _MSC_VER
149  {
150  unsigned long ret = 0;
151  _BitScanReverse(&ret, in_bits);
152  return in_bits ? 31 - ret : 32;
153  }
154 #elif __clang__ || defined __GNUC__
156  {
157  return in_bits ? __builtin_clz(in_bits) : 32;
158  }
159 #else
161  {
162  if (in_bits == 0) return 32;
163  int ret = 0;
164  if ((in_bits & 0xFFFF0000ULL) == 0) { ret += 16; in_bits <<= 16; }
165  if ((in_bits & 0xFF000000ULL) == 0) { ret += 8; in_bits <<= 8; }
166  if ((in_bits & 0xF0000000ULL) == 0) { ret += 4; in_bits <<= 4; }
167  if ((in_bits & 0xC0000000ULL) == 0) { ret += 2; in_bits <<= 2; }
168  if ((in_bits & 0x80000000ULL) == 0) { ret += 1; in_bits <<= 1; }
169  return ret;
170  }
171 #endif
172 
173 }
174 #endif // defined(AK_BIT_SCAN_INSTRUCTIONS)
175 
176 /// Utility functions
177 namespace AK
178 {
179  /// Count non-zero bits, e.g. to count # of channels in a channelmask
180  /// \return Number of channels.
182  {
183  return AKPLATFORM::AkPopCount(in_uWord);
184  }
185 
186  /// Computes the next power of two given a value.
187  /// \return next power of two.
189  {
190  in_uValue--;
191  in_uValue |= in_uValue >> 1;
192  in_uValue |= in_uValue >> 2;
193  in_uValue |= in_uValue >> 4;
194  in_uValue |= in_uValue >> 8;
195  in_uValue |= in_uValue >> 16;
196  in_uValue++;
197  return in_uValue;
198  }
199 
201  {
202  return (x << r) | (x >> (32 - r));
203  }
204 
206  {
207  return (x << r) | (x >> (64 - r));
208  }
209 }
Audiokinetic namespace.
AkForceInline AkUInt32 AkBitScanReverse64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:133
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
AkForceInline AkUInt32 ROTL32(AkUInt32 x, AkUInt32 r)
Definition: AkBitFuncs.h:200
AkForceInline AkUInt32 AkPopCount(AkUInt32 in_bits)
Definition: AkBitFuncs.h:52
AkForceInline AkUInt32 AkBitScanForward64(AkUInt64 in_bits)
Definition: AkBitFuncs.h:76
AkForceInline AkUInt32 AkBitScanReverse(AkUInt32 in_bits)
Definition: AkBitFuncs.h:160
AkForceInline AkUInt32 GetNextPowerOfTwo(AkUInt32 in_uValue)
Definition: AkBitFuncs.h:188
AkForceInline AkUInt32 AkBitScanForward(AkUInt32 in_bits)
Definition: AkBitFuncs.h:104
AkForceInline AkUInt32 GetNumNonZeroBits(AkUInt32 in_uWord)
Definition: AkBitFuncs.h:181
uint64_t AkUInt64
Unsigned 64-bit integer.
AkForceInline AkUInt64 ROTL64(AkUInt64 x, AkUInt64 r)
Definition: AkBitFuncs.h:205
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