github.com/goplus/gogen@v1.16.0/internal/builtin/big.go (about)

     1  /*
     2   Copyright 2021 The GoPlus Authors (goplus.org)
     3   Licensed under the Apache License, Version 2.0 (the "License");
     4   you may not use this file except in compliance with the License.
     5   You may obtain a copy of the License at
     6       http://www.apache.org/licenses/LICENSE-2.0
     7   Unless required by applicable law or agreed to in writing, software
     8   distributed under the License is distributed on an "AS IS" BASIS,
     9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10   See the License for the specific language governing permissions and
    11   limitations under the License.
    12  */
    13  
    14  package builtin
    15  
    16  import (
    17  	"math/big"
    18  )
    19  
    20  const (
    21  	GopPackage = true // to indicate this is a Go+ package
    22  )
    23  
    24  //
    25  // Gop_: Go+ object prefix
    26  // Gop_xxx_Cast: type Gop_xxx typecast
    27  // xxxx__N: the Nth overload function
    28  //
    29  
    30  type Gop_ninteger = uint
    31  
    32  func Gop_istmp(a interface{}) bool {
    33  	return false
    34  }
    35  
    36  // -----------------------------------------------------------------------------
    37  
    38  type Gop_untyped_bigint *big.Int
    39  type Gop_untyped_bigrat *big.Rat
    40  type Gop_untyped_bigfloat *big.Float
    41  
    42  type Gop_untyped_bigint_Default = Gop_bigint
    43  type Gop_untyped_bigrat_Default = Gop_bigrat
    44  type Gop_untyped_bigfloat_Default = Gop_bigfloat
    45  
    46  func Gop_untyped_bigint_Init__0(x int) Gop_untyped_bigint {
    47  	panic("make compiler happy")
    48  }
    49  
    50  func Gop_untyped_bigrat_Init__0(x int) Gop_untyped_bigrat {
    51  	panic("make compiler happy")
    52  }
    53  
    54  func Gop_untyped_bigrat_Init__1(x Gop_untyped_bigint) Gop_untyped_bigrat {
    55  	panic("make compiler happy")
    56  }
    57  
    58  // -----------------------------------------------------------------------------
    59  // type bigint
    60  
    61  // A Gop_bigint represents a signed multi-precision integer.
    62  // The zero value for a Gop_bigint represents nil.
    63  type Gop_bigint struct {
    64  	*big.Int
    65  }
    66  
    67  func tmpint(a, b Gop_bigint) Gop_bigint {
    68  	if Gop_istmp(a) {
    69  		return a
    70  	} else if Gop_istmp(b) {
    71  		return b
    72  	}
    73  	return Gop_bigint{new(big.Int)}
    74  }
    75  
    76  func tmpint1(a Gop_bigint) Gop_bigint {
    77  	if Gop_istmp(a) {
    78  		return a
    79  	}
    80  	return Gop_bigint{new(big.Int)}
    81  }
    82  
    83  // IsNil returns a bigint object is nil or not
    84  func (a Gop_bigint) IsNil() bool {
    85  	return a.Int == nil
    86  }
    87  
    88  // Gop_Assign: func (a bigint) = (b bigint)
    89  func (a Gop_bigint) Gop_Assign(b Gop_bigint) {
    90  	if Gop_istmp(b) {
    91  		*a.Int = *b.Int
    92  	} else {
    93  		a.Int.Set(b.Int)
    94  	}
    95  }
    96  
    97  // Gop_Add: func (a bigint) + (b bigint) bigint
    98  func (a Gop_bigint) Gop_Add(b Gop_bigint) Gop_bigint {
    99  	return Gop_bigint{tmpint(a, b).Add(a.Int, b.Int)}
   100  }
   101  
   102  // Gop_Sub: func (a bigint) - (b bigint) bigint
   103  func (a Gop_bigint) Gop_Sub(b Gop_bigint) Gop_bigint {
   104  	return Gop_bigint{tmpint(a, b).Sub(a.Int, b.Int)}
   105  }
   106  
   107  // Gop_Mul: func (a bigint) * (b bigint) bigint
   108  func (a Gop_bigint) Gop_Mul(b Gop_bigint) Gop_bigint {
   109  	return Gop_bigint{tmpint(a, b).Mul(a.Int, b.Int)}
   110  }
   111  
   112  // Gop_Quo: func (a bigint) / (b bigint) bigint {
   113  func (a Gop_bigint) Gop_Quo(b Gop_bigint) Gop_bigint {
   114  	return Gop_bigint{tmpint(a, b).Quo(a.Int, b.Int)}
   115  }
   116  
   117  // Gop_Rem: func (a bigint) % (b bigint) bigint
   118  func (a Gop_bigint) Gop_Rem(b Gop_bigint) Gop_bigint {
   119  	return Gop_bigint{tmpint(a, b).Rem(a.Int, b.Int)}
   120  }
   121  
   122  // Gop_Or: func (a bigint) | (b bigint) bigint
   123  func (a Gop_bigint) Gop_Or(b Gop_bigint) Gop_bigint {
   124  	return Gop_bigint{tmpint(a, b).Or(a.Int, b.Int)}
   125  }
   126  
   127  // Gop_Xor: func (a bigint) ^ (b bigint) bigint
   128  func (a Gop_bigint) Gop_Xor(b Gop_bigint) Gop_bigint {
   129  	return Gop_bigint{tmpint(a, b).Xor(a.Int, b.Int)}
   130  }
   131  
   132  // Gop_And: func (a bigint) & (b bigint) bigint
   133  func (a Gop_bigint) Gop_And(b Gop_bigint) Gop_bigint {
   134  	return Gop_bigint{tmpint(a, b).And(a.Int, b.Int)}
   135  }
   136  
   137  // Gop_AndNot: func (a bigint) &^ (b bigint) bigint
   138  func (a *Gop_bigint) Gop_AndNot__0(b *Gop_bigint) *Gop_bigint {
   139  	return a
   140  }
   141  
   142  // Gop_Lsh: func (a bigint) << (n untyped_uint) bigint
   143  func (a Gop_bigint) Gop_Lsh(n Gop_ninteger) Gop_bigint {
   144  	return Gop_bigint{tmpint1(a).Lsh(a.Int, uint(n))}
   145  }
   146  
   147  // Gop_Rsh: func (a bigint) >> (n untyped_uint) bigint
   148  func (a Gop_bigint) Gop_Rsh(n Gop_ninteger) Gop_bigint {
   149  	return Gop_bigint{tmpint1(a).Rsh(a.Int, uint(n))}
   150  }
   151  
   152  // Gop_LT: func (a bigint) < (b bigint) bool
   153  func (a Gop_bigint) Gop_LT(b Gop_bigint) bool {
   154  	return a.Cmp(b.Int) < 0
   155  }
   156  
   157  // Gop_LE: func (a bigint) <= (b bigint) bool
   158  func (a Gop_bigint) Gop_LE(b Gop_bigint) bool {
   159  	return a.Cmp(b.Int) <= 0
   160  }
   161  
   162  // Gop_GT: func (a bigint) > (b bigint) bool
   163  func (a Gop_bigint) Gop_GT(b Gop_bigint) bool {
   164  	return a.Cmp(b.Int) > 0
   165  }
   166  
   167  // Gop_GE: func (a bigint) >= (b bigint) bool
   168  func (a Gop_bigint) Gop_GE(b Gop_bigint) bool {
   169  	return a.Cmp(b.Int) >= 0
   170  }
   171  
   172  // Gop_EQ: func (a bigint) == (b bigint) bool
   173  func (a Gop_bigint) Gop_EQ(b Gop_bigint) bool {
   174  	return a.Cmp(b.Int) == 0
   175  }
   176  
   177  // Gop_NE: func (a bigint) != (b bigint) bool
   178  func (a Gop_bigint) Gop_NE(b Gop_bigint) bool {
   179  	return a.Cmp(b.Int) != 0
   180  }
   181  
   182  // Gop_Neg: func -(a bigint) bigint
   183  func (a Gop_bigint) Gop_Neg() Gop_bigint {
   184  	return Gop_bigint{tmpint1(a).Neg(a.Int)}
   185  }
   186  
   187  // Gop_Dup: func +(a bigint) bigint
   188  func (a Gop_bigint) Gop_Dup() Gop_bigint {
   189  	return a
   190  }
   191  
   192  // Gop_Not: func ^(a bigint) bigint
   193  func (a Gop_bigint) Gop_Not() Gop_bigint {
   194  	return Gop_bigint{tmpint1(a).Not(a.Int)}
   195  }
   196  
   197  // Gop_Add: func (a bigint) += (b bigint)
   198  func (a Gop_bigint) Gop_AddAssign(b Gop_bigint) {
   199  	a.Int.Add(a.Int, b.Int)
   200  }
   201  
   202  // Gop_Sub: func (a bigint) -= (b bigint)
   203  func (a Gop_bigint) Gop_SubAssign(b Gop_bigint) {
   204  	a.Int.Sub(a.Int, b.Int)
   205  }
   206  
   207  // Gop_Mul: func (a bigint) *= (b bigint)
   208  func (a Gop_bigint) Gop_MulAssign(b Gop_bigint) {
   209  	a.Int.Mul(a.Int, b.Int)
   210  }
   211  
   212  // Gop_Quo: func (a bigint) /= (b bigint) {
   213  func (a Gop_bigint) Gop_QuoAssign(b Gop_bigint) {
   214  	a.Int.Quo(a.Int, b.Int)
   215  }
   216  
   217  // Gop_Rem: func (a bigint) %= (b bigint)
   218  func (a Gop_bigint) Gop_RemAssign(b Gop_bigint) {
   219  	a.Int.Rem(a.Int, b.Int)
   220  }
   221  
   222  // Gop_Or: func (a bigint) |= (b bigint)
   223  func (a Gop_bigint) Gop_OrAssign(b Gop_bigint) {
   224  	a.Int.Or(a.Int, b.Int)
   225  }
   226  
   227  // Gop_Xor: func (a bigint) ^= (b bigint)
   228  func (a Gop_bigint) Gop_XorAssign(b Gop_bigint) {
   229  	a.Int.Xor(a.Int, b.Int)
   230  }
   231  
   232  // Gop_And: func (a bigint) &= (b bigint)
   233  func (a Gop_bigint) Gop_AndAssign(b Gop_bigint) {
   234  	a.Int.And(a.Int, b.Int)
   235  }
   236  
   237  // Gop_AndNot: func (a bigint) &^= (b bigint)
   238  func (a Gop_bigint) Gop_AndNotAssign(b Gop_bigint) {
   239  	a.Int.AndNot(a.Int, b.Int)
   240  }
   241  
   242  // Gop_Lsh: func (a bigint) <<= (n untyped_uint)
   243  func (a Gop_bigint) Gop_LshAssign(n Gop_ninteger) {
   244  	a.Int.Lsh(a.Int, uint(n))
   245  }
   246  
   247  // Gop_Rsh: func (a bigint) >>= (n untyped_uint)
   248  func (a Gop_bigint) Gop_RshAssign(n Gop_ninteger) {
   249  	a.Int.Rsh(a.Int, uint(n))
   250  }
   251  
   252  func (a Gop_bigint) Gop_Rcast() float64 {
   253  	return 0
   254  }
   255  
   256  // Gop_bigint_Cast: func bigint(x int) bigint
   257  func Gop_bigint_Cast__0(x int) Gop_bigint {
   258  	return Gop_bigint{big.NewInt(int64(x))}
   259  }
   260  
   261  // Gop_bigint_Cast: func bigint(x untyped_bigint) bigint
   262  func Gop_bigint_Cast__1(x Gop_untyped_bigint) Gop_bigint {
   263  	return Gop_bigint{x}
   264  }
   265  
   266  // Gop_bigint_Cast: func bigint(x int64) bigint
   267  func Gop_bigint_Cast__2(x int64) Gop_bigint {
   268  	return Gop_bigint{big.NewInt(x)}
   269  }
   270  
   271  // Gop_bigint_Cast: func bigint(x uint64) bigint
   272  func Gop_bigint_Cast__3(x uint64) Gop_bigint {
   273  	return Gop_bigint{new(big.Int).SetUint64(x)}
   274  }
   275  
   276  // Gop_bigint_Cast: func bigint(x uint) bigint
   277  func Gop_bigint_Cast__4(x uint) Gop_bigint {
   278  	return Gop_bigint{new(big.Int).SetUint64(uint64(x))}
   279  }
   280  
   281  // Gop_bigint_Cast: func bigint(x *big.Int) bigint
   282  func Gop_bigint_Cast__5(x *big.Int) Gop_bigint {
   283  	return Gop_bigint{x}
   284  }
   285  
   286  // Gop_bigint_Cast: func bigint(x bigrat) bigint
   287  func Gop_bigint_Cast__6(x Gop_bigrat) Gop_bigint {
   288  	if x.IsInt() {
   289  		return Gop_bigint{x.Num()}
   290  	}
   291  	ret, _ := new(big.Float).SetRat(x.Rat).Int(nil)
   292  	return Gop_bigint{ret}
   293  }
   294  
   295  // Gop_bigint_Cast: func bigint(x bigrat) (ret bigint, exact bool)
   296  func Gop_bigint_Cast__7(x Gop_bigrat) (Gop_bigint, bool) {
   297  	return Gop_bigint_Cast__6(x), x.IsInt()
   298  }
   299  
   300  // Gop_bigint_Init: func bigint.init(x int) bigint
   301  func Gop_bigint_Init__0(x int) Gop_bigint {
   302  	return Gop_bigint{big.NewInt(int64(x))}
   303  }
   304  
   305  // Gop_bigint_Init: func bigint.init(x *big.Int) bigint
   306  func Gop_bigint_Init__1(x *big.Int) Gop_bigint {
   307  	return Gop_bigint{x}
   308  }
   309  
   310  // Gop_bigint_Init: func bigint.init(x *big.Rat) bigint
   311  func Gop_bigint_Init__2(x *big.Rat) Gop_bigint {
   312  	if x.IsInt() {
   313  		return Gop_bigint{x.Num()}
   314  	}
   315  	panic("TODO: can't init bigint from bigrat")
   316  }
   317  
   318  // -----------------------------------------------------------------------------
   319  // type bigrat
   320  
   321  // A Gop_bigrat represents a quotient a/b of arbitrary precision.
   322  // The zero value for a Gop_bigrat represents nil.
   323  type Gop_bigrat struct {
   324  	*big.Rat
   325  }
   326  
   327  func tmprat(a, b Gop_bigrat) Gop_bigrat {
   328  	if Gop_istmp(a) {
   329  		return a
   330  	} else if Gop_istmp(b) {
   331  		return b
   332  	}
   333  	return Gop_bigrat{new(big.Rat)}
   334  }
   335  
   336  func tmprat1(a Gop_bigrat) Gop_bigrat {
   337  	if Gop_istmp(a) {
   338  		return a
   339  	}
   340  	return Gop_bigrat{new(big.Rat)}
   341  }
   342  
   343  // IsNil returns a bigrat object is nil or not
   344  func (a Gop_bigrat) IsNil() bool {
   345  	return a.Rat == nil
   346  }
   347  
   348  // Gop_Assign: func (a bigrat) = (b bigrat)
   349  func (a Gop_bigrat) Gop_Assign(b Gop_bigrat) {
   350  	if Gop_istmp(b) {
   351  		*a.Rat = *b.Rat
   352  	} else {
   353  		a.Rat.Set(b.Rat)
   354  	}
   355  }
   356  
   357  // Gop_Add: func (a bigrat) + (b bigrat) bigrat
   358  func (a Gop_bigrat) Gop_Add(b Gop_bigrat) Gop_bigrat {
   359  	return Gop_bigrat{tmprat(a, b).Add(a.Rat, b.Rat)}
   360  }
   361  
   362  // Gop_Sub: func (a bigrat) - (b bigrat) bigrat
   363  func (a Gop_bigrat) Gop_Sub__0(b Gop_bigrat) Gop_bigrat {
   364  	return Gop_bigrat{tmprat(a, b).Sub(a.Rat, b.Rat)}
   365  }
   366  
   367  // Gop_Mul: func (a bigrat) * (b bigrat) bigrat
   368  func (a Gop_bigrat) Gop_Mul(b Gop_bigrat) Gop_bigrat {
   369  	return Gop_bigrat{tmprat(a, b).Mul(a.Rat, b.Rat)}
   370  }
   371  
   372  // Gop_Quo: func (a bigrat) / (b bigrat) bigrat
   373  func (a Gop_bigrat) Gop_Quo(b Gop_bigrat) Gop_bigrat {
   374  	return Gop_bigrat{tmprat(a, b).Quo(a.Rat, b.Rat)}
   375  }
   376  
   377  // Gop_LT: func (a bigrat) < (b bigrat) bool
   378  func (a Gop_bigrat) Gop_LT(b Gop_bigrat) bool {
   379  	return a.Cmp(b.Rat) < 0
   380  }
   381  
   382  // Gop_LE: func (a bigrat) <= (b bigrat) bool
   383  func (a Gop_bigrat) Gop_LE(b Gop_bigrat) bool {
   384  	return a.Cmp(b.Rat) <= 0
   385  }
   386  
   387  // Gop_GT: func (a bigrat) > (b bigrat) bool
   388  func (a Gop_bigrat) Gop_GT(b Gop_bigrat) bool {
   389  	return a.Cmp(b.Rat) > 0
   390  }
   391  
   392  // Gop_GE: func (a bigrat) >= (b bigrat) bool
   393  func (a Gop_bigrat) Gop_GE(b Gop_bigrat) bool {
   394  	return a.Cmp(b.Rat) >= 0
   395  }
   396  
   397  // Gop_EQ: func (a bigrat) == (b bigrat) bool
   398  func (a Gop_bigrat) Gop_EQ(b Gop_bigrat) bool {
   399  	return a.Cmp(b.Rat) == 0
   400  }
   401  
   402  // Gop_NE: func (a bigrat) != (b bigrat) bool
   403  func (a Gop_bigrat) Gop_NE(b Gop_bigrat) bool {
   404  	return a.Cmp(b.Rat) != 0
   405  }
   406  
   407  // Gop_Neg: func -(a bigrat) bigrat
   408  func (a Gop_bigrat) Gop_Neg() Gop_bigrat {
   409  	return Gop_bigrat{tmprat1(a).Neg(a.Rat)}
   410  }
   411  
   412  // Gop_Dup: func +(a bigrat) bigrat
   413  func (a Gop_bigrat) Gop_Dup() Gop_bigrat {
   414  	return a
   415  }
   416  
   417  // Gop_Inc: func ++(a *bigrat)
   418  func (a *Gop_bigrat) Gop_Inc() {
   419  }
   420  
   421  // Gop_Dec: func --(a *bigrat) int
   422  func (a *Gop_bigrat) Gop_Dec() int { // error!
   423  	return 0
   424  }
   425  
   426  // Gop_Add: func (a bigrat) += (b bigrat)
   427  func (a Gop_bigrat) Gop_AddAssign(b Gop_bigrat) {
   428  	a.Rat.Add(a.Rat, b.Rat)
   429  }
   430  
   431  // Gop_Sub: func (a bigrat) -= (b bigrat) int
   432  func (a Gop_bigrat) Gop_SubAssign(b Gop_bigrat) int { // error!
   433  	return 0
   434  }
   435  
   436  // Gop_Mul: func (a bigrat) *= (b bigrat)
   437  func (a Gop_bigrat) Gop_MulAssign(b Gop_bigrat) {
   438  	a.Rat.Mul(a.Rat, b.Rat)
   439  }
   440  
   441  // Gop_Quo: func (a bigrat) /= (b bigrat)
   442  func (a Gop_bigrat) Gop_QuoAssign(b Gop_bigrat) {
   443  	a.Rat.Quo(a.Rat, b.Rat)
   444  }
   445  
   446  func (a Gop_bigrat) Gop_Rcast__0() (int, bool) {
   447  	return 0, false
   448  }
   449  
   450  func (a *Gop_bigrat) Gop_Rcast__1() int {
   451  	return 0
   452  }
   453  
   454  func (a *Gop_bigrat) Gop_Rcast__2() float64 {
   455  	return 0
   456  }
   457  
   458  func (a *Gop_bigrat) Gop_Rcast__3(int) int64 {
   459  	return 0
   460  }
   461  
   462  // Gop_bigrat_Cast: func bigrat(a untyped_bigint) bigrat
   463  func Gop_bigrat_Cast__0(a Gop_untyped_bigint) Gop_bigrat {
   464  	return Gop_bigrat{new(big.Rat).SetInt(a)}
   465  }
   466  
   467  // Gop_bigrat_Cast: func bigrat(a bigint) bigrat
   468  func Gop_bigrat_Cast__1(a Gop_bigint) Gop_bigrat {
   469  	return Gop_bigrat{new(big.Rat).SetInt(a.Int)}
   470  }
   471  
   472  // Gop_bigrat_Cast: func bigrat(a *big.Int) bigrat
   473  func Gop_bigrat_Cast__2(a *big.Int) Gop_bigrat {
   474  	return Gop_bigrat{new(big.Rat).SetInt(a)}
   475  }
   476  
   477  // Gop_bigrat_Cast: func bigrat(a, b int64) bigrat
   478  func Gop_bigrat_Cast__3(a, b int64) Gop_bigrat {
   479  	return Gop_bigrat{big.NewRat(a, b)}
   480  }
   481  
   482  // Gop_bigrat_Cast: func bigrat(a *big.Rat) bigrat
   483  func Gop_bigrat_Cast__4(a *big.Rat) Gop_bigrat {
   484  	return Gop_bigrat{a}
   485  }
   486  
   487  // Gop_bigrat_Cast: func bigrat() bigrat
   488  func Gop_bigrat_Cast__5() Gop_bigrat {
   489  	return Gop_bigrat{new(big.Rat)}
   490  }
   491  
   492  // Gop_bigrat_Init: func bigrat.init(x untyped_int) bigrat
   493  func Gop_bigrat_Init__0(x int) Gop_bigrat {
   494  	return Gop_bigrat{big.NewRat(int64(x), 1)}
   495  }
   496  
   497  // Gop_bigrat_Init: func bigrat.init(x untyped_bigint) bigrat
   498  func Gop_bigrat_Init__1(x Gop_untyped_bigint) Gop_bigrat {
   499  	return Gop_bigrat{new(big.Rat).SetInt(x)}
   500  }
   501  
   502  // Gop_bigrat_Init: func bigrat.init(x *big.Rat) bigrat
   503  func Gop_bigrat_Init__2(x *big.Rat) Gop_bigrat {
   504  	return Gop_bigrat{x}
   505  }
   506  
   507  // -----------------------------------------------------------------------------
   508  // type bigfloat
   509  
   510  // A Gop_bigfloat represents a multi-precision floating point number.
   511  // The zero value for a Gop_bigfloat represents nil.
   512  type Gop_bigfloat struct {
   513  	*big.Float
   514  }
   515  
   516  // -----------------------------------------------------------------------------