-
Notifications
You must be signed in to change notification settings - Fork 6
/
php_gmp.pir
668 lines (465 loc) · 10 KB
/
php_gmp.pir
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# Copyright (C) 2008, The Perl Foundation.
=head1 NAME
php_gmp.pir - PHP gmp Library
=head1 DESCRIPTION
Note:
Using BigInt is not the good way.
The good way is by a full NCI wrapper over the GMP library.
=head2 Functions
=over 4
=cut
.HLL 'pipp'
.include 'src/common/php_MACRO.pir'
.const string GMP_PMC = 'BigInt'
.const int GMP_ROUND_ZERO = 0
.const int GMP_ROUND_PLUSINF = 1
.const int GMP_ROUND_MINUSINF = 2
.sub '__init' :anon :load :init
.REGISTER_LONG_CONSTANT('GMP_ROUND_ZERO', GMP_ROUND_ZERO)
.REGISTER_LONG_CONSTANT('GMP_ROUND_PLUSINF', GMP_ROUND_PLUSINF)
.REGISTER_LONG_CONSTANT('GMP_ROUND_MINUSINF', GMP_ROUND_MINUSINF)
# $S0 =
# .REGISTER_STRING_CONSTANT('GMP_VERSION', $S0)
.end
.macro UNARY_OP(args, op)
.local int argc
argc = .args
unless argc != 1 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
$P1 = shift args
.local pmc gmpnum_a
gmpnum_a = fetch_resource($P1, GMP_PMC)
unless null gmpnum_a goto L2
.RETURN_FALSE()
L2:
new $P0, GMP_PMC
$P0 = .op gmpnum_a
.RETURN_RESOURCE($P0)
.endm
.macro BINARY_OP(args, op, allow_ui_return, check_b_zero)
$P1 = shift args
$P2 = shift args
.local pmc gmpnum_a
gmpnum_a = fetch_resource($P1, GMP_PMC)
unless null gmpnum_a goto L2
.RETURN_FALSE()
L2:
.local int use_ui
use_ui = 0
$I0 = isa $P2, 'PhpInteger'
unless $I0 goto L3
use_ui = 1
goto L4
L3:
.local pmc gmpnum_b
gmpnum_b = fetch_resource($P2, GMP_PMC)
unless null gmpnum_b goto L4
.RETURN_FALSE()
L4:
unless .check_b_zero goto L5
.local int b_is_zero
b_is_zero = 0
unless use_ui goto L6
$I2 = $P2
b_is_zero = iseq $I2, 0
goto L7
L6:
$I2 = gmpnum_b
b_is_zero = iseq $I2, 0
L7:
unless b_is_zero goto L5
error(E_WARNING, "Zero operand not allowed")
.RETURN_FALSE()
L5:
new $P0, GMP_PMC
unless use_ui goto L8
$I2 = $P2
$P0 = .op gmpnum_a, $I2
goto L9
L8:
$P0 = .op gmpnum_a, gmpnum_b
L9:
.RETURN_RESOURCE($P0)
.endm
=item C<resource gmp_abs(resource a)>
Calculates absolute value
=cut
.sub 'gmp_abs'
.param pmc args :slurpy
.UNARY_OP(args, neg)
.end
=item C<resource gmp_add(resource a, resource b)>
Add a and b
=cut
.sub 'gmp_add'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, add, 0, 0)
.end
=item C<resource gmp_and(resource a, resource b)>
Calculates logical AND of a and b
=cut
.sub 'gmp_and'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, band, 0, 0) # not working
.end
=item C<void gmp_clrbit(resource &a, int index)>
Clears bit in a
NOT IMPLEMENTED.
=cut
.sub 'gmp_clrbit'
not_implemented()
.end
=item C<int gmp_cmp(resource a, resource b)>
Compares two numbers
=cut
.sub 'gmp_cmp'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
$P1 = shift args
$P2 = shift args
.local pmc gmpnum_a
gmpnum_a = fetch_resource($P1, GMP_PMC)
unless null gmpnum_a goto L2
.RETURN_FALSE()
L2:
.local int use_ui
use_ui = 0
$I0 = isa $P2, 'PhpInteger'
unless $I0 goto L3
use_ui = 1
goto L4
L3:
.local pmc gmpnum_b
gmpnum_b = fetch_resource($P2, GMP_PMC)
unless null gmpnum_b goto L4
.RETURN_FALSE()
L4:
unless use_ui goto L5
$I2 = $P2
$I0 = cmp gmpnum_a, $I2
goto L6
L5:
$I0 = cmp gmpnum_a, gmpnum_b
L6:
.RETURN_LONG($I0)
.end
=item C<resource gmp_com(resource a)>
Calculates one's complement of a
NOT IMPLEMENTED.
=cut
.sub 'gmp_com'
not_implemented()
.end
=item C<resource gmp_div_q(resource a, resource b [, int round])>
Divide a by b, returns quotient only
NOT IMPLEMENTED.
=cut
.sub 'gmp_div_q'
not_implemented()
.end
=item C<array gmp_div_qr(resource a, resource b [, int round])>
Divide a by b, returns quotient and reminder
NOT IMPLEMENTED.
=cut
.sub 'gmp_div_qr'
not_implemented()
.end
=item C<resource gmp_div_r(resource a, resource b [, int round])>
Divide a by b, returns reminder only
NOT IMPLEMENTED.
=cut
.sub 'gmp_div_r'
not_implemented()
.end
=item C<resource gmp_divexact(resource a, resource b)>
Divide a by b using exact division algorithm
NOT IMPLEMENTED.
=cut
.sub 'gmp_divexact'
not_implemented()
.end
=item C<resource gmp_fact(int a)>
Calculates factorial function
NOT IMPLEMENTED.
=cut
.sub 'gmp_fact'
not_implemented()
.end
=item C<resource gmp_gcd(resource a, resource b)>
Computes greatest common denominator (gcd) of a and b
NOT IMPLEMENTED.
=cut
.sub 'gmp_gcd'
not_implemented()
.end
=item C<array gmp_gcdext(resource a, resource b)>
Computes G, S, and T, such that AS + BT = G = `gcd' (A, B)
NOT IMPLEMENTED.
=cut
.sub 'gmp_gcdext'
not_implemented()
.end
=item C<int gmp_hamdist(resource a, resource b)>
Calculates hamming distance between a and b
NOT IMPLEMENTED.
=cut
.sub 'gmp_hamdist'
not_implemented()
.end
=item C<resource gmp_init(mixed number [, int base])>
Initializes GMP number
STILL INCOMPLETE.
=cut
.sub 'gmp_init'
.param pmc args :slurpy
.local int argc
argc = args
if argc < 1 goto L1
if argc > 2 goto L1
goto L2
L1:
wrong_param_count()
.RETURN_NULL()
L2:
$P1 = shift args
$I1 = $P1
new $P0, GMP_PMC
set $P0, $I1
.RETURN_RESOURCE($P0)
.end
=item C<int gmp_intval(resource gmpnumber)>
Gets signed long value of GMP number
=cut
.sub 'gmp_intval'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 1 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
$P1 = shift args
$I0 = isa $P1, 'PhpResource'
unless $I0 goto L2
.local pmc gmpnum
gmpnum = fetch_resource($P1, GMP_PMC)
unless null gmpnum goto L3
.RETURN_FALSE()
L3:
$I0 = gmpnum
.RETURN_LONG($I0)
L2:
$I0 = $P1
.RETURN_LONG($I0)
.end
=item C<resource gmp_invert(resource a, resource b)>
Computes the inverse of a modulo b
NOT IMPLEMENTED.
=cut
.sub 'gmp_invert'
not_implemented()
.end
=item C<int gmp_jacobi(resource a, resource b)>
Computes Jacobi symbol
NOT IMPLEMENTED.
=cut
.sub 'gmp_jacobi'
not_implemented()
.end
=item C<int gmp_legendre(resource a, resource b)>
Computes Legendre symbol
NOT IMPLEMENTED.
=cut
.sub 'gmp_legendre'
not_implemented()
.end
=item C<resource gmp_mod(resource a, resource b)>
Computes a modulo b
=cut
.sub 'gmp_mod'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, mod, 1, 1)
.end
=item C<resource gmp_mul(resource a, resource b)>
Multiply a and b
=cut
.sub 'gmp_mul'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, mul, 0, 0)
.end
=item C<resource gmp_neg(resource a)>
Negates a number
=cut
.sub 'gmp_neg'
.param pmc args :slurpy
.UNARY_OP(args, neg)
.end
=item C<resource gmp_nextprime(resource a)>
Finds next prime of a
NOT IMPLEMENTED.
=cut
.sub 'gmp_nextprime'
not_implemented()
.end
=item C<resource gmp_or(resource a, resource b)>
Calculates logical OR of a and b
=cut
.sub 'gmp_or'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, bor, 0, 0) # not working
.end
=item C<bool gmp_perfect_square(resource a)>
Checks if a is an exact square
NOT IMPLEMENTED.
=cut
.sub 'gmp_perfect_square'
not_implemented()
.end
=item C<int gmp_popcount(resource a)>
Calculates the population count of a
NOT IMPLEMENTED.
=cut
.sub 'gmp_popcount'
not_implemented()
.end
=item C<resource gmp_pow(resource base, int exp)>
Raise base to power exp
NOT IMPLEMENTED.
=cut
.sub 'gmp_pow'
not_implemented()
.end
=item C<resource gmp_powm(resource base, resource exp, resource mod)>
Raise base to power exp and take result modulo mod
NOT IMPLEMENTED.
=cut
.sub 'gmp_powm'
not_implemented()
.end
=item C<int gmp_prob_prime(resource a[, int reps])>
Checks if a is "probably prime"
NOT IMPLEMENTED.
=cut
.sub 'gmp_prob_prime'
not_implemented()
.end
=item C<resource gmp_random([int limiter])>
Gets random number
NOT IMPLEMENTED.
=cut
.sub 'gmp_random'
not_implemented()
.end
=item C<int gmp_scan0(resource a, int start)>
Finds first zero bit
NOT IMPLEMENTED.
=cut
.sub 'gmp_scan0'
not_implemented()
.end
=item C<int gmp_scan1(resource a, int start)>
Finds first non-zero bit
NOT IMPLEMENTED.
=cut
.sub 'gmp_scan1'
not_implemented()
.end
=item C<void gmp_setbit(resource &a, int index[, bool set_clear])>
Sets or clear bit in a
NOT IMPLEMENTED.
=cut
.sub 'gmp_setbit'
not_implemented()
.end
=item C<int gmp_sign(resource a)>
Gets the sign of the number
NOT IMPLEMENTED.
=cut
.sub 'gmp_sign'
not_implemented()
.end
=item C<resource gmp_sqrt(resource a)>
Takes integer part of square root of a
NOT IMPLEMENTED.
=cut
.sub 'gmp_sqrt'
not_implemented()
.end
=item C<array gmp_sqrtrem(resource a)>
Square root with remainder
NOT IMPLEMENTED.
=cut
.sub 'gmp_sqrtrem'
not_implemented()
.end
=item C<string gmp_strval(resource gmpnumber [, int base])>
Gets string representation of GMP number
NOT IMPLEMENTED.
=cut
.sub 'gmp_strval'
not_implemented()
.end
=item C<resource gmp_sub(resource a, resource b)>
Subtract b from a
=cut
.sub 'gmp_sub'
.param pmc args :slurpy
.local int argc
argc = args
unless argc != 2 goto L1
wrong_param_count()
.RETURN_NULL()
L1:
.BINARY_OP(args, sub, 0, 0)
.end
=item C<resource gmp_xor(resource a, resource b)>
Calculates logical exclusive OR of a and b
NOT IMPLEMENTED.
=cut
.sub 'gmp_xor'
not_implemented()
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: