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