Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Mark Mendell | b8b9769 | 2015-05-22 16:58:19 -0400 | [diff] [blame] | 18 | #include "nodes.h" |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 19 | |
| 20 | #include "base/logging.h" |
| 21 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 24 | void CalculateMagicAndShiftForDivRem(int64_t divisor, bool is_long, |
| 25 | int64_t* magic, int* shift) { |
| 26 | // It does not make sense to calculate magic and shift for zero divisor. |
| 27 | DCHECK_NE(divisor, 0); |
| 28 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 29 | /* Implementation according to H.S.Warren's "Hacker's Delight" (Addison Wesley, 2002) |
| 30 | * Chapter 10 and T.Grablund, P.L.Montogomery's "Division by Invariant Integers Using |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 31 | * Multiplication" (PLDI 1994). |
| 32 | * The magic number M and shift S can be calculated in the following way: |
| 33 | * Let nc be the most positive value of numerator(n) such that nc = kd - 1, |
| 34 | * where divisor(d) >= 2. |
| 35 | * Let nc be the most negative value of numerator(n) such that nc = kd + 1, |
| 36 | * where divisor(d) <= -2. |
| 37 | * Thus nc can be calculated like: |
| 38 | * nc = exp + exp % d - 1, where d >= 2 and exp = 2^31 for int or 2^63 for long |
| 39 | * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^31 for int or 2^63 for long |
| 40 | * |
| 41 | * So the shift p is the smallest p satisfying |
| 42 | * 2^p > nc * (d - 2^p % d), where d >= 2 |
| 43 | * 2^p > nc * (d + 2^p % d), where d <= -2. |
| 44 | * |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 45 | * The magic number M is calculated by |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 46 | * M = (2^p + d - 2^p % d) / d, where d >= 2 |
| 47 | * M = (2^p - d - 2^p % d) / d, where d <= -2. |
| 48 | * |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 49 | * Notice that p is always bigger than or equal to 32 (resp. 64), so we just return 32 - p |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 50 | * (resp. 64 - p) as the shift number S. |
| 51 | */ |
| 52 | |
| 53 | int64_t p = is_long ? 63 : 31; |
| 54 | const uint64_t exp = is_long ? (UINT64_C(1) << 63) : (UINT32_C(1) << 31); |
| 55 | |
| 56 | // Initialize the computations. |
| 57 | uint64_t abs_d = (divisor >= 0) ? divisor : -divisor; |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 58 | uint64_t sign_bit = is_long ? static_cast<uint64_t>(divisor) >> 63 : |
| 59 | static_cast<uint32_t>(divisor) >> 31; |
| 60 | uint64_t tmp = exp + sign_bit; |
| 61 | uint64_t abs_nc = tmp - 1 - (tmp % abs_d); |
Guillaume Sanchez | 0f88e87 | 2015-03-30 17:55:45 +0100 | [diff] [blame] | 62 | uint64_t quotient1 = exp / abs_nc; |
| 63 | uint64_t remainder1 = exp % abs_nc; |
| 64 | uint64_t quotient2 = exp / abs_d; |
| 65 | uint64_t remainder2 = exp % abs_d; |
| 66 | |
| 67 | /* |
| 68 | * To avoid handling both positive and negative divisor, "Hacker's Delight" |
| 69 | * introduces a method to handle these 2 cases together to avoid duplication. |
| 70 | */ |
| 71 | uint64_t delta; |
| 72 | do { |
| 73 | p++; |
| 74 | quotient1 = 2 * quotient1; |
| 75 | remainder1 = 2 * remainder1; |
| 76 | if (remainder1 >= abs_nc) { |
| 77 | quotient1++; |
| 78 | remainder1 = remainder1 - abs_nc; |
| 79 | } |
| 80 | quotient2 = 2 * quotient2; |
| 81 | remainder2 = 2 * remainder2; |
| 82 | if (remainder2 >= abs_d) { |
| 83 | quotient2++; |
| 84 | remainder2 = remainder2 - abs_d; |
| 85 | } |
| 86 | delta = abs_d - remainder2; |
| 87 | } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0)); |
| 88 | |
| 89 | *magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1); |
| 90 | |
| 91 | if (!is_long) { |
| 92 | *magic = static_cast<int>(*magic); |
| 93 | } |
| 94 | |
| 95 | *shift = is_long ? p - 64 : p - 32; |
| 96 | } |
| 97 | |
David Brazdil | 0debae7 | 2015-11-12 18:37:00 +0000 | [diff] [blame] | 98 | bool IsBooleanValueOrMaterializedCondition(HInstruction* cond_input) { |
David Brazdil | b3e773e | 2016-01-26 11:28:37 +0000 | [diff] [blame] | 99 | return !cond_input->IsCondition() || !cond_input->IsEmittedAtUseSite(); |
Mark Mendell | b8b9769 | 2015-05-22 16:58:19 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Guillaume Sanchez | b19930c | 2015-04-09 21:12:15 +0100 | [diff] [blame] | 102 | } // namespace art |