File: MathAlgorithms.h

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (409 lines) | stat: -rw-r--r-- 12,321 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* mfbt maths algorithms. */

#ifndef mozilla_MathAlgorithms_h
#define mozilla_MathAlgorithms_h

#include "mozilla/Assertions.h"

#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdint>
#include <type_traits>

namespace mozilla {

namespace detail {

template <typename T>
struct AllowDeprecatedAbsFixed : std::false_type {};

template <>
struct AllowDeprecatedAbsFixed<int32_t> : std::true_type {};
template <>
struct AllowDeprecatedAbsFixed<int64_t> : std::true_type {};

template <typename T>
struct AllowDeprecatedAbs : AllowDeprecatedAbsFixed<T> {};

template <>
struct AllowDeprecatedAbs<int> : std::true_type {};
template <>
struct AllowDeprecatedAbs<long> : std::true_type {};

}  // namespace detail

// DO NOT USE DeprecatedAbs.  It exists only until its callers can be converted
// to Abs below, and it will be removed when all callers have been changed.
template <typename T>
inline std::enable_if_t<detail::AllowDeprecatedAbs<T>::value, T> DeprecatedAbs(
    const T aValue) {
  // The absolute value of the smallest possible value of a signed-integer type
  // won't fit in that type (on twos-complement systems -- and we're blithely
  // assuming we're on such systems, for the non-<stdint.h> types listed above),
  // so assert that the input isn't that value.
  //
  // This is the case if: the value is non-negative; or if adding one (giving a
  // value in the range [-maxvalue, 0]), then negating (giving a value in the
  // range [0, maxvalue]), doesn't produce maxvalue (because in twos-complement,
  // (minvalue + 1) == -maxvalue).
  MOZ_ASSERT(aValue >= 0 ||
                 -(aValue + 1) != T((1ULL << (CHAR_BIT * sizeof(T) - 1)) - 1),
             "You can't negate the smallest possible negative integer!");
  return aValue >= 0 ? aValue : -aValue;
}

namespace detail {

template <typename T, typename = void>
struct AbsReturnType;

template <typename T>
struct AbsReturnType<
    T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {
  using Type = std::make_unsigned_t<T>;
};

template <typename T>
struct AbsReturnType<T, std::enable_if_t<std::is_floating_point_v<T>>> {
  using Type = T;
};

}  // namespace detail

template <typename T>
inline constexpr typename detail::AbsReturnType<T>::Type Abs(const T aValue) {
  using ReturnType = typename detail::AbsReturnType<T>::Type;
  return aValue >= 0 ? ReturnType(aValue) : ~ReturnType(aValue) + 1;
}

template <>
inline float Abs<float>(const float aFloat) {
  return std::fabs(aFloat);
}

template <>
inline double Abs<double>(const double aDouble) {
  return std::fabs(aDouble);
}

template <>
inline long double Abs<long double>(const long double aLongDouble) {
  return std::fabs(aLongDouble);
}

}  // namespace mozilla

namespace mozilla {

namespace detail {

// FIXME: use std::count[lr]_zero once we move to C++20

#if defined(__clang__) || defined(__GNUC__)

#  if defined(__clang__)
#    if !__has_builtin(__builtin_ctz) || !__has_builtin(__builtin_clz)
#      error "A clang providing __builtin_c[lt]z is required to build"
#    endif
#  else
// gcc has had __builtin_clz and friends since 3.4: no need to check.
#  endif

constexpr uint_fast8_t CountLeadingZeroes32(uint32_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_clz(aValue));
}

constexpr uint_fast8_t CountTrailingZeroes32(uint32_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_ctz(aValue));
}

constexpr uint_fast8_t CountPopulation32(uint32_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_popcount(aValue));
}

constexpr uint_fast8_t CountPopulation64(uint64_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_popcountll(aValue));
}

constexpr uint_fast8_t CountLeadingZeroes64(uint64_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_clzll(aValue));
}

constexpr uint_fast8_t CountTrailingZeroes64(uint64_t aValue) {
  return static_cast<uint_fast8_t>(__builtin_ctzll(aValue));
}

#else
#  error "Implement these!"
constexpr uint_fast8_t CountLeadingZeroes32(uint32_t aValue) = delete;
constexpr uint_fast8_t CountTrailingZeroes32(uint32_t aValue) = delete;
constexpr uint_fast8_t CountPopulation32(uint32_t aValue) = delete;
constexpr uint_fast8_t CountPopulation64(uint64_t aValue) = delete;
constexpr uint_fast8_t CountLeadingZeroes64(uint64_t aValue) = delete;
constexpr uint_fast8_t CountTrailingZeroes64(uint64_t aValue) = delete;
#endif

}  // namespace detail

/**
 * Compute the number of high-order zero bits in the NON-ZERO number |aValue|.
 * That is, looking at the bitwise representation of the number, with the
 * highest- valued bits at the start, return the number of zeroes before the
 * first one is observed.
 *
 * CountLeadingZeroes32(0xF0FF1000) is 0;
 * CountLeadingZeroes32(0x7F8F0001) is 1;
 * CountLeadingZeroes32(0x3FFF0100) is 2;
 * CountLeadingZeroes32(0x1FF50010) is 3; and so on.
 */
constexpr uint_fast8_t CountLeadingZeroes32(uint32_t aValue) {
  MOZ_ASSERT(aValue != 0);
  return detail::CountLeadingZeroes32(aValue);
}

/**
 * Compute the number of low-order zero bits in the NON-ZERO number |aValue|.
 * That is, looking at the bitwise representation of the number, with the
 * lowest- valued bits at the start, return the number of zeroes before the
 * first one is observed.
 *
 * CountTrailingZeroes32(0x0100FFFF) is 0;
 * CountTrailingZeroes32(0x7000FFFE) is 1;
 * CountTrailingZeroes32(0x0080FFFC) is 2;
 * CountTrailingZeroes32(0x0080FFF8) is 3; and so on.
 */
constexpr uint_fast8_t CountTrailingZeroes32(uint32_t aValue) {
  MOZ_ASSERT(aValue != 0);
  return detail::CountTrailingZeroes32(aValue);
}

/**
 * Compute the number of one bits in the number |aValue|,
 */
constexpr uint_fast8_t CountPopulation32(uint32_t aValue) {
  return detail::CountPopulation32(aValue);
}

/** Analogous to CountPopulation32, but for 64-bit numbers */
constexpr uint_fast8_t CountPopulation64(uint64_t aValue) {
  return detail::CountPopulation64(aValue);
}

/** Analogous to CountLeadingZeroes32, but for 64-bit numbers. */
constexpr uint_fast8_t CountLeadingZeroes64(uint64_t aValue) {
  MOZ_ASSERT(aValue != 0);
  return detail::CountLeadingZeroes64(aValue);
}

/** Analogous to CountTrailingZeroes32, but for 64-bit numbers. */
constexpr uint_fast8_t CountTrailingZeroes64(uint64_t aValue) {
  MOZ_ASSERT(aValue != 0);
  return detail::CountTrailingZeroes64(aValue);
}

namespace detail {

template <typename T, size_t Size = sizeof(T)>
class CeilingLog2;

template <typename T>
class CeilingLog2<T, 4> {
 public:
  static constexpr uint_fast8_t compute(const T aValue) {
    // Check for <= 1 to avoid the == 0 undefined case.
    return aValue <= 1 ? 0u : 32u - CountLeadingZeroes32(aValue - 1);
  }
};

template <typename T>
class CeilingLog2<T, 8> {
 public:
  static constexpr uint_fast8_t compute(const T aValue) {
    // Check for <= 1 to avoid the == 0 undefined case.
    return aValue <= 1 ? 0u : 64u - CountLeadingZeroes64(aValue - 1);
  }
};

}  // namespace detail

/**
 * Compute the log of the least power of 2 greater than or equal to |aValue|.
 *
 * CeilingLog2(0..1) is 0;
 * CeilingLog2(2) is 1;
 * CeilingLog2(3..4) is 2;
 * CeilingLog2(5..8) is 3;
 * CeilingLog2(9..16) is 4; and so on.
 */
template <typename T>
constexpr uint_fast8_t CeilingLog2(const T aValue) {
  return detail::CeilingLog2<T>::compute(aValue);
}

/** A CeilingLog2 variant that accepts only size_t. */
constexpr uint_fast8_t CeilingLog2Size(size_t aValue) {
  return CeilingLog2(aValue);
}

/**
 * Compute the bit position of the most significant bit set in
 * |aValue|. Requires that |aValue| is non-zero.
 */
template <typename T>
constexpr uint_fast8_t FindMostSignificantBit(T aValue) {
  static_assert(sizeof(T) <= 8);
  static_assert(std::is_integral_v<T>);
  MOZ_ASSERT(aValue != 0);
  // This casts to 32-bits
  if constexpr (sizeof(T) <= 4) {
    return 31u - CountLeadingZeroes32(aValue);
  }
  // This doesn't
  if constexpr (sizeof(T) == 8) {
    return 63u - CountLeadingZeroes64(aValue);
  }
}

/**
 * Compute the log of the greatest power of 2 less than or equal to |aValue|.
 *
 * FloorLog2(0..1) is 0;
 * FloorLog2(2..3) is 1;
 * FloorLog2(4..7) is 2;
 * FloorLog2(8..15) is 3; and so on.
 */
template <typename T>
constexpr uint_fast8_t FloorLog2(const T aValue) {
  return FindMostSignificantBit(aValue | 1);
}

/** A FloorLog2 variant that accepts only size_t. */
constexpr uint_fast8_t FloorLog2Size(size_t aValue) {
  return FloorLog2(aValue);
}

/*
 * Compute the smallest power of 2 greater than or equal to |x|.  |x| must not
 * be so great that the computed value would overflow |size_t|.
 */
constexpr size_t RoundUpPow2(size_t aValue) {
  MOZ_ASSERT(aValue <= (size_t(1) << (sizeof(size_t) * CHAR_BIT - 1)),
             "can't round up -- will overflow!");
  return size_t(1) << CeilingLog2(aValue);
}

/**
 * Rotates the bits of the given value left by the amount of the shift width.
 */
template <typename T>
MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW constexpr T RotateLeft(const T aValue,
                                                         uint_fast8_t aShift) {
  static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values");

  MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
  MOZ_ASSERT(aShift > 0,
             "Rotation by value length is undefined behavior, but compilers "
             "do not currently fold a test into the rotate instruction. "
             "Please remove this restriction when compilers optimize the "
             "zero case (http://blog.regehr.org/archives/1063).");

  return (aValue << aShift) | (aValue >> (sizeof(T) * CHAR_BIT - aShift));
}

/**
 * Rotates the bits of the given value right by the amount of the shift width.
 */
template <typename T>
MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW constexpr T RotateRight(const T aValue,
                                                          uint_fast8_t aShift) {
  static_assert(std::is_unsigned_v<T>, "Rotates require unsigned values");

  MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
  MOZ_ASSERT(aShift > 0,
             "Rotation by value length is undefined behavior, but compilers "
             "do not currently fold a test into the rotate instruction. "
             "Please remove this restriction when compilers optimize the "
             "zero case (http://blog.regehr.org/archives/1063).");

  return (aValue >> aShift) | (aValue << (sizeof(T) * CHAR_BIT - aShift));
}

/**
 * Returns true if |x| is a power of two.
 * Zero is not an integer power of two. (-Inf is not an integer)
 */
template <typename T>
constexpr bool IsPowerOfTwo(T x) {
  static_assert(std::is_unsigned_v<T>, "IsPowerOfTwo requires unsigned values");
  return x && (x & (x - 1)) == 0;
}

template <typename T>
constexpr uint_fast8_t CountTrailingZeroes(T aValue) {
  static_assert(sizeof(T) <= 8);
  static_assert(std::is_integral_v<T>);
  // This casts to 32-bits
  if constexpr (sizeof(T) <= 4) {
    return CountTrailingZeroes32(aValue);
  }
  // This doesn't
  if constexpr (sizeof(T) == 8) {
    return CountTrailingZeroes64(aValue);
  }
}

// Greatest Common Divisor, from
// https://en.wikipedia.org/wiki/Binary_GCD_algorithm#Implementation
template <typename T>
MOZ_ALWAYS_INLINE T GCD(T aA, T aB) {
  static_assert(std::is_integral_v<T>);

  MOZ_ASSERT(aA >= 0);
  MOZ_ASSERT(aB >= 0);

  if (aA == 0) {
    return aB;
  }
  if (aB == 0) {
    return aA;
  }

  T az = CountTrailingZeroes(aA);
  T bz = CountTrailingZeroes(aB);
  T shift = std::min<T>(az, bz);
  aA >>= az;
  aB >>= bz;

  while (aA != 0) {
    if constexpr (!std::is_signed_v<T>) {
      if (aA < aB) {
        std::swap(aA, aB);
      }
    }
    T diff = aA - aB;
    if constexpr (std::is_signed_v<T>) {
      aB = std::min<T>(aA, aB);
    }
    if constexpr (std::is_signed_v<T>) {
      aA = std::abs(diff);
    } else {
      aA = diff;
    }
    if (aA) {
      aA >>= CountTrailingZeroes(aA);
    }
  }

  return aB << shift;
}

} /* namespace mozilla */

#endif /* mozilla_MathAlgorithms_h */