blob: 26cab2ff0936ac8e79de2308051f1286f79ba54e [file] [log] [blame]
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_utils.h"
18
19#include "base/logging.h"
20
21void CalculateMagicAndShiftForDivRem(int64_t divisor, bool is_long,
22 int64_t* magic, int* shift) {
23 // It does not make sense to calculate magic and shift for zero divisor.
24 DCHECK_NE(divisor, 0);
25
26 /* According to implementation from H.S.Warren's "Hacker's Delight" (Addison Wesley, 2002)
27 * Chapter 10 and T,Grablund, P.L.Montogomery's "Division by Invariant Integers Using
28 * Multiplication" (PLDI 1994).
29 * The magic number M and shift S can be calculated in the following way:
30 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
31 * where divisor(d) >= 2.
32 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
33 * where divisor(d) <= -2.
34 * Thus nc can be calculated like:
35 * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long
36 * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^31 for int or 2^63 for long
37 *
38 * So the shift p is the smallest p satisfying
39 * 2^p > nc * (d - 2^p % d), where d >= 2
40 * 2^p > nc * (d + 2^p % d), where d <= -2.
41 *
42 * The magic number M is calcuated by
43 * M = (2^p + d - 2^p % d) / d, where d >= 2
44 * M = (2^p - d - 2^p % d) / d, where d <= -2.
45 *
46 * Notice that p is always bigger than or equal to 32 (resp. 64), so we just return 32-p
47 * (resp. 64 - p) as the shift number S.
48 */
49
50 int64_t p = is_long ? 63 : 31;
51 const uint64_t exp = is_long ? (UINT64_C(1) << 63) : (UINT32_C(1) << 31);
52
53 // Initialize the computations.
54 uint64_t abs_d = (divisor >= 0) ? divisor : -divisor;
55 uint64_t tmp = exp + (is_long ? static_cast<uint64_t>(divisor) >> 63 :
56 static_cast<uint32_t>(divisor) >> 31);
57 uint64_t abs_nc = tmp - 1 - tmp % abs_d;
58 uint64_t quotient1 = exp / abs_nc;
59 uint64_t remainder1 = exp % abs_nc;
60 uint64_t quotient2 = exp / abs_d;
61 uint64_t remainder2 = exp % abs_d;
62
63 /*
64 * To avoid handling both positive and negative divisor, "Hacker's Delight"
65 * introduces a method to handle these 2 cases together to avoid duplication.
66 */
67 uint64_t delta;
68 do {
69 p++;
70 quotient1 = 2 * quotient1;
71 remainder1 = 2 * remainder1;
72 if (remainder1 >= abs_nc) {
73 quotient1++;
74 remainder1 = remainder1 - abs_nc;
75 }
76 quotient2 = 2 * quotient2;
77 remainder2 = 2 * remainder2;
78 if (remainder2 >= abs_d) {
79 quotient2++;
80 remainder2 = remainder2 - abs_d;
81 }
82 delta = abs_d - remainder2;
83 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
84
85 *magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
86
87 if (!is_long) {
88 *magic = static_cast<int>(*magic);
89 }
90
91 *shift = is_long ? p - 64 : p - 32;
92}
93