utils-arm64.cc 3.44 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#if V8_TARGET_ARCH_ARM64
6

7
#include "src/codegen/arm64/utils-arm64.h"
8 9 10 11 12 13

namespace v8 {
namespace internal {

#define __ assm->

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
uint32_t float_sign(float val) {
  uint32_t bits = bit_cast<uint32_t>(val);
  return unsigned_bitextract_32(31, 31, bits);
}

uint32_t float_exp(float val) {
  uint32_t bits = bit_cast<uint32_t>(val);
  return unsigned_bitextract_32(30, 23, bits);
}

uint32_t float_mantissa(float val) {
  uint32_t bits = bit_cast<uint32_t>(val);
  return unsigned_bitextract_32(22, 0, bits);
}

uint32_t double_sign(double val) {
  uint64_t bits = bit_cast<uint64_t>(val);
  return static_cast<uint32_t>(unsigned_bitextract_64(63, 63, bits));
}

uint32_t double_exp(double val) {
  uint64_t bits = bit_cast<uint64_t>(val);
  return static_cast<uint32_t>(unsigned_bitextract_64(62, 52, bits));
}

uint64_t double_mantissa(double val) {
  uint64_t bits = bit_cast<uint64_t>(val);
  return unsigned_bitextract_64(51, 0, bits);
}

float float_pack(uint32_t sign, uint32_t exp, uint32_t mantissa) {
  uint32_t bits = sign << kFloatExponentBits | exp;
  return bit_cast<float>((bits << kFloatMantissaBits) | mantissa);
}

double double_pack(uint64_t sign, uint64_t exp, uint64_t mantissa) {
  uint64_t bits = sign << kDoubleExponentBits | exp;
  return bit_cast<double>((bits << kDoubleMantissaBits) | mantissa);
}

int float16classify(float16 value) {
  const uint16_t exponent_max = (1 << kFloat16ExponentBits) - 1;
  const uint16_t exponent_mask = exponent_max << kFloat16MantissaBits;
  const uint16_t mantissa_mask = (1 << kFloat16MantissaBits) - 1;

  const uint16_t exponent = (value & exponent_mask) >> kFloat16MantissaBits;
  const uint16_t mantissa = value & mantissa_mask;
  if (exponent == 0) {
    if (mantissa == 0) {
      return FP_ZERO;
    }
    return FP_SUBNORMAL;
  } else if (exponent == exponent_max) {
    if (mantissa == 0) {
      return FP_INFINITE;
    }
    return FP_NAN;
  }
  return FP_NORMAL;
}
74 75

int CountLeadingZeros(uint64_t value, int width) {
76
  DCHECK(base::bits::IsPowerOfTwo(width) && (width <= 64));
77 78
  if (value == 0) {
    return width;
79
  }
80
  return base::bits::CountLeadingZeros64(value << (64 - width));
81 82 83
}

int CountLeadingSignBits(int64_t value, int width) {
84
  DCHECK(base::bits::IsPowerOfTwo(width) && (width <= 64));
85 86 87 88 89 90 91 92
  if (value >= 0) {
    return CountLeadingZeros(value, width) - 1;
  } else {
    return CountLeadingZeros(~value, width) - 1;
  }
}

int CountSetBits(uint64_t value, int width) {
93
  DCHECK((width == 32) || (width == 64));
94
  if (width == 64) {
95
    return static_cast<int>(base::bits::CountPopulation(value));
96
  }
97
  return static_cast<int>(
98
      base::bits::CountPopulation(static_cast<uint32_t>(value & 0xFFFFFFFFF)));
99
}
100

101 102
int LowestSetBitPosition(uint64_t value) {
  DCHECK_NE(value, 0U);
103
  return base::bits::CountTrailingZeros(value) + 1;
104
}
105

106 107 108
int HighestSetBitPosition(uint64_t value) {
  DCHECK_NE(value, 0U);
  return 63 - CountLeadingZeros(value, 64);
109 110
}

111
uint64_t LargestPowerOf2Divisor(uint64_t value) {
112 113 114
  // Simulate two's complement (instead of casting to signed and negating) to
  // avoid undefined behavior on signed overflow.
  return value & ((~value) + 1);
115 116
}

117
int MaskToBit(uint64_t mask) {
118
  DCHECK_EQ(CountSetBits(mask, 64), 1);
119
  return base::bits::CountTrailingZeros(mask);
120 121
}

122
#undef __
123

124 125
}  // namespace internal
}  // namespace v8
126

127
#endif  // V8_TARGET_ARCH_ARM64