LCOV - code coverage report
Current view: top level - src/base - bits.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 27 35 77.1 %
Date: 2017-04-26 Functions: 7 10 70.0 %

          Line data    Source code
       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             : #include "src/base/bits.h"
       6             : 
       7             : #include <limits>
       8             : 
       9             : #include "src/base/logging.h"
      10             : #include "src/base/safe_math.h"
      11             : 
      12             : namespace v8 {
      13             : namespace base {
      14             : namespace bits {
      15             : 
      16    18827083 : uint32_t RoundUpToPowerOfTwo32(uint32_t value) {
      17             :   DCHECK_LE(value, 0x80000000u);
      18    18827083 :   value = value - 1;
      19    18827083 :   value = value | (value >> 1);
      20    18827083 :   value = value | (value >> 2);
      21    18827083 :   value = value | (value >> 4);
      22    18827083 :   value = value | (value >> 8);
      23    18827083 :   value = value | (value >> 16);
      24    18827083 :   return value + 1;
      25             : }
      26             : 
      27             : 
      28        1280 : int32_t SignedMulHigh32(int32_t lhs, int32_t rhs) {
      29        2655 :   int64_t const value = static_cast<int64_t>(lhs) * static_cast<int64_t>(rhs);
      30        3935 :   return bit_cast<int32_t, uint32_t>(bit_cast<uint64_t>(value) >> 32u);
      31             : }
      32             : 
      33             : 
      34        1375 : int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc) {
      35        1375 :   return bit_cast<int32_t>(bit_cast<uint32_t>(acc) +
      36        1375 :                            bit_cast<uint32_t>(SignedMulHigh32(lhs, rhs)));
      37             : }
      38             : 
      39             : 
      40       23231 : int32_t SignedDiv32(int32_t lhs, int32_t rhs) {
      41       23231 :   if (rhs == 0) return 0;
      42       23180 :   if (rhs == -1) return -lhs;
      43       22758 :   return lhs / rhs;
      44             : }
      45             : 
      46             : 
      47       42688 : int32_t SignedMod32(int32_t lhs, int32_t rhs) {
      48       42688 :   if (rhs == 0 || rhs == -1) return 0;
      49       42486 :   return lhs % rhs;
      50             : }
      51             : 
      52             : 
      53           0 : int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value) {
      54      245401 :   if (value.IsValid())
      55             :     return value.ValueUnsafe();
      56             : 
      57             :   // We could return max/min but we don't really expose what the maximum delta
      58             :   // is. Instead, return max/(-max), which is something that clients can reason
      59             :   // about.
      60             :   // TODO(rvargas) crbug.com/332611: don't use internal values.
      61             :   int64_t limit = std::numeric_limits<int64_t>::max();
      62           0 :   if (value.validity() == internal::RANGE_UNDERFLOW)
      63             :     limit = -limit;
      64           0 :   return value.ValueOrDefault(limit);
      65             : }
      66             : 
      67             : 
      68      245401 : int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs) {
      69             :   internal::CheckedNumeric<int64_t> rv(lhs);
      70             :   rv += rhs;
      71      245401 :   return FromCheckedNumeric(rv);
      72             : }
      73             : 
      74             : 
      75           0 : int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs) {
      76             :   internal::CheckedNumeric<int64_t> rv(lhs);
      77             :   rv -= rhs;
      78           0 :   return FromCheckedNumeric(rv);
      79             : }
      80             : 
      81      117814 : bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
      82             :   internal::CheckedNumeric<int32_t> rv(lhs);
      83             :   rv *= rhs;
      84             :   int32_t limit = std::numeric_limits<int32_t>::max();
      85      117814 :   *val = rv.ValueOrDefault(limit);
      86      117814 :   return !rv.IsValid();
      87             : }
      88             : 
      89           0 : bool SignedMulOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
      90             :   internal::CheckedNumeric<int64_t> rv(lhs);
      91             :   rv *= rhs;
      92             :   int64_t limit = std::numeric_limits<int64_t>::max();
      93           0 :   *val = rv.ValueOrDefault(limit);
      94           0 :   return !rv.IsValid();
      95             : }
      96             : 
      97             : }  // namespace bits
      98             : }  // namespace base
      99             : }  // namespace v8

Generated by: LCOV version 1.10