MPMATH.H 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /VSS_Sync/wwlib/mpmath.h $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 3/21/01 12:01p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef MPMATH_H
  39. #define MPMATH_H
  40. #include "bool.h"
  41. //lint -e740 -e534 -e537 -e760
  42. //lint -d_LINT=1
  43. #include "straw.h"
  44. #include <stdlib.h>
  45. #define UNITSIZE 32
  46. #define MAX_BIT_PRECISION 2048
  47. #define MAX_UNIT_PRECISION (MAX_BIT_PRECISION/UNITSIZE)
  48. #define signeddigit signed long
  49. #define LOG_UNITSIZE 5
  50. //#define digit unsigned long
  51. typedef unsigned long digit;
  52. #define MPEXPORT
  53. #ifdef _MSC_VER
  54. #pragma warning( disable : 4236)
  55. #endif
  56. extern "C" {
  57. extern int MPEXPORT XMP_Fetch_Prime_Size(void);
  58. unsigned short const * MPEXPORT XMP_Fetch_Prime_Table(void);
  59. int MPEXPORT XMP_Significance(const digit * r, int precision);
  60. void MPEXPORT XMP_Inc(digit * r, int precision);
  61. void MPEXPORT XMP_Dec(digit * r, int precision);
  62. void MPEXPORT XMP_Neg(digit * r, int precision);
  63. void MPEXPORT XMP_Abs(digit * r, int precision);
  64. void MPEXPORT XMP_Shift_Right_Bits(digit * r1, int bits, int precision);
  65. void MPEXPORT XMP_Shift_Left_Bits(digit * r1, int bits, int precision);
  66. bool MPEXPORT XMP_Rotate_Left(digit * r1, bool carry, int precision);
  67. void MPEXPORT XMP_Not(digit * digit_ptr, int precision);
  68. int MPEXPORT XMP_Prepare_Modulus(const digit * n_modulus, int precision);
  69. void MPEXPORT XMP_Init(digit * r, digit value, int precision);
  70. unsigned MPEXPORT XMP_Count_Bits(const digit * r, int precision);
  71. int MPEXPORT XMP_Count_Bytes(const digit * r, int precision);
  72. void MPEXPORT XMP_Move(digit * dest, digit const * source, int precision);
  73. int MPEXPORT XMP_Digits_To_Bits(int digits);
  74. int MPEXPORT XMP_Compare(const digit * r1, const digit * r2, int precision);
  75. int MPEXPORT XMP_Bits_To_Digits(int bits);
  76. bool MPEXPORT XMP_Add(digit * result, const digit * r1, const digit * r2, bool carry, int precision);
  77. bool MPEXPORT XMP_Add_Int(digit * result, const digit * r1, digit r2, bool carry, int precision);
  78. bool MPEXPORT XMP_Test_Bit(const digit * r, unsigned bit);
  79. void MPEXPORT XMP_Set_Bit(digit * r, unsigned bit);
  80. digit MPEXPORT XMP_Bits_To_Mask(int bits);
  81. bool MPEXPORT XMP_Is_Negative(const digit * r, int precision);
  82. bool MPEXPORT XMP_Test_Eq_Int(digit const * r, int i, int p);
  83. bool MPEXPORT XMP_Sub(digit * result, const digit * r1, const digit * r2, bool borrow, int precision);
  84. bool MPEXPORT XMP_Sub_Int(digit * result, const digit * r1, unsigned short r2, bool borrow, int precision);
  85. int MPEXPORT XMP_Unsigned_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  86. int MPEXPORT XMP_Unsigned_Mult_Int(digit * prod, const digit * multiplicand, short multiplier, int precision);
  87. int MPEXPORT XMP_Signed_Mult_Int(digit * prod, const digit * multiplicand, signed short multiplier, int precision);
  88. int MPEXPORT XMP_Signed_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  89. unsigned short MPEXPORT XMP_Unsigned_Div_Int(digit * quotient, digit const * dividend, unsigned short divisor, int precision);
  90. int MPEXPORT XMP_Unsigned_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision);
  91. void MPEXPORT XMP_Signed_Div(digit * remainder, digit * quotient, digit const * dividend, digit const * divisor, int precision);
  92. int MPEXPORT XMP_Reciprocal(digit * quotient, const digit * divisor, int precision);
  93. void MPEXPORT XMP_Decode_ASCII(char const * str, digit * mpn, int precision);
  94. //void MPEXPORT xmp_single_mul(unsigned short * prod, unsigned short * multiplicand, unsigned short multiplier, int precision);
  95. void MPEXPORT XMP_Double_Mul(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  96. //int MPEXPORT xmp_stage_modulus(const digit * n_modulus, int precision);
  97. int MPEXPORT XMP_Mod_Mult(digit * prod, const digit * multiplicand, const digit * multiplier, int precision);
  98. void MPEXPORT XMP_Mod_Mult_Clear(int precision);
  99. int MPEXPORT XMP_Exponent_Mod(digit * expout, const digit * expin, const digit * exponent_ptr, const digit * modulus, int precision);
  100. bool MPEXPORT XMP_Is_Small_Prime(const digit * candidate, int precision);
  101. bool MPEXPORT XMP_Small_Divisors_Test(const digit * candidate, int precision);
  102. bool MPEXPORT XMP_Fermat_Test(const digit * candidate_prime, unsigned rounds, int precision);
  103. void MPEXPORT XMP_Inverse_A_Mod_B(digit * result, digit const * number, digit const * modulus, int precision);
  104. void MPEXPORT XMP_Signed_Decode(digit * result, const unsigned char * from, int frombytes, int precision);
  105. void MPEXPORT XMP_Unsigned_Decode(digit * result, const unsigned char * from, int frombytes, int precision);
  106. unsigned MPEXPORT XMP_Encode(unsigned char * to, digit const * from, int precision);
  107. unsigned MPEXPORT XMP_Encode_Bounded(unsigned char * to, unsigned tobytes, digit const * from, int precision);
  108. void MPEXPORT XMP_Randomize(digit * result, Straw & rng, int nbits, int precision);
  109. void MPEXPORT XMP_Randomize_Bounded(digit * result, Straw & rng, digit const * min, digit const * max, int precision);
  110. bool MPEXPORT XMP_Is_Prime(digit const * prime, int precision);
  111. bool MPEXPORT XMP_Rabin_Miller_Test(Straw & rng, digit const * w, int rounds, int precision);
  112. int MPEXPORT XMP_DER_Length_Encode(unsigned long length, unsigned char * output);
  113. int MPEXPORT XMP_DER_Encode(digit const * from, unsigned char * output, int precision);
  114. void MPEXPORT XMP_DER_Decode(digit * result, unsigned char const * input, int precision);
  115. }
  116. inline int MPEXPORT XMP_Digits_To_Bits(int digits)
  117. {
  118. return(digits << LOG_UNITSIZE);
  119. }
  120. inline int MPEXPORT XMP_Bits_To_Digits(int bits)
  121. {
  122. return ((bits + (UNITSIZE-1)) / UNITSIZE);
  123. }
  124. inline digit MPEXPORT XMP_Bits_To_Mask(int bits)
  125. {
  126. if (!bits) return(0);
  127. return(1 << ((bits-1) % UNITSIZE));
  128. }
  129. inline bool MPEXPORT XMP_Is_Negative(const digit * r, int precision)
  130. {
  131. return((signeddigit) *(r + (precision-1)) < 0);
  132. }
  133. inline void MPEXPORT XMP_Set_Bit(digit * r, unsigned bit)
  134. {
  135. r[bit >> LOG_UNITSIZE] |= ((digit)1 << (bit & (UNITSIZE-1)));
  136. }
  137. inline bool MPEXPORT XMP_Test_Bit(const digit * r, unsigned bit)
  138. {
  139. return ((r[bit >> LOG_UNITSIZE] & ((digit)1 << (bit & (UNITSIZE-1)))) != 0);
  140. }
  141. #endif