xref: /PHP-7.4/ext/bcmath/libbcmath/src/raisemod.c (revision f781c8f1)
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.  (LICENSE)
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 <stdlib.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include "bcmath.h"
38 #include "private.h"
39 
40 
41 /* Truncate a number to zero scale.  To avoid sharing issues (refcount and
42    shared n_value) the number is copied, this copy is truncated, and the
43    original number is "freed". */
44 
45 static void
_bc_truncate(bc_num * num)46 _bc_truncate (bc_num *num)
47 {
48   bc_num temp;
49 
50   temp = bc_new_num ((*num)->n_len, 0);
51   temp->n_sign = (*num)->n_sign;
52   memcpy (temp->n_value, (*num)->n_value, (*num)->n_len);
53   bc_free_num (num);
54   *num = temp;
55 }
56 
57 
58 /* Raise BASE to the EXPO power, reduced modulo MOD.  The result is
59    placed in RESULT.  If a EXPO is not an integer,
60    only the integer part is used.  */
61 
62 int
bc_raisemod(bc_num base,bc_num expo,bc_num mod,bc_num * result,int scale)63 bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
64 {
65   bc_num power, exponent, modulus, parity, temp;
66   int rscale;
67 
68   /* Check for correct numbers. */
69   if (bc_is_zero(mod)) return -1;
70   if (bc_is_neg(expo)) return -1;
71 
72   /* Set initial values.  */
73   power = bc_copy_num (base);
74   exponent = bc_copy_num (expo);
75   modulus = bc_copy_num (mod);
76   temp = bc_copy_num (BCG(_one_));
77   bc_init_num(&parity);
78 
79   /* Check the base for scale digits. */
80   if (power->n_scale != 0)
81     {
82       php_error_docref (NULL, E_WARNING, "non-zero scale in base");
83       _bc_truncate (&power);
84     }
85 
86   /* Check the exponent for scale digits. */
87   if (exponent->n_scale != 0)
88     {
89       php_error_docref (NULL, E_WARNING, "non-zero scale in exponent");
90       _bc_truncate (&exponent);
91     }
92 
93   /* Check the modulus for scale digits. */
94   if (modulus->n_scale != 0)
95     {
96       php_error_docref (NULL, E_WARNING, "non-zero scale in modulus");
97       _bc_truncate (&modulus);
98     }
99 
100   /* Do the calculation. */
101   rscale = MAX(scale, power->n_scale);
102   if ( !bc_compare(modulus, BCG(_one_)) )
103     {
104       bc_free_num (&temp);
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