github.com/cloudwego/frugal@v0.1.15/internal/atm/ssa/int65.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     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  package ssa
    18  
    19  import (
    20      `math`
    21      `math/bits`
    22      `strconv`
    23  )
    24  
    25  var (
    26      MinInt65 = Int65 { 0, 1 }
    27      MaxInt65 = Int65 { math.MaxUint64, 0 }
    28  )
    29  
    30  const (
    31      _MinInt65Str = "-18446744073709551616"
    32  )
    33  
    34  type Int65 struct {
    35      u uint64
    36      s uint64
    37  }
    38  
    39  func Int65i(v int64) Int65 {
    40      return Int65 {
    41          u: uint64(v),
    42          s: uint64(v) >> 63,
    43      }
    44  }
    45  
    46  func (self Int65) String() string {
    47      if self.s == 0 {
    48          return strconv.FormatUint(self.u, 10)
    49      } else if self.u != 0 {
    50          return "-" + strconv.FormatUint(-self.u, 10)
    51      } else {
    52          return _MinInt65Str
    53      }
    54  }
    55  
    56  func (self Int65) OneLess() (r Int65) {
    57      r.u, r.s = bits.Sub64(self.u, 1, 0)
    58      r.s = (self.s - r.s) & 1
    59      return
    60  }
    61  
    62  func (self Int65) OneMore() (r Int65) {
    63      r.u, r.s = bits.Add64(self.u, 1, 0)
    64      r.s = (self.s + r.s) & 1
    65      return
    66  }
    67  
    68  func (self Int65) Compare(other Int65) int {
    69      if self.s == 0 && other.s != 0 {
    70          return 1
    71      } else if self.s != 0 && other.s == 0 {
    72          return -1
    73      } else {
    74          return cmpu64(self.u, other.u)
    75      }
    76  }
    77  
    78  func (self Int65) CompareZero() int {
    79      if self.s != 0 {
    80          return -1
    81      } else if self.u != 0 {
    82          return 1
    83      } else {
    84          return 0
    85      }
    86  }