integer-literal-inl.h 1.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 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
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_NUMBERS_INTEGER_LITERAL_INL_H_
#define V8_NUMBERS_INTEGER_LITERAL_INL_H_

#include "src/numbers/integer-literal.h"

namespace v8 {
namespace internal {

inline std::string IntegerLiteral::ToString() const {
  if (negative_) return std::string("-") + std::to_string(absolute_value_);
  return std::to_string(absolute_value_);
}

inline IntegerLiteral operator<<(const IntegerLiteral& x,
                                 const IntegerLiteral& y) {
  DCHECK(!y.is_negative());
  DCHECK_LT(y.absolute_value(), sizeof(uint64_t) * kBitsPerByte);
  return IntegerLiteral(x.is_negative(), x.absolute_value()
                                             << y.absolute_value());
}

inline IntegerLiteral operator+(const IntegerLiteral& x,
                                const IntegerLiteral& y) {
  if (x.is_negative() == y.is_negative()) {
    DCHECK_GE(x.absolute_value() + y.absolute_value(), x.absolute_value());
    return IntegerLiteral(x.is_negative(),
                          x.absolute_value() + y.absolute_value());
  }
  if (x.absolute_value() >= y.absolute_value()) {
    return IntegerLiteral(x.is_negative(),
                          x.absolute_value() - y.absolute_value());
  }
  return IntegerLiteral(!x.is_negative(),
                        y.absolute_value() - x.absolute_value());
}

}  // namespace internal
}  // namespace v8
#endif  // V8_NUMBERS_INTEGER_LITERAL_INL_H_