1 /*
2  * gauche/bignum.h - Internal API for bignums
3  *
4  *   Copyright (c) 2000-2020  Shiro Kawai  <shiro@acm.org>
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *   1. Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  *   3. Neither the name of the authors nor the names of its contributors
18  *      may be used to endorse or promote products derived from this
19  *      software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27  *   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  *   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* APIs concerning bignums.  They are not for general public use,
35    so separated to here. */
36 
37 #ifndef GAUCHE_BIGNUM_H
38 #define GAUCHE_BIGNUM_H
39 
40 SCM_EXTERN ScmObj Scm_MakeBignumFromSI(long val);
41 SCM_EXTERN ScmObj Scm_MakeBignumFromUI(u_long val);
42 SCM_EXTERN ScmObj Scm_MakeBignumFromUIArray(int sign,
43                                             const u_long *values,
44                                             int size);
45 SCM_EXTERN ScmObj Scm_MakeBignumFromDouble(double val);
46 SCM_EXTERN ScmObj Scm_BignumCopy(const ScmBignum *b);
47 SCM_EXTERN ScmObj Scm_BignumToString(const ScmBignum *b, int radix,
48                                      int use_upper);
49 
50 SCM_EXTERN long   Scm_BignumToSI(const ScmBignum *b, int clamp, int* oor);
51 SCM_EXTERN u_long Scm_BignumToUI(const ScmBignum *b, int clamp, int* oor);
52 #if SIZEOF_LONG == 4
53 SCM_EXTERN int64_t  Scm_BignumToSI64(const ScmBignum *b, int clamp, int *oor);
54 SCM_EXTERN uint64_t Scm_BignumToUI64(const ScmBignum *b, int clamp, int *oor);
55 #else  /* SIZEOF_LONG >= 8 */
56 #define Scm_BignumToSI64       Scm_BignumToSI
57 #define Scm_BignumToUI64       Scm_BignumToUI
58 #endif /* SIZEOF_LONG >= 8 */
59 SCM_EXTERN double Scm_BignumToDouble(const ScmBignum *b);
60 SCM_EXTERN ScmObj Scm_NormalizeBignum(ScmBignum *b);
61 SCM_EXTERN ScmObj Scm_BignumNegate(const ScmBignum *b);
62 SCM_EXTERN int    Scm_BignumCmp(const ScmBignum *bx, const ScmBignum *by);
63 SCM_EXTERN int    Scm_BignumAbsCmp(const ScmBignum *bx, const ScmBignum *by);
64 SCM_EXTERN int    Scm_BignumCmp3U(const ScmBignum *bx,
65                                   const ScmBignum *off,
66                                   const ScmBignum *by);
67 SCM_EXTERN ScmObj Scm_BignumComplement(const ScmBignum *bx);
68 
69 SCM_EXTERN ScmObj Scm_BignumAdd(const ScmBignum *bx, const ScmBignum *by);
70 SCM_EXTERN ScmObj Scm_BignumAddSI(const ScmBignum *bx, long y);
71 SCM_EXTERN ScmObj Scm_BignumSub(const ScmBignum *bx, const ScmBignum *by);
72 SCM_EXTERN ScmObj Scm_BignumSubSI(const ScmBignum *bx, long y);
73 SCM_EXTERN ScmObj Scm_BignumMul(const ScmBignum *bx, const ScmBignum *by);
74 SCM_EXTERN ScmObj Scm_BignumMulSI(const ScmBignum *bx, long y);
75 SCM_EXTERN ScmObj Scm_BignumDivSI(const ScmBignum *bx, long y, long *r);
76 SCM_EXTERN ScmObj Scm_BignumDivRem(const ScmBignum *bx, const ScmBignum *by);
77 SCM_EXTERN long   Scm_BignumRemSI(const ScmBignum *bx, long y);
78 
79 SCM_EXTERN ScmObj Scm_BignumLogAnd(const ScmBignum *bx, const ScmBignum *by);
80 SCM_EXTERN ScmObj Scm_BignumLogIor(const ScmBignum *bx, const ScmBignum *by);
81 SCM_EXTERN ScmObj Scm_BignumLogXor(const ScmBignum *bx, const ScmBignum *by);
82 SCM_EXTERN ScmObj Scm_BignumLogNot(const ScmBignum *bx);
83 SCM_EXTERN int    Scm_BignumLogCount(const ScmBignum *b);
84 SCM_EXTERN ScmObj Scm_BignumAsh(const ScmBignum *bx, int cnt);
85 
86 SCM_EXTERN ScmBignum *Scm_MakeBignumWithSize(int size, u_long init);
87 SCM_EXTERN ScmBignum *Scm_BignumAccMultAddUI(ScmBignum *acc,
88                                              u_long coef, u_long c);
89 
90 SCM_EXTERN int Scm_DumpBignum(const ScmBignum *b, ScmPort *out);
91 
92 #endif /* GAUCHE_BIGNUM_H */
93 
94