raisemod.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details. (LICENSE)
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to:
  15. The Free Software Foundation, Inc.
  16. 59 Temple Place, Suite 330
  17. Boston, MA 02111-1307 USA.
  18. You may contact the author by:
  19. e-mail: philnelson@acm.org
  20. us-mail: Philip A. Nelson
  21. Computer Science Department, 9062
  22. Western Washington University
  23. Bellingham, WA 98226-9062
  24. *************************************************************************/
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <stdarg.h>
  30. #include "bcmath.h"
  31. #include "private.h"
  32. #include "zend_exceptions.h"
  33. /* Raise BASE to the EXPO power, reduced modulo MOD. The result is placed in RESULT. */
  34. zend_result bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
  35. {
  36. bc_num power, exponent, modulus, parity, temp;
  37. int rscale;
  38. /* Check the base for scale digits. */
  39. if (base->n_scale != 0) {
  40. /* 1st argument from PHP_FUNCTION(bcpowmod) */
  41. zend_argument_value_error(1, "cannot have a fractional part");
  42. return FAILURE;
  43. }
  44. /* Check the exponent for scale digits. */
  45. if (expo->n_scale != 0) {
  46. /* 2nd argument from PHP_FUNCTION(bcpowmod) */
  47. zend_argument_value_error(2, "cannot have a fractional part");
  48. return FAILURE;
  49. }
  50. if (bc_is_neg(expo)) {
  51. zend_argument_value_error(2, "must be greater than or equal to 0");
  52. return FAILURE;
  53. }
  54. /* Check the modulus for scale digits. */
  55. if (mod->n_scale != 0) {
  56. /* 3rd argument from PHP_FUNCTION(bcpowmod) */
  57. zend_argument_value_error(3, "cannot have a fractional part");
  58. return FAILURE;
  59. }
  60. /* Modulus cannot be 0 */
  61. if (bc_is_zero(mod)) {
  62. zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
  63. return FAILURE;
  64. }
  65. /* Set initial values. */
  66. power = bc_copy_num (base);
  67. exponent = bc_copy_num (expo);
  68. modulus = bc_copy_num (mod);
  69. temp = bc_copy_num (BCG(_one_));
  70. bc_init_num(&parity);
  71. /* Do the calculation. */
  72. rscale = MAX(scale, power->n_scale);
  73. if ( !bc_compare(modulus, BCG(_one_)) )
  74. {
  75. bc_free_num (&temp);
  76. temp = bc_new_num (1, scale);
  77. }
  78. else
  79. {
  80. while ( !bc_is_zero(exponent) )
  81. {
  82. (void) bc_divmod (exponent, BCG(_two_), &exponent, &parity, 0);
  83. if ( !bc_is_zero(parity) )
  84. {
  85. bc_multiply (temp, power, &temp, rscale);
  86. (void) bc_modulo (temp, modulus, &temp, scale);
  87. }
  88. bc_multiply (power, power, &power, rscale);
  89. (void) bc_modulo (power, modulus, &power, scale);
  90. }
  91. }
  92. /* Assign the value. */
  93. bc_free_num (&power);
  94. bc_free_num (&exponent);
  95. bc_free_num (&modulus);
  96. bc_free_num (result);
  97. bc_free_num (&parity);
  98. *result = temp;
  99. return SUCCESS; /* Everything is OK. */
  100. }