mfbt/MathAlgorithms.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* mfbt maths algorithms. */
michael@0 8
michael@0 9 #ifndef mozilla_MathAlgorithms_h
michael@0 10 #define mozilla_MathAlgorithms_h
michael@0 11
michael@0 12 #include "mozilla/Assertions.h"
michael@0 13 #include "mozilla/TypeTraits.h"
michael@0 14
michael@0 15 #include <cmath>
michael@0 16 #include <limits.h>
michael@0 17 #include <stdint.h>
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20
michael@0 21 // Greatest Common Divisor
michael@0 22 template<typename IntegerType>
michael@0 23 MOZ_ALWAYS_INLINE IntegerType
michael@0 24 EuclidGCD(IntegerType a, IntegerType b)
michael@0 25 {
michael@0 26 // Euclid's algorithm; O(N) in the worst case. (There are better
michael@0 27 // ways, but we don't need them for the current use of this algo.)
michael@0 28 MOZ_ASSERT(a > 0);
michael@0 29 MOZ_ASSERT(b > 0);
michael@0 30
michael@0 31 while (a != b) {
michael@0 32 if (a > b) {
michael@0 33 a = a - b;
michael@0 34 } else {
michael@0 35 b = b - a;
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 return a;
michael@0 40 }
michael@0 41
michael@0 42 // Least Common Multiple
michael@0 43 template<typename IntegerType>
michael@0 44 MOZ_ALWAYS_INLINE IntegerType
michael@0 45 EuclidLCM(IntegerType a, IntegerType b)
michael@0 46 {
michael@0 47 // Divide first to reduce overflow risk.
michael@0 48 return (a / EuclidGCD(a, b)) * b;
michael@0 49 }
michael@0 50
michael@0 51 namespace detail {
michael@0 52
michael@0 53 template<typename T>
michael@0 54 struct AllowDeprecatedAbsFixed : FalseType {};
michael@0 55
michael@0 56 template<> struct AllowDeprecatedAbsFixed<int32_t> : TrueType {};
michael@0 57 template<> struct AllowDeprecatedAbsFixed<int64_t> : TrueType {};
michael@0 58
michael@0 59 template<typename T>
michael@0 60 struct AllowDeprecatedAbs : AllowDeprecatedAbsFixed<T> {};
michael@0 61
michael@0 62 template<> struct AllowDeprecatedAbs<int> : TrueType {};
michael@0 63 template<> struct AllowDeprecatedAbs<long> : TrueType {};
michael@0 64
michael@0 65 } // namespace detail
michael@0 66
michael@0 67 // DO NOT USE DeprecatedAbs. It exists only until its callers can be converted
michael@0 68 // to Abs below, and it will be removed when all callers have been changed.
michael@0 69 template<typename T>
michael@0 70 inline typename mozilla::EnableIf<detail::AllowDeprecatedAbs<T>::value, T>::Type
michael@0 71 DeprecatedAbs(const T t)
michael@0 72 {
michael@0 73 // The absolute value of the smallest possible value of a signed-integer type
michael@0 74 // won't fit in that type (on twos-complement systems -- and we're blithely
michael@0 75 // assuming we're on such systems, for the non-<stdint.h> types listed above),
michael@0 76 // so assert that the input isn't that value.
michael@0 77 //
michael@0 78 // This is the case if: the value is non-negative; or if adding one (giving a
michael@0 79 // value in the range [-maxvalue, 0]), then negating (giving a value in the
michael@0 80 // range [0, maxvalue]), doesn't produce maxvalue (because in twos-complement,
michael@0 81 // (minvalue + 1) == -maxvalue).
michael@0 82 MOZ_ASSERT(t >= 0 ||
michael@0 83 -(t + 1) != T((1ULL << (CHAR_BIT * sizeof(T) - 1)) - 1),
michael@0 84 "You can't negate the smallest possible negative integer!");
michael@0 85 return t >= 0 ? t : -t;
michael@0 86 }
michael@0 87
michael@0 88 namespace detail {
michael@0 89
michael@0 90 // For now mozilla::Abs only takes intN_T, the signed natural types, and
michael@0 91 // float/double/long double. Feel free to add overloads for other standard,
michael@0 92 // signed types if you need them.
michael@0 93
michael@0 94 template<typename T>
michael@0 95 struct AbsReturnTypeFixed;
michael@0 96
michael@0 97 template<> struct AbsReturnTypeFixed<int8_t> { typedef uint8_t Type; };
michael@0 98 template<> struct AbsReturnTypeFixed<int16_t> { typedef uint16_t Type; };
michael@0 99 template<> struct AbsReturnTypeFixed<int32_t> { typedef uint32_t Type; };
michael@0 100 template<> struct AbsReturnTypeFixed<int64_t> { typedef uint64_t Type; };
michael@0 101
michael@0 102 template<typename T>
michael@0 103 struct AbsReturnType : AbsReturnTypeFixed<T> {};
michael@0 104
michael@0 105 template<> struct AbsReturnType<char> : EnableIf<char(-1) < char(0), unsigned char> {};
michael@0 106 template<> struct AbsReturnType<signed char> { typedef unsigned char Type; };
michael@0 107 template<> struct AbsReturnType<short> { typedef unsigned short Type; };
michael@0 108 template<> struct AbsReturnType<int> { typedef unsigned int Type; };
michael@0 109 template<> struct AbsReturnType<long> { typedef unsigned long Type; };
michael@0 110 template<> struct AbsReturnType<long long> { typedef unsigned long long Type; };
michael@0 111 template<> struct AbsReturnType<float> { typedef float Type; };
michael@0 112 template<> struct AbsReturnType<double> { typedef double Type; };
michael@0 113 template<> struct AbsReturnType<long double> { typedef long double Type; };
michael@0 114
michael@0 115 } // namespace detail
michael@0 116
michael@0 117 template<typename T>
michael@0 118 inline typename detail::AbsReturnType<T>::Type
michael@0 119 Abs(const T t)
michael@0 120 {
michael@0 121 typedef typename detail::AbsReturnType<T>::Type ReturnType;
michael@0 122 return t >= 0 ? ReturnType(t) : ~ReturnType(t) + 1;
michael@0 123 }
michael@0 124
michael@0 125 template<>
michael@0 126 inline float
michael@0 127 Abs<float>(const float f)
michael@0 128 {
michael@0 129 return std::fabs(f);
michael@0 130 }
michael@0 131
michael@0 132 template<>
michael@0 133 inline double
michael@0 134 Abs<double>(const double d)
michael@0 135 {
michael@0 136 return std::fabs(d);
michael@0 137 }
michael@0 138
michael@0 139 template<>
michael@0 140 inline long double
michael@0 141 Abs<long double>(const long double d)
michael@0 142 {
michael@0 143 return std::fabs(d);
michael@0 144 }
michael@0 145
michael@0 146 } // namespace mozilla
michael@0 147
michael@0 148 #if defined(_WIN32) && (_MSC_VER >= 1300) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
michael@0 149 # define MOZ_BITSCAN_WINDOWS
michael@0 150
michael@0 151 extern "C" {
michael@0 152 unsigned char _BitScanForward(unsigned long* Index, unsigned long mask);
michael@0 153 unsigned char _BitScanReverse(unsigned long* Index, unsigned long mask);
michael@0 154 # pragma intrinsic(_BitScanForward, _BitScanReverse)
michael@0 155
michael@0 156 # if defined(_M_AMD64) || defined(_M_X64)
michael@0 157 # define MOZ_BITSCAN_WINDOWS64
michael@0 158 unsigned char _BitScanForward64(unsigned long* index, unsigned __int64 mask);
michael@0 159 unsigned char _BitScanReverse64(unsigned long* index, unsigned __int64 mask);
michael@0 160 # pragma intrinsic(_BitScanForward64, _BitScanReverse64)
michael@0 161 # endif
michael@0 162 } // extern "C"
michael@0 163
michael@0 164 #endif
michael@0 165
michael@0 166 namespace mozilla {
michael@0 167
michael@0 168 namespace detail {
michael@0 169
michael@0 170 #if defined(MOZ_BITSCAN_WINDOWS)
michael@0 171
michael@0 172 inline uint_fast8_t
michael@0 173 CountLeadingZeroes32(uint32_t u)
michael@0 174 {
michael@0 175 unsigned long index;
michael@0 176 _BitScanReverse(&index, static_cast<unsigned long>(u));
michael@0 177 return uint_fast8_t(31 - index);
michael@0 178 }
michael@0 179
michael@0 180
michael@0 181 inline uint_fast8_t
michael@0 182 CountTrailingZeroes32(uint32_t u)
michael@0 183 {
michael@0 184 unsigned long index;
michael@0 185 _BitScanForward(&index, static_cast<unsigned long>(u));
michael@0 186 return uint_fast8_t(index);
michael@0 187 }
michael@0 188
michael@0 189 inline uint_fast8_t
michael@0 190 CountPopulation32(uint32_t u)
michael@0 191 {
michael@0 192 uint32_t x = u - ((u >> 1) & 0x55555555);
michael@0 193 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
michael@0 194 return (((x + (x >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
michael@0 195 }
michael@0 196
michael@0 197 inline uint_fast8_t
michael@0 198 CountLeadingZeroes64(uint64_t u)
michael@0 199 {
michael@0 200 # if defined(MOZ_BITSCAN_WINDOWS64)
michael@0 201 unsigned long index;
michael@0 202 _BitScanReverse64(&index, static_cast<unsigned __int64>(u));
michael@0 203 return uint_fast8_t(63 - index);
michael@0 204 # else
michael@0 205 uint32_t hi = uint32_t(u >> 32);
michael@0 206 if (hi != 0)
michael@0 207 return CountLeadingZeroes32(hi);
michael@0 208 return 32 + CountLeadingZeroes32(uint32_t(u));
michael@0 209 # endif
michael@0 210 }
michael@0 211
michael@0 212 inline uint_fast8_t
michael@0 213 CountTrailingZeroes64(uint64_t u)
michael@0 214 {
michael@0 215 # if defined(MOZ_BITSCAN_WINDOWS64)
michael@0 216 unsigned long index;
michael@0 217 _BitScanForward64(&index, static_cast<unsigned __int64>(u));
michael@0 218 return uint_fast8_t(index);
michael@0 219 # else
michael@0 220 uint32_t lo = uint32_t(u);
michael@0 221 if (lo != 0)
michael@0 222 return CountTrailingZeroes32(lo);
michael@0 223 return 32 + CountTrailingZeroes32(uint32_t(u >> 32));
michael@0 224 # endif
michael@0 225 }
michael@0 226
michael@0 227 # ifdef MOZ_HAVE_BITSCAN64
michael@0 228 # undef MOZ_HAVE_BITSCAN64
michael@0 229 # endif
michael@0 230
michael@0 231 #elif defined(__clang__) || defined(__GNUC__)
michael@0 232
michael@0 233 # if defined(__clang__)
michael@0 234 # if !__has_builtin(__builtin_ctz) || !__has_builtin(__builtin_clz)
michael@0 235 # error "A clang providing __builtin_c[lt]z is required to build"
michael@0 236 # endif
michael@0 237 # else
michael@0 238 // gcc has had __builtin_clz and friends since 3.4: no need to check.
michael@0 239 # endif
michael@0 240
michael@0 241 inline uint_fast8_t
michael@0 242 CountLeadingZeroes32(uint32_t u)
michael@0 243 {
michael@0 244 return __builtin_clz(u);
michael@0 245 }
michael@0 246
michael@0 247 inline uint_fast8_t
michael@0 248 CountTrailingZeroes32(uint32_t u)
michael@0 249 {
michael@0 250 return __builtin_ctz(u);
michael@0 251 }
michael@0 252
michael@0 253 inline uint_fast8_t
michael@0 254 CountPopulation32(uint32_t u)
michael@0 255 {
michael@0 256 return __builtin_popcount(u);
michael@0 257 }
michael@0 258
michael@0 259 inline uint_fast8_t
michael@0 260 CountLeadingZeroes64(uint64_t u)
michael@0 261 {
michael@0 262 return __builtin_clzll(u);
michael@0 263 }
michael@0 264
michael@0 265 inline uint_fast8_t
michael@0 266 CountTrailingZeroes64(uint64_t u)
michael@0 267 {
michael@0 268 return __builtin_ctzll(u);
michael@0 269 }
michael@0 270
michael@0 271 #else
michael@0 272 # error "Implement these!"
michael@0 273 inline uint_fast8_t CountLeadingZeroes32(uint32_t u) MOZ_DELETE;
michael@0 274 inline uint_fast8_t CountTrailingZeroes32(uint32_t u) MOZ_DELETE;
michael@0 275 inline uint_fast8_t CountPopulation32(uint32_t u) MOZ_DELETE;
michael@0 276 inline uint_fast8_t CountLeadingZeroes64(uint64_t u) MOZ_DELETE;
michael@0 277 inline uint_fast8_t CountTrailingZeroes64(uint64_t u) MOZ_DELETE;
michael@0 278 #endif
michael@0 279
michael@0 280 } // namespace detail
michael@0 281
michael@0 282 /**
michael@0 283 * Compute the number of high-order zero bits in the NON-ZERO number |u|. That
michael@0 284 * is, looking at the bitwise representation of the number, with the highest-
michael@0 285 * valued bits at the start, return the number of zeroes before the first one
michael@0 286 * is observed.
michael@0 287 *
michael@0 288 * CountLeadingZeroes32(0xF0FF1000) is 0;
michael@0 289 * CountLeadingZeroes32(0x7F8F0001) is 1;
michael@0 290 * CountLeadingZeroes32(0x3FFF0100) is 2;
michael@0 291 * CountLeadingZeroes32(0x1FF50010) is 3; and so on.
michael@0 292 */
michael@0 293 inline uint_fast8_t
michael@0 294 CountLeadingZeroes32(uint32_t u)
michael@0 295 {
michael@0 296 MOZ_ASSERT(u != 0);
michael@0 297 return detail::CountLeadingZeroes32(u);
michael@0 298 }
michael@0 299
michael@0 300 /**
michael@0 301 * Compute the number of low-order zero bits in the NON-ZERO number |u|. That
michael@0 302 * is, looking at the bitwise representation of the number, with the lowest-
michael@0 303 * valued bits at the start, return the number of zeroes before the first one
michael@0 304 * is observed.
michael@0 305 *
michael@0 306 * CountTrailingZeroes32(0x0100FFFF) is 0;
michael@0 307 * CountTrailingZeroes32(0x7000FFFE) is 1;
michael@0 308 * CountTrailingZeroes32(0x0080FFFC) is 2;
michael@0 309 * CountTrailingZeroes32(0x0080FFF8) is 3; and so on.
michael@0 310 */
michael@0 311 inline uint_fast8_t
michael@0 312 CountTrailingZeroes32(uint32_t u)
michael@0 313 {
michael@0 314 MOZ_ASSERT(u != 0);
michael@0 315 return detail::CountTrailingZeroes32(u);
michael@0 316 }
michael@0 317
michael@0 318 /**
michael@0 319 * Compute the number of one bits in the number |u|,
michael@0 320 */
michael@0 321 inline uint_fast8_t
michael@0 322 CountPopulation32(uint32_t u)
michael@0 323 {
michael@0 324 return detail::CountPopulation32(u);
michael@0 325 }
michael@0 326
michael@0 327 /** Analogous to CountLeadingZeroes32, but for 64-bit numbers. */
michael@0 328 inline uint_fast8_t
michael@0 329 CountLeadingZeroes64(uint64_t u)
michael@0 330 {
michael@0 331 MOZ_ASSERT(u != 0);
michael@0 332 return detail::CountLeadingZeroes64(u);
michael@0 333 }
michael@0 334
michael@0 335 /** Analogous to CountTrailingZeroes32, but for 64-bit numbers. */
michael@0 336 inline uint_fast8_t
michael@0 337 CountTrailingZeroes64(uint64_t u)
michael@0 338 {
michael@0 339 MOZ_ASSERT(u != 0);
michael@0 340 return detail::CountTrailingZeroes64(u);
michael@0 341 }
michael@0 342
michael@0 343 namespace detail {
michael@0 344
michael@0 345 template<typename T, size_t Size = sizeof(T)>
michael@0 346 class CeilingLog2;
michael@0 347
michael@0 348 template<typename T>
michael@0 349 class CeilingLog2<T, 4>
michael@0 350 {
michael@0 351 public:
michael@0 352 static uint_fast8_t compute(const T t) {
michael@0 353 // Check for <= 1 to avoid the == 0 undefined case.
michael@0 354 return t <= 1 ? 0 : 32 - CountLeadingZeroes32(t - 1);
michael@0 355 }
michael@0 356 };
michael@0 357
michael@0 358 template<typename T>
michael@0 359 class CeilingLog2<T, 8>
michael@0 360 {
michael@0 361 public:
michael@0 362 static uint_fast8_t compute(const T t) {
michael@0 363 // Check for <= 1 to avoid the == 0 undefined case.
michael@0 364 return t <= 1 ? 0 : 64 - CountLeadingZeroes64(t - 1);
michael@0 365 }
michael@0 366 };
michael@0 367
michael@0 368 } // namespace detail
michael@0 369
michael@0 370 /**
michael@0 371 * Compute the log of the least power of 2 greater than or equal to |t|.
michael@0 372 *
michael@0 373 * CeilingLog2(0..1) is 0;
michael@0 374 * CeilingLog2(2) is 1;
michael@0 375 * CeilingLog2(3..4) is 2;
michael@0 376 * CeilingLog2(5..8) is 3;
michael@0 377 * CeilingLog2(9..16) is 4; and so on.
michael@0 378 */
michael@0 379 template<typename T>
michael@0 380 inline uint_fast8_t
michael@0 381 CeilingLog2(const T t)
michael@0 382 {
michael@0 383 return detail::CeilingLog2<T>::compute(t);
michael@0 384 }
michael@0 385
michael@0 386 /** A CeilingLog2 variant that accepts only size_t. */
michael@0 387 inline uint_fast8_t
michael@0 388 CeilingLog2Size(size_t n)
michael@0 389 {
michael@0 390 return CeilingLog2(n);
michael@0 391 }
michael@0 392
michael@0 393 namespace detail {
michael@0 394
michael@0 395 template<typename T, size_t Size = sizeof(T)>
michael@0 396 class FloorLog2;
michael@0 397
michael@0 398 template<typename T>
michael@0 399 class FloorLog2<T, 4>
michael@0 400 {
michael@0 401 public:
michael@0 402 static uint_fast8_t compute(const T t) {
michael@0 403 return 31 - CountLeadingZeroes32(t | 1);
michael@0 404 }
michael@0 405 };
michael@0 406
michael@0 407 template<typename T>
michael@0 408 class FloorLog2<T, 8>
michael@0 409 {
michael@0 410 public:
michael@0 411 static uint_fast8_t compute(const T t) {
michael@0 412 return 63 - CountLeadingZeroes64(t | 1);
michael@0 413 }
michael@0 414 };
michael@0 415
michael@0 416 } // namespace detail
michael@0 417
michael@0 418 /**
michael@0 419 * Compute the log of the greatest power of 2 less than or equal to |t|.
michael@0 420 *
michael@0 421 * FloorLog2(0..1) is 0;
michael@0 422 * FloorLog2(2..3) is 1;
michael@0 423 * FloorLog2(4..7) is 2;
michael@0 424 * FloorLog2(8..15) is 3; and so on.
michael@0 425 */
michael@0 426 template<typename T>
michael@0 427 inline uint_fast8_t
michael@0 428 FloorLog2(const T t)
michael@0 429 {
michael@0 430 return detail::FloorLog2<T>::compute(t);
michael@0 431 }
michael@0 432
michael@0 433 /** A FloorLog2 variant that accepts only size_t. */
michael@0 434 inline uint_fast8_t
michael@0 435 FloorLog2Size(size_t n)
michael@0 436 {
michael@0 437 return FloorLog2(n);
michael@0 438 }
michael@0 439
michael@0 440 /*
michael@0 441 * Compute the smallest power of 2 greater than or equal to |x|. |x| must not
michael@0 442 * be so great that the computed value would overflow |size_t|.
michael@0 443 */
michael@0 444 inline size_t
michael@0 445 RoundUpPow2(size_t x)
michael@0 446 {
michael@0 447 MOZ_ASSERT(x <= (size_t(1) << (sizeof(size_t) * CHAR_BIT - 1)),
michael@0 448 "can't round up -- will overflow!");
michael@0 449 return size_t(1) << CeilingLog2(x);
michael@0 450 }
michael@0 451
michael@0 452 /**
michael@0 453 * Rotates the bits of the given value left by the amount of the shift width.
michael@0 454 */
michael@0 455 template<typename T>
michael@0 456 inline T
michael@0 457 RotateLeft(const T t, uint_fast8_t shift)
michael@0 458 {
michael@0 459 MOZ_ASSERT(shift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
michael@0 460 static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
michael@0 461 return (t << shift) | (t >> (sizeof(T) * CHAR_BIT - shift));
michael@0 462 }
michael@0 463
michael@0 464 /**
michael@0 465 * Rotates the bits of the given value right by the amount of the shift width.
michael@0 466 */
michael@0 467 template<typename T>
michael@0 468 inline T
michael@0 469 RotateRight(const T t, uint_fast8_t shift)
michael@0 470 {
michael@0 471 MOZ_ASSERT(shift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
michael@0 472 static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
michael@0 473 return (t >> shift) | (t << (sizeof(T) * CHAR_BIT - shift));
michael@0 474 }
michael@0 475
michael@0 476 } /* namespace mozilla */
michael@0 477
michael@0 478 #endif /* mozilla_MathAlgorithms_h */

mercurial