Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 5 | package starlark |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "math" |
| 10 | "math/big" |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 11 | "strconv" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 12 | |
Alan Donovan | 6beab7e | 2018-10-31 17:53:09 -0400 | [diff] [blame] | 13 | "go.starlark.net/syntax" |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 16 | // Int is the type of a Starlark int. |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 17 | // |
| 18 | // The zero value is not a legal value; use MakeInt(0). |
| 19 | type Int struct{ impl intImpl } |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 20 | |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 21 | // --- high-level accessors --- |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 22 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 23 | // MakeInt returns a Starlark int for the specified signed integer. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 24 | func MakeInt(x int) Int { return MakeInt64(int64(x)) } |
| 25 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 26 | // MakeInt64 returns a Starlark int for the specified int64. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 27 | func MakeInt64(x int64) Int { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 28 | if math.MinInt32 <= x && x <= math.MaxInt32 { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 29 | return makeSmallInt(x) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 30 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 31 | return makeBigInt(big.NewInt(x)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 34 | // MakeUint returns a Starlark int for the specified unsigned integer. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 35 | func MakeUint(x uint) Int { return MakeUint64(uint64(x)) } |
| 36 | |
Alan Donovan | e3deafe | 2018-10-23 11:05:09 -0400 | [diff] [blame] | 37 | // MakeUint64 returns a Starlark int for the specified uint64. |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 38 | func MakeUint64(x uint64) Int { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 39 | if x <= math.MaxInt32 { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 40 | return makeSmallInt(int64(x)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 41 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 42 | return makeBigInt(new(big.Int).SetUint64(x)) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // MakeBigInt returns a Starlark int for the specified big.Int. |
| 46 | // The caller must not subsequently modify x. |
| 47 | func MakeBigInt(x *big.Int) Int { |
| 48 | if n := x.BitLen(); n < 32 || n == 32 && x.Int64() == math.MinInt32 { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 49 | return makeSmallInt(x.Int64()) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 50 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 51 | return makeBigInt(x) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | var ( |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 55 | zero, one = makeSmallInt(0), makeSmallInt(1) |
| 56 | oneBig = big.NewInt(1) |
alandonovan | 58f9101 | 2019-01-03 16:32:47 -0500 | [diff] [blame] | 57 | |
| 58 | _ HasUnary = Int{} |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 59 | ) |
| 60 | |
alandonovan | 58f9101 | 2019-01-03 16:32:47 -0500 | [diff] [blame] | 61 | // Unary implements the operations +int, -int, and ~int. |
| 62 | func (i Int) Unary(op syntax.Token) (Value, error) { |
| 63 | switch op { |
| 64 | case syntax.MINUS: |
| 65 | return zero.Sub(i), nil |
| 66 | case syntax.PLUS: |
| 67 | return i, nil |
| 68 | case syntax.TILDE: |
| 69 | return i.Not(), nil |
| 70 | } |
| 71 | return nil, nil |
| 72 | } |
| 73 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 74 | // Int64 returns the value as an int64. |
| 75 | // If it is not exactly representable the result is undefined and ok is false. |
| 76 | func (i Int) Int64() (_ int64, ok bool) { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 77 | iSmall, iBig := i.get() |
| 78 | if iBig != nil { |
| 79 | x, acc := bigintToInt64(iBig) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 80 | if acc != big.Exact { |
| 81 | return // inexact |
| 82 | } |
| 83 | return x, true |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 84 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 85 | return iSmall, true |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // BigInt returns the value as a big.Int. |
| 89 | // The returned variable must not be modified by the client. |
| 90 | func (i Int) BigInt() *big.Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 91 | iSmall, iBig := i.get() |
| 92 | if iBig != nil { |
| 93 | return iBig |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 94 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 95 | return big.NewInt(iSmall) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Uint64 returns the value as a uint64. |
| 99 | // If it is not exactly representable the result is undefined and ok is false. |
| 100 | func (i Int) Uint64() (_ uint64, ok bool) { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 101 | iSmall, iBig := i.get() |
| 102 | if iBig != nil { |
| 103 | x, acc := bigintToUint64(iBig) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 104 | if acc != big.Exact { |
| 105 | return // inexact |
| 106 | } |
| 107 | return x, true |
| 108 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 109 | if iSmall < 0 { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 110 | return // inexact |
| 111 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 112 | return uint64(iSmall), true |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // The math/big API should provide this function. |
| 116 | func bigintToInt64(i *big.Int) (int64, big.Accuracy) { |
| 117 | sign := i.Sign() |
| 118 | if sign > 0 { |
| 119 | if i.Cmp(maxint64) > 0 { |
| 120 | return math.MaxInt64, big.Below |
| 121 | } |
| 122 | } else if sign < 0 { |
| 123 | if i.Cmp(minint64) < 0 { |
| 124 | return math.MinInt64, big.Above |
| 125 | } |
| 126 | } |
| 127 | return i.Int64(), big.Exact |
| 128 | } |
| 129 | |
| 130 | // The math/big API should provide this function. |
| 131 | func bigintToUint64(i *big.Int) (uint64, big.Accuracy) { |
| 132 | sign := i.Sign() |
| 133 | if sign > 0 { |
| 134 | if i.BitLen() > 64 { |
| 135 | return math.MaxUint64, big.Below |
| 136 | } |
| 137 | } else if sign < 0 { |
| 138 | return 0, big.Above |
| 139 | } |
| 140 | return i.Uint64(), big.Exact |
| 141 | } |
| 142 | |
| 143 | var ( |
| 144 | minint64 = new(big.Int).SetInt64(math.MinInt64) |
| 145 | maxint64 = new(big.Int).SetInt64(math.MaxInt64) |
| 146 | ) |
| 147 | |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 148 | func (i Int) Format(s fmt.State, ch rune) { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 149 | iSmall, iBig := i.get() |
| 150 | if iBig != nil { |
| 151 | iBig.Format(s, ch) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 152 | return |
| 153 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 154 | big.NewInt(iSmall).Format(s, ch) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 155 | } |
| 156 | func (i Int) String() string { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 157 | iSmall, iBig := i.get() |
| 158 | if iBig != nil { |
| 159 | return iBig.Text(10) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 160 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 161 | return strconv.FormatInt(iSmall, 10) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 162 | } |
| 163 | func (i Int) Type() string { return "int" } |
| 164 | func (i Int) Freeze() {} // immutable |
| 165 | func (i Int) Truth() Bool { return i.Sign() != 0 } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 166 | func (i Int) Hash() (uint32, error) { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 167 | iSmall, iBig := i.get() |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 168 | var lo big.Word |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 169 | if iBig != nil { |
| 170 | lo = iBig.Bits()[0] |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 171 | } else { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 172 | lo = big.Word(iSmall) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 173 | } |
| 174 | return 12582917 * uint32(lo+3), nil |
| 175 | } |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 176 | func (x Int) CompareSameType(op syntax.Token, v Value, depth int) (bool, error) { |
| 177 | y := v.(Int) |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 178 | xSmall, xBig := x.get() |
| 179 | ySmall, yBig := y.get() |
| 180 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 181 | return threeway(op, x.BigInt().Cmp(y.BigInt())), nil |
| 182 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 183 | return threeway(op, signum64(xSmall-ySmall)), nil |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Float returns the float value nearest i. |
| 187 | func (i Int) Float() Float { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 188 | iSmall, iBig := i.get() |
| 189 | if iBig != nil { |
| 190 | f, _ := new(big.Float).SetInt(iBig).Float64() |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 191 | return Float(f) |
| 192 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 193 | return Float(iSmall) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 194 | } |
| 195 | |
alandonovan | 3b7e02e | 2020-11-11 12:03:41 -0500 | [diff] [blame^] | 196 | // finiteFloat returns the finite float value nearest i, |
| 197 | // or an error if the magnitude is too large. |
| 198 | func (i Int) finiteFloat() (Float, error) { |
| 199 | f := i.Float() |
| 200 | if math.IsInf(float64(f), 0) { |
| 201 | return 0, fmt.Errorf("int too large to convert to float") |
| 202 | } |
| 203 | return f, nil |
| 204 | } |
| 205 | |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 206 | func (x Int) Sign() int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 207 | xSmall, xBig := x.get() |
| 208 | if xBig != nil { |
| 209 | return xBig.Sign() |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 210 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 211 | return signum64(xSmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | func (x Int) Add(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 215 | xSmall, xBig := x.get() |
| 216 | ySmall, yBig := y.get() |
| 217 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 218 | return MakeBigInt(new(big.Int).Add(x.BigInt(), y.BigInt())) |
| 219 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 220 | return MakeInt64(xSmall + ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 221 | } |
| 222 | func (x Int) Sub(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 223 | xSmall, xBig := x.get() |
| 224 | ySmall, yBig := y.get() |
| 225 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 226 | return MakeBigInt(new(big.Int).Sub(x.BigInt(), y.BigInt())) |
| 227 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 228 | return MakeInt64(xSmall - ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 229 | } |
| 230 | func (x Int) Mul(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 231 | xSmall, xBig := x.get() |
| 232 | ySmall, yBig := y.get() |
| 233 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 234 | return MakeBigInt(new(big.Int).Mul(x.BigInt(), y.BigInt())) |
| 235 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 236 | return MakeInt64(xSmall * ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 237 | } |
| 238 | func (x Int) Or(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 239 | xSmall, xBig := x.get() |
| 240 | ySmall, yBig := y.get() |
| 241 | if xBig != nil || yBig != nil { |
| 242 | return MakeBigInt(new(big.Int).Or(x.BigInt(), y.BigInt())) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 243 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 244 | return makeSmallInt(xSmall | ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 245 | } |
| 246 | func (x Int) And(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 247 | xSmall, xBig := x.get() |
| 248 | ySmall, yBig := y.get() |
| 249 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 250 | return MakeBigInt(new(big.Int).And(x.BigInt(), y.BigInt())) |
| 251 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 252 | return makeSmallInt(xSmall & ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 253 | } |
| 254 | func (x Int) Xor(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 255 | xSmall, xBig := x.get() |
| 256 | ySmall, yBig := y.get() |
| 257 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 258 | return MakeBigInt(new(big.Int).Xor(x.BigInt(), y.BigInt())) |
| 259 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 260 | return makeSmallInt(xSmall ^ ySmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 261 | } |
| 262 | func (x Int) Not() Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 263 | xSmall, xBig := x.get() |
| 264 | if xBig != nil { |
| 265 | return MakeBigInt(new(big.Int).Not(xBig)) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 266 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 267 | return makeSmallInt(^xSmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 268 | } |
| 269 | func (x Int) Lsh(y uint) Int { return MakeBigInt(new(big.Int).Lsh(x.BigInt(), y)) } |
| 270 | func (x Int) Rsh(y uint) Int { return MakeBigInt(new(big.Int).Rsh(x.BigInt(), y)) } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 271 | |
| 272 | // Precondition: y is nonzero. |
| 273 | func (x Int) Div(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 274 | xSmall, xBig := x.get() |
| 275 | ySmall, yBig := y.get() |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 276 | // http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 277 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 278 | xb, yb := x.BigInt(), y.BigInt() |
| 279 | |
| 280 | var quo, rem big.Int |
| 281 | quo.QuoRem(xb, yb, &rem) |
| 282 | if (xb.Sign() < 0) != (yb.Sign() < 0) && rem.Sign() != 0 { |
| 283 | quo.Sub(&quo, oneBig) |
| 284 | } |
| 285 | return MakeBigInt(&quo) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 286 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 287 | quo := xSmall / ySmall |
| 288 | rem := xSmall % ySmall |
| 289 | if (xSmall < 0) != (ySmall < 0) && rem != 0 { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 290 | quo -= 1 |
| 291 | } |
| 292 | return MakeInt64(quo) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Precondition: y is nonzero. |
| 296 | func (x Int) Mod(y Int) Int { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 297 | xSmall, xBig := x.get() |
| 298 | ySmall, yBig := y.get() |
| 299 | if xBig != nil || yBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 300 | xb, yb := x.BigInt(), y.BigInt() |
| 301 | |
| 302 | var quo, rem big.Int |
| 303 | quo.QuoRem(xb, yb, &rem) |
| 304 | if (xb.Sign() < 0) != (yb.Sign() < 0) && rem.Sign() != 0 { |
| 305 | rem.Add(&rem, yb) |
| 306 | } |
| 307 | return MakeBigInt(&rem) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 308 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 309 | rem := xSmall % ySmall |
| 310 | if (xSmall < 0) != (ySmall < 0) && rem != 0 { |
| 311 | rem += ySmall |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 312 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 313 | return makeSmallInt(rem) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 316 | func (i Int) rational() *big.Rat { |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 317 | iSmall, iBig := i.get() |
| 318 | if iBig != nil { |
| 319 | return new(big.Rat).SetInt(iBig) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 320 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 321 | return new(big.Rat).SetInt64(iSmall) |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 322 | } |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 323 | |
| 324 | // AsInt32 returns the value of x if is representable as an int32. |
| 325 | func AsInt32(x Value) (int, error) { |
| 326 | i, ok := x.(Int) |
| 327 | if !ok { |
| 328 | return 0, fmt.Errorf("got %s, want int", x.Type()) |
| 329 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 330 | iSmall, iBig := i.get() |
| 331 | if iBig != nil { |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 332 | return 0, fmt.Errorf("%s out of range", i) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 333 | } |
alandonovan | c6daab6 | 2020-06-17 14:27:56 -0400 | [diff] [blame] | 334 | return int(iSmall), nil |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 335 | } |
| 336 | |
alandonovan | 05f260d | 2017-10-18 12:43:53 -0400 | [diff] [blame] | 337 | // NumberToInt converts a number x to an integer value. |
| 338 | // An int is returned unchanged, a float is truncated towards zero. |
| 339 | // NumberToInt reports an error for all other values. |
| 340 | func NumberToInt(x Value) (Int, error) { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 341 | switch x := x.(type) { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 342 | case Int: |
| 343 | return x, nil |
| 344 | case Float: |
| 345 | f := float64(x) |
| 346 | if math.IsInf(f, 0) { |
| 347 | return zero, fmt.Errorf("cannot convert float infinity to integer") |
| 348 | } else if math.IsNaN(f) { |
| 349 | return zero, fmt.Errorf("cannot convert float NaN to integer") |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 350 | } |
Diego Siqueira | 6cae335 | 2017-10-08 01:16:14 +0200 | [diff] [blame] | 351 | return finiteFloatToInt(x), nil |
| 352 | |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 353 | } |
| 354 | return zero, fmt.Errorf("cannot convert %s to int", x.Type()) |
| 355 | } |
| 356 | |
| 357 | // finiteFloatToInt converts f to an Int, truncating towards zero. |
| 358 | // f must be finite. |
| 359 | func finiteFloatToInt(f Float) Int { |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 360 | if math.MinInt64 <= f && f <= math.MaxInt64 { |
| 361 | // small values |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 362 | return MakeInt64(int64(f)) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 363 | } |
Edward McFarlane | d50186b | 2019-02-24 19:44:57 +0000 | [diff] [blame] | 364 | rat := f.rational() |
| 365 | if rat == nil { |
| 366 | panic(f) // non-finite |
| 367 | } |
| 368 | return MakeBigInt(new(big.Int).Div(rat.Num(), rat.Denom())) |
Alan Donovan | 312d1a5 | 2017-10-02 10:10:28 -0400 | [diff] [blame] | 369 | } |