1 /* raisemod.c: bcmath library file. */
2 /*
3 Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4 Copyright (C) 2000 Philip A. Nelson
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details. (COPYING.LIB)
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to:
18
19 The Free Software Foundation, Inc.
20 59 Temple Place, Suite 330
21 Boston, MA 02111-1307 USA.
22
23 You may contact the author by:
24 e-mail: philnelson@acm.org
25 us-mail: Philip A. Nelson
26 Computer Science Department, 9062
27 Western Washington University
28 Bellingham, WA 98226-9062
29
30 *************************************************************************/
31
32 #include <config.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <stdarg.h>
38 #include "bcmath.h"
39 #include "private.h"
40
41
42 /* Truncate a number to zero scale. To avoid sharing issues (refcount and
43 shared n_value) the number is copied, this copy is truncated, and the
44 original number is "freed". */
45
46 static void
_bc_truncate(bc_num * num)47 _bc_truncate (bc_num *num)
48 {
49 bc_num temp;
50
51 temp = bc_new_num ((*num)->n_len, 0);
52 temp->n_sign = (*num)->n_sign;
53 memcpy (temp->n_value, (*num)->n_value, (*num)->n_len);
54 bc_free_num (num);
55 *num = temp;
56 }
57
58
59 /* Raise BASE to the EXPO power, reduced modulo MOD. The result is
60 placed in RESULT. If a EXPO is not an integer,
61 only the integer part is used. */
62
63 int
bc_raisemod(bc_num base,bc_num expo,bc_num mod,bc_num * result,int scale)64 bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
65 {
66 bc_num power, exponent, modulus, parity, temp;
67 int rscale;
68
69 /* Check for correct numbers. */
70 if (bc_is_zero(mod)) return -1;
71 if (bc_is_neg(expo)) return -1;
72
73 /* Set initial values. */
74 power = bc_copy_num (base);
75 exponent = bc_copy_num (expo);
76 modulus = bc_copy_num (mod);
77 temp = bc_copy_num (BCG(_one_));
78 bc_init_num(&parity);
79
80 /* Check the base for scale digits. */
81 if (power->n_scale != 0)
82 {
83 php_error_docref (NULL, E_WARNING, "non-zero scale in base");
84 _bc_truncate (&power);
85 }
86
87 /* Check the exponent for scale digits. */
88 if (exponent->n_scale != 0)
89 {
90 php_error_docref (NULL, E_WARNING, "non-zero scale in exponent");
91 _bc_truncate (&exponent);
92 }
93
94 /* Check the modulus for scale digits. */
95 if (modulus->n_scale != 0)
96 {
97 php_error_docref (NULL, E_WARNING, "non-zero scale in modulus");
98 _bc_truncate (&modulus);
99 }
100
101 /* Do the calculation. */
102 rscale = MAX(scale, power->n_scale);
103 if ( !bc_compare(modulus, BCG(_one_)) )
104 {
105 temp = bc_new_num (1, scale);
106 }
107 else
108 {
109 while ( !bc_is_zero(exponent) )
110 {
111 (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
112 if ( !bc_is_zero(parity) )
113 {
114 bc_multiply (temp, power, &temp, rscale);
115 (void) bc_modulo (temp, modulus, &temp, scale);
116 }
117
118 bc_multiply (power, power, &power, rscale);
119 (void) bc_modulo (power, modulus, &power, scale);
120 }
121 }
122
123 /* Assign the value. */
124 bc_free_num (&power);
125 bc_free_num (&exponent);
126 bc_free_num (&modulus);
127 bc_free_num (result);
128 bc_free_num (&parity);
129 *result = temp;
130 return 0; /* Everything is OK. */
131 }
132