v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
division-by-constant.h
Go to the documentation of this file.
1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_DIVISION_BY_CONSTANT_H_
6#define V8_BASE_DIVISION_BY_CONSTANT_H_
7
8#include <stdint.h>
9
10#include <concepts>
11#include <tuple>
12#include <type_traits>
13
16
17namespace v8 {
18namespace base {
19
20// ----------------------------------------------------------------------------
21
22// The magic numbers for division via multiplication, see Warren's "Hacker's
23// Delight", chapter 10.
24template <class T>
25struct EXPORT_TEMPLATE_DECLARE(V8_BASE_EXPORT) MagicNumbersForDivision {
26 static_assert(std::is_integral_v<T>);
27 MagicNumbersForDivision(T m, unsigned s, bool a)
28 : multiplier(m), shift(s), add(a) {}
29 bool operator==(const MagicNumbersForDivision& rhs) const {
30 return multiplier == rhs.multiplier && shift == rhs.shift && add == rhs.add;
31 }
32
33 T multiplier;
34 unsigned shift;
35 bool add;
36};
37
38// Calculate the multiplier and shift for signed division via multiplication.
39// The divisor must not be -1, 0 or 1 when interpreted as a signed value.
40template <typename T>
41 requires std::unsigned_integral<T>
43 MagicNumbersForDivision<T> SignedDivisionByConstant(T d);
44
45template <typename T>
46 requires std::signed_integral<T>
47MagicNumbersForDivision<T> SignedDivisionByConstant(T d) {
48 using Unsigned = std::make_unsigned_t<T>;
49 MagicNumbersForDivision<Unsigned> magic =
50 SignedDivisionByConstant(static_cast<Unsigned>(d));
51 return {static_cast<T>(magic.multiplier), magic.shift, magic.add};
52}
53
54// Calculate the multiplier and shift for unsigned division via multiplication,
55// see Warren's "Hacker's Delight", chapter 10. The divisor must not be 0 and
56// leading_zeros can be used to speed up the calculation if the given number of
57// upper bits of the dividend value are known to be zero.
58template <class T>
60MagicNumbersForDivision<T> UnsignedDivisionByConstant(
61 T d, unsigned leading_zeros = 0);
62
63// Explicit instantiation declarations.
65 MagicNumbersForDivision<uint32_t>;
66extern template struct EXPORT_TEMPLATE_DECLARE(V8_BASE_EXPORT)
67 MagicNumbersForDivision<uint64_t>;
68
70 MagicNumbersForDivision<uint32_t> SignedDivisionByConstant(uint32_t d);
72 MagicNumbersForDivision<uint64_t> SignedDivisionByConstant(uint64_t d);
73
75 MagicNumbersForDivision<uint32_t> UnsignedDivisionByConstant(
76 uint32_t d, unsigned leading_zeros);
78 MagicNumbersForDivision<uint64_t> UnsignedDivisionByConstant(
79 uint64_t d, unsigned leading_zeros);
80
81} // namespace base
82} // namespace v8
83
84#endif // V8_BASE_DIVISION_BY_CONSTANT_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
#define EXPORT_TEMPLATE_DECLARE(export)
int m
Definition mul-fft.cc:294
MagicNumbersForDivision< T > UnsignedDivisionByConstant(T d, unsigned leading_zeros)
MagicNumbersForDivision< T > SignedDivisionByConstant(T d)
bool operator==(PointerWithPayload< PointerType, PayloadType, NumPayloadBits > lhs, PointerWithPayload< PointerType, PayloadType, NumPayloadBits > rhs)