2
0

blitz_cclib.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "blitz.h"
  2. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  3. extern int bbIntAbs( int x );
  4. extern int bbIntSgn( int x );
  5. extern int bbIntMod( int x,int y );
  6. extern void bbIntToLong( BBInt64 *r,int x );
  7. extern double bbFloatAbs( double x );
  8. extern double bbFloatSgn( double x );
  9. extern double bbFloatPow( double x,double y );
  10. extern double bbFloatMod( double x,double y );
  11. extern int bbFloatToInt( double x );
  12. extern void bbFloatToLong( BBInt64 *r,double x );
  13. extern BBInt64 bbLongNeg( BBInt64 x );
  14. extern BBInt64 bbLongNot( BBInt64 x );
  15. extern BBInt64 bbLongAbs( BBInt64 x );
  16. extern BBInt64 bbLongSgn( BBInt64 x );
  17. extern void bbLongAdd( BBInt64 *r,BBInt64 x,BBInt64 y );
  18. extern void bbLongSub( BBInt64 *r,BBInt64 x,BBInt64 y );
  19. extern void bbLongMul( BBInt64 *r,BBInt64 x,BBInt64 y );
  20. extern void bbLongDiv( BBInt64 *r,BBInt64 x,BBInt64 y );
  21. extern void bbLongMod( BBInt64 *r,BBInt64 x,BBInt64 y );
  22. extern void bbLongAnd( BBInt64 *r,BBInt64 x,BBInt64 y );
  23. extern void bbLongOrl( BBInt64 *r,BBInt64 x,BBInt64 y );
  24. extern void bbLongXor( BBInt64 *r,BBInt64 x,BBInt64 y );
  25. extern void bbLongShl( BBInt64 *r,BBInt64 x,BBInt64 y );
  26. extern void bbLongShr( BBInt64 *r,BBInt64 x,BBInt64 y );
  27. extern void bbLongSar( BBInt64 *r,BBInt64 x,BBInt64 y );
  28. extern int bbLongSlt( BBInt64 x,BBInt64 y );
  29. extern int bbLongSgt( BBInt64 x,BBInt64 y );
  30. extern int bbLongSle( BBInt64 x,BBInt64 y );
  31. extern int bbLongSge( BBInt64 x,BBInt64 y );
  32. extern int bbLongSeq( BBInt64 x,BBInt64 y );
  33. extern int bbLongSne( BBInt64 x,BBInt64 y );
  34. extern double bbLongToFloat( BBInt64 x );
  35. extern BBSIZET bbSizetSgn( BBSIZET x );
  36. extern BBSIZET bbSizetAbs( BBSIZET x );
  37. extern BBUINT bbUIntSgn( BBUINT x );
  38. extern BBUINT bbUIntAbs( BBUINT x );
  39. extern BBULONG bbULongSgn( BBULONG x );
  40. extern BBULONG bbULongAbs( BBULONG x );
  41. #else
  42. int bbIntAbs( int x ){
  43. return x>=0 ? x : -x;
  44. }
  45. int bbIntSgn( int x ){
  46. return x==0 ? 0 : (x>0 ? 1 : -1);
  47. }
  48. int bbIntMod( int x,int y ){
  49. return x % y;
  50. }
  51. void bbIntToLong( BBInt64 *r,int x ){
  52. *r=x;
  53. }
  54. double bbFloatAbs( double x ){
  55. return fabs( x );
  56. }
  57. double bbFloatSgn( double x ){
  58. return x==0 ? 0 : (x>0 ? 1 : -1);
  59. }
  60. double bbFloatPow( double x,double y ){
  61. return pow(x,y);
  62. }
  63. double bbFloatMod( double x,double y ){
  64. return fmod( x,y );
  65. }
  66. void bbFloatToLong( BBInt64 *r,double x ){
  67. *r=x;
  68. }
  69. BBInt64 bbLongNeg( BBInt64 x ){
  70. return -x;
  71. }
  72. BBInt64 bbLongNot( BBInt64 x ){
  73. return ~x;
  74. }
  75. BBInt64 bbLongAbs( BBInt64 x ){
  76. return x>=0 ? x : -x;
  77. }
  78. BBInt64 bbLongSgn( BBInt64 x ){
  79. return x>0 ? 1 : (x<0 ? -1 : 0);
  80. }
  81. void bbLongAdd( BBInt64 *r,BBInt64 x,BBInt64 y ){
  82. *r=x+y;
  83. }
  84. void bbLongSub( BBInt64 *r,BBInt64 x,BBInt64 y ){
  85. *r=x-y;
  86. }
  87. void bbLongMul( BBInt64 *r,BBInt64 x,BBInt64 y ){
  88. *r=x*y;
  89. }
  90. void bbLongDiv( BBInt64 *r,BBInt64 x,BBInt64 y ){
  91. *r=x/y;
  92. }
  93. void bbLongMod( BBInt64 *r,BBInt64 x,BBInt64 y ){
  94. *r=x%y;
  95. }
  96. void bbLongAnd( BBInt64 *r,BBInt64 x,BBInt64 y ){
  97. *r=x&y;
  98. }
  99. void bbLongOrl( BBInt64 *r,BBInt64 x,BBInt64 y ){
  100. *r=x|y;
  101. }
  102. void bbLongXor( BBInt64 *r,BBInt64 x,BBInt64 y ){
  103. *r=x^y;
  104. }
  105. void bbLongShl( BBInt64 *r,BBInt64 x,BBInt64 y ){
  106. *r=x<<y;
  107. }
  108. void bbLongShr( BBInt64 *r,BBInt64 x,BBInt64 y ){
  109. *r=(BBUInt64)x>>(BBUInt64)y;
  110. }
  111. void bbLongSar( BBInt64 *r,BBInt64 x,BBInt64 y ){
  112. *r=x>>y;
  113. }
  114. int bbLongSlt( BBInt64 x,BBInt64 y ){
  115. return x<y;
  116. }
  117. int bbLongSgt( BBInt64 x,BBInt64 y ){
  118. return x>y;
  119. }
  120. int bbLongSle( BBInt64 x,BBInt64 y ){
  121. return x<=y;
  122. }
  123. int bbLongSge( BBInt64 x,BBInt64 y ){
  124. return x>=y;
  125. }
  126. int bbLongSeq( BBInt64 x,BBInt64 y ){
  127. return x==y;
  128. }
  129. int bbLongSne( BBInt64 x,BBInt64 y ){
  130. return x!=y;
  131. }
  132. double bbLongToFloat( BBInt64 x ){
  133. return (double)x;
  134. }
  135. BBSIZET bbSizetAbs( BBSIZET x ){
  136. return x>=0 ? x : -x;
  137. }
  138. BBSIZET bbSizetSgn( BBSIZET x ){
  139. return x==0 ? 0 : (x>0 ? 1 : -1);
  140. }
  141. BBUINT bbUIntAbs( BBUINT x ){
  142. return x>=0 ? x : -x;
  143. }
  144. BBUINT bbUIntSgn( BBUINT x ){
  145. return x==0 ? 0 : (x>0 ? 1 : -1);
  146. }
  147. BBULONG bbULongAbs( BBULONG x ){
  148. return x>=0 ? x : -x;
  149. }
  150. BBULONG bbULongSgn( BBULONG x ){
  151. return x==0 ? 0 : (x>0 ? 1 : -1);
  152. }
  153. #endif
  154. BBLONG bbLongPow(BBLONG base, BBBYTE exp) {
  155. static const BBBYTE highest_bit_set[] = {
  156. 0, 1, 2, 2, 3, 3, 3, 3,
  157. 4, 4, 4, 4, 4, 4, 4, 4,
  158. 5, 5, 5, 5, 5, 5, 5, 5,
  159. 5, 5, 5, 5, 5, 5, 5, 5,
  160. 6, 6, 6, 6, 6, 6, 6, 6,
  161. 6, 6, 6, 6, 6, 6, 6, 6,
  162. 6, 6, 6, 6, 6, 6, 6, 6,
  163. 6, 6, 6, 6, 6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1
  164. 255, 255, 255, 255, 255, 255, 255, 255,
  165. 255, 255, 255, 255, 255, 255, 255, 255,
  166. 255, 255, 255, 255, 255, 255, 255, 255,
  167. 255, 255, 255, 255, 255, 255, 255, 255,
  168. 255, 255, 255, 255, 255, 255, 255, 255,
  169. 255, 255, 255, 255, 255, 255, 255, 255,
  170. 255, 255, 255, 255, 255, 255, 255, 255,
  171. 255, 255, 255, 255, 255, 255, 255, 255,
  172. 255, 255, 255, 255, 255, 255, 255, 255,
  173. 255, 255, 255, 255, 255, 255, 255, 255,
  174. 255, 255, 255, 255, 255, 255, 255, 255,
  175. 255, 255, 255, 255, 255, 255, 255, 255,
  176. 255, 255, 255, 255, 255, 255, 255, 255,
  177. 255, 255, 255, 255, 255, 255, 255, 255,
  178. 255, 255, 255, 255, 255, 255, 255, 255,
  179. 255, 255, 255, 255, 255, 255, 255, 255,
  180. 255, 255, 255, 255, 255, 255, 255, 255,
  181. 255, 255, 255, 255, 255, 255, 255, 255,
  182. 255, 255, 255, 255, 255, 255, 255, 255,
  183. 255, 255, 255, 255, 255, 255, 255, 255,
  184. 255, 255, 255, 255, 255, 255, 255, 255,
  185. 255, 255, 255, 255, 255, 255, 255, 255,
  186. 255, 255, 255, 255, 255, 255, 255, 255,
  187. 255, 255, 255, 255, 255, 255, 255, 255,
  188. };
  189. BBLONG result = 1;
  190. switch (highest_bit_set[exp]) {
  191. case 255: // we use 255 as an overflow marker and return 0 on overflow/underflow
  192. if (base == 1) {
  193. return 1;
  194. }
  195. if (base == -1) {
  196. return 1 - 2 * (exp & 1);
  197. }
  198. return 0;
  199. case 6:
  200. if (exp & 1) result *= base;
  201. exp >>= 1;
  202. base *= base;
  203. case 5:
  204. if (exp & 1) result *= base;
  205. exp >>= 1;
  206. base *= base;
  207. case 4:
  208. if (exp & 1) result *= base;
  209. exp >>= 1;
  210. base *= base;
  211. case 3:
  212. if (exp & 1) result *= base;
  213. exp >>= 1;
  214. base *= base;
  215. case 2:
  216. if (exp & 1) result *= base;
  217. exp >>= 1;
  218. base *= base;
  219. case 1:
  220. if (exp & 1) result *= base;
  221. default:
  222. return result;
  223. }
  224. }