1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
|| $Id: bignum.c,v 1.32 2003/01/27 10:18:25 mirar Exp $ 
*/ 
 
#include "global.h" 
 
#ifdef AUTO_BIGNUM 
 
#include "interpret.h" 
#include "program.h" 
#include "object.h" 
#include "svalue.h" 
#include "pike_error.h" 
 
#define sp Pike_sp 
 
PMOD_EXPORT struct svalue auto_bignum_program = { 
  T_INT, 0, 
#ifdef HAVE_UNION_INIT 
  {0}, /* Only to avoid warnings. */ 
#endif 
}; 
 
PMOD_EXPORT struct program *get_auto_bignum_program(void) 
{ 
  return program_from_function(&auto_bignum_program); 
} 
 
PMOD_EXPORT struct program *get_auto_bignum_program_or_zero(void) 
{ 
  if (auto_bignum_program.type == T_INT) 
    return 0; 
  return program_from_function(&auto_bignum_program); 
} 
 
void exit_auto_bignum(void) 
{ 
  free_svalue(&auto_bignum_program); 
  auto_bignum_program.type=T_INT; 
} 
 
PMOD_EXPORT void convert_stack_top_to_bignum(void) 
{ 
  apply_svalue(&auto_bignum_program, 1); 
 
  if(sp[-1].type != T_OBJECT) 
     if (auto_bignum_program.type!=T_PROGRAM) 
        Pike_error("Gmp.mpz conversion failed (Gmp.bignum not loaded).\n"); 
     else 
        Pike_error("Gmp.mpz conversion failed (unknown error).\n"); 
} 
 
PMOD_EXPORT void convert_stack_top_with_base_to_bignum(void) 
{ 
  apply_svalue(&auto_bignum_program, 2); 
 
  if(sp[-1].type != T_OBJECT) 
     if (auto_bignum_program.type!=T_PROGRAM) 
        Pike_error("Gmp.mpz conversion failed (Gmp.bignum not loaded).\n"); 
     else 
        Pike_error("Gmp.mpz conversion failed (unknown error).\n"); 
} 
 
int is_bignum_object(struct object *o) 
{ 
  /* Note: 
   * This function should *NOT* try to resolv Gmp.mpz unless 
   * it is already loaded into memory. 
   * /Hubbe 
   */ 
 
  if (auto_bignum_program.type == T_INT) 
    return 0; /* not possible */ 
  
  return o->prog == program_from_svalue(&auto_bignum_program); 
} 
 
PMOD_EXPORT int is_bignum_object_in_svalue(struct svalue *sv) 
{ 
  return sv->type == T_OBJECT && is_bignum_object(sv->u.object); 
} 
 
PMOD_EXPORT struct object *make_bignum_object(void) 
{ 
  convert_stack_top_to_bignum(); 
  return (--sp)->u.object; 
} 
 
PMOD_EXPORT struct object *bignum_from_svalue(struct svalue *s) 
{ 
  push_svalue(s); 
  convert_stack_top_to_bignum(); 
  return (--sp)->u.object; 
} 
 
PMOD_EXPORT struct pike_string *string_from_bignum(struct object *o, int base) 
{ 
  push_int(base); 
  safe_apply(o, "digits", 1); 
   
  if(sp[-1].type != T_STRING) 
    Pike_error("Gmp.mpz string conversion failed.\n"); 
   
  return (--sp)->u.string; 
} 
 
PMOD_EXPORT void convert_svalue_to_bignum(struct svalue *s) 
{ 
  push_svalue(s); 
  convert_stack_top_to_bignum(); 
  free_svalue(s); 
  *s=sp[-1]; 
  sp--; 
  dmalloc_touch_svalue(sp); 
} 
 
#ifdef INT64 
 
PMOD_EXPORT void push_int64(INT64 i) 
{ 
  if(i == DO_NOT_WARN((INT_TYPE)i)) 
  { 
    push_int(DO_NOT_WARN((INT_TYPE)i)); 
  } 
  else 
  { 
    unsigned int neg = 0; 
    if( i < 0 ) 
    { 
      i = -i; 
      neg = 1; 
    } 
 
#if PIKE_BYTEORDER == 1234 
    { 
      char digits[8]; 
      char *ledigits = (char *)&i; 
      digits[7] = ledigits[ 0 ];      digits[6] = ledigits[ 1 ]; 
      digits[5] = ledigits[ 2 ];      digits[4] = ledigits[ 3 ]; 
      digits[3] = ledigits[ 4 ];      digits[2] = ledigits[ 5 ]; 
      digits[1] = ledigits[ 6 ];      digits[0] = ledigits[ 7 ]; 
      push_string( make_shared_binary_string( digits, 8 ) ); 
    } 
#else 
    push_string( make_shared_binary_string( (char *)&i, 8 ) ); 
#endif 
    push_int( 256 ); 
    apply_svalue(&auto_bignum_program, 2); 
 
 
    if(neg) 
      apply_low(sp[-1].u.object,FIND_LFUN(sp[-1].u.object->prog,LFUN_COMPL),0); 
  } 
} 
 
/* This routines can be optimized quite drastically. */ 
#define BIGNUM_INT64_MASK  0xffffff 
#define BIGNUM_INT64_SHIFT 24 
PMOD_EXPORT int int64_from_bignum(INT64 *i, struct object *bignum) 
{ 
  int neg, pos, rshfun, andfun; 
 
  *i = 0; 
 
  push_int(0); 
  apply_low(bignum, FIND_LFUN(bignum->prog, LFUN_LT), 1); 
  if(sp[-1].type != T_INT) 
    Pike_error("Result from Gmp.bignum->`< not an integer.\n"); 
  neg = (--sp)->u.integer; 
 
  if(neg) 
    apply_low(bignum, FIND_LFUN(bignum->prog, LFUN_COMPL), 0); 
 
  rshfun = FIND_LFUN(bignum->prog, LFUN_RSH); 
  andfun = FIND_LFUN(bignum->prog, LFUN_AND); 
   
  ref_push_object(bignum); 
     
  for(pos = 0; sp[-1].type != T_INT; ) 
  { 
    push_int(BIGNUM_INT64_MASK); 
    apply_low(sp[-2].u.object, andfun, 1); 
    if(sp[-1].type != T_INT) 
      Pike_error("Result from Gmp.bignum->`& not an integer.\n"); 
    *i |= (INT64)(--sp)->u.integer << (INT64)pos; 
    pos += BIGNUM_INT64_SHIFT; 
     
    push_int(BIGNUM_INT64_SHIFT); 
    apply_low(sp[-2].u.object, rshfun, 1); 
    stack_swap(); 
    pop_stack(); 
  } 
   
  *i |= (INT64)(--sp)->u.integer << (INT64)pos; 
 
  if(neg) 
    *i = ~*i; 
   
  return 1;   /* We may someday return 0 if the conversion fails. */ 
} 
#endif /* INT64 */ 
 
#endif /* AUTO_BIGNUM */