xref: /PHP-8.0/ext/bcmath/libbcmath/src/raisemod.c (revision cd05b56a)
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 #include "zend_exceptions.h"
40 
41 /* Raise BASE to the EXPO power, reduced modulo MOD.  The result is placed in RESULT. */
bc_raisemod(bc_num base,bc_num expo,bc_num mod,bc_num * result,int scale)42 zend_result bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
43 {
44   bc_num power, exponent, modulus, parity, temp;
45   int rscale;
46 
47 	/* Check the base for scale digits. */
48 	if (base->n_scale != 0) {
49 		/* 1st argument from PHP_FUNCTION(bcpowmod) */
50 		zend_argument_value_error(1, "cannot have a fractional part");
51 		return FAILURE;
52     }
53 	/* Check the exponent for scale digits. */
54 	if (expo->n_scale != 0) {
55 		/* 2nd argument from PHP_FUNCTION(bcpowmod) */
56 		zend_argument_value_error(2, "cannot have a fractional part");
57 		return FAILURE;
58     }
59 	if (bc_is_neg(expo)) {
60 		zend_argument_value_error(2, "must be greater than or equal to 0");
61 		return FAILURE;
62 	}
63 	/* Check the modulus for scale digits. */
64 	if (mod->n_scale != 0) {
65 		/* 3rd argument from PHP_FUNCTION(bcpowmod) */
66 		zend_argument_value_error(3, "cannot have a fractional part");
67 		return FAILURE;
68     }
69     /* Modulus cannot be 0 */
70 	if (bc_is_zero(mod)) {
71 		zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
72 		return FAILURE;
73 	}
74 
75   /* Set initial values.  */
76   power = bc_copy_num (base);
77   exponent = bc_copy_num (expo);
78   modulus = bc_copy_num (mod);
79   temp = bc_copy_num (BCG(_one_));
80   bc_init_num(&parity);
81 
82   /* Do the calculation. */
83   rscale = MAX(scale, power->n_scale);
84   if ( !bc_compare(modulus, BCG(_one_)) )
85     {
86       bc_free_num (&temp);
87       temp = bc_new_num (1, scale);
88     }
89   else
90     {
91       while ( !bc_is_zero(exponent) )
92 	{
93 	  (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
94 	  if ( !bc_is_zero(parity) )
95 	    {
96 	      bc_multiply (temp, power, &temp, rscale);
97 	      (void) bc_modulo (temp, modulus, &temp, scale);
98 	    }
99 
100 	  bc_multiply (power, power, &power, rscale);
101 	  (void) bc_modulo (power, modulus, &power, scale);
102 	}
103     }
104 
105   /* Assign the value. */
106   bc_free_num (&power);
107   bc_free_num (&exponent);
108   bc_free_num (&modulus);
109   bc_free_num (result);
110   bc_free_num (&parity);
111   *result = temp;
112   return SUCCESS;	/* Everything is OK. */
113 }
114