zarith.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zarith.c,v 1.6 2002/02/21 22:24:54 giles Exp $ */
  14. /* Arithmetic operators */
  15. #include "math_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "store.h"
  19. /****** NOTE: none of the arithmetic operators ******/
  20. /****** currently check for floating exceptions ******/
  21. /*
  22. * Many of the procedures in this file are public only so they can be
  23. * called from the FunctionType 4 interpreter (zfunc4.c).
  24. */
  25. /* Define max and min values for what will fit in value.intval. */
  26. #define MIN_INTVAL min_long
  27. #define MAX_INTVAL max_long
  28. #define MAX_HALF_INTVAL ((1L << (size_of(long) * 4 - 1)) - 1)
  29. /* <num1> <num2> add <sum> */
  30. /* We make this into a separate procedure because */
  31. /* the interpreter will almost always call it directly. */
  32. int
  33. zop_add(register os_ptr op)
  34. {
  35. switch (r_type(op)) {
  36. default:
  37. return_op_typecheck(op);
  38. case t_real:
  39. switch (r_type(op - 1)) {
  40. default:
  41. return_op_typecheck(op - 1);
  42. case t_real:
  43. op[-1].value.realval += op->value.realval;
  44. break;
  45. case t_integer:
  46. make_real(op - 1, (double)op[-1].value.intval + op->value.realval);
  47. }
  48. break;
  49. case t_integer:
  50. switch (r_type(op - 1)) {
  51. default:
  52. return_op_typecheck(op - 1);
  53. case t_real:
  54. op[-1].value.realval += (double)op->value.intval;
  55. break;
  56. case t_integer: {
  57. long int2 = op->value.intval;
  58. if (((op[-1].value.intval += int2) ^ int2) < 0 &&
  59. ((op[-1].value.intval - int2) ^ int2) >= 0
  60. ) { /* Overflow, convert to real */
  61. make_real(op - 1, (double)(op[-1].value.intval - int2) + int2);
  62. }
  63. }
  64. }
  65. }
  66. return 0;
  67. }
  68. int
  69. zadd(i_ctx_t *i_ctx_p)
  70. {
  71. os_ptr op = osp;
  72. int code = zop_add(op);
  73. if (code == 0) {
  74. pop(1);
  75. }
  76. return code;
  77. }
  78. /* <num1> <num2> div <real_quotient> */
  79. int
  80. zdiv(i_ctx_t *i_ctx_p)
  81. {
  82. os_ptr op = osp;
  83. os_ptr op1 = op - 1;
  84. /* We can't use the non_int_cases macro, */
  85. /* because we have to check explicitly for op == 0. */
  86. switch (r_type(op)) {
  87. default:
  88. return_op_typecheck(op);
  89. case t_real:
  90. if (op->value.realval == 0)
  91. return_error(e_undefinedresult);
  92. switch (r_type(op1)) {
  93. default:
  94. return_op_typecheck(op1);
  95. case t_real:
  96. op1->value.realval /= op->value.realval;
  97. break;
  98. case t_integer:
  99. make_real(op1, (double)op1->value.intval / op->value.realval);
  100. }
  101. break;
  102. case t_integer:
  103. if (op->value.intval == 0)
  104. return_error(e_undefinedresult);
  105. switch (r_type(op1)) {
  106. default:
  107. return_op_typecheck(op1);
  108. case t_real:
  109. op1->value.realval /= (double)op->value.intval;
  110. break;
  111. case t_integer:
  112. make_real(op1, (double)op1->value.intval / (double)op->value.intval);
  113. }
  114. }
  115. pop(1);
  116. return 0;
  117. }
  118. /* <num1> <num2> mul <product> */
  119. int
  120. zmul(i_ctx_t *i_ctx_p)
  121. {
  122. os_ptr op = osp;
  123. switch (r_type(op)) {
  124. default:
  125. return_op_typecheck(op);
  126. case t_real:
  127. switch (r_type(op - 1)) {
  128. default:
  129. return_op_typecheck(op - 1);
  130. case t_real:
  131. op[-1].value.realval *= op->value.realval;
  132. break;
  133. case t_integer:
  134. make_real(op - 1, (double)op[-1].value.intval * op->value.realval);
  135. }
  136. break;
  137. case t_integer:
  138. switch (r_type(op - 1)) {
  139. default:
  140. return_op_typecheck(op - 1);
  141. case t_real:
  142. op[-1].value.realval *= (double)op->value.intval;
  143. break;
  144. case t_integer: {
  145. long int1 = op[-1].value.intval;
  146. long int2 = op->value.intval;
  147. long abs1 = (int1 >= 0 ? int1 : -int1);
  148. long abs2 = (int2 >= 0 ? int2 : -int2);
  149. float fprod;
  150. if ((abs1 > MAX_HALF_INTVAL || abs2 > MAX_HALF_INTVAL) &&
  151. /* At least one of the operands is very large. */
  152. /* Check for integer overflow. */
  153. abs1 != 0 &&
  154. abs2 > MAX_INTVAL / abs1 &&
  155. /* Check for the boundary case */
  156. (fprod = (float)int1 * int2,
  157. (int1 * int2 != MIN_INTVAL ||
  158. fprod != (float)MIN_INTVAL))
  159. )
  160. make_real(op - 1, fprod);
  161. else
  162. op[-1].value.intval = int1 * int2;
  163. }
  164. }
  165. }
  166. pop(1);
  167. return 0;
  168. }
  169. /* <num1> <num2> sub <difference> */
  170. /* We make this into a separate procedure because */
  171. /* the interpreter will almost always call it directly. */
  172. int
  173. zop_sub(register os_ptr op)
  174. {
  175. switch (r_type(op)) {
  176. default:
  177. return_op_typecheck(op);
  178. case t_real:
  179. switch (r_type(op - 1)) {
  180. default:
  181. return_op_typecheck(op - 1);
  182. case t_real:
  183. op[-1].value.realval -= op->value.realval;
  184. break;
  185. case t_integer:
  186. make_real(op - 1, (double)op[-1].value.intval - op->value.realval);
  187. }
  188. break;
  189. case t_integer:
  190. switch (r_type(op - 1)) {
  191. default:
  192. return_op_typecheck(op - 1);
  193. case t_real:
  194. op[-1].value.realval -= (double)op->value.intval;
  195. break;
  196. case t_integer: {
  197. long int1 = op[-1].value.intval;
  198. if ((int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
  199. (int1 ^ op->value.intval) < 0
  200. ) { /* Overflow, convert to real */
  201. make_real(op - 1, (float)int1 - op->value.intval);
  202. }
  203. }
  204. }
  205. }
  206. return 0;
  207. }
  208. int
  209. zsub(i_ctx_t *i_ctx_p)
  210. {
  211. os_ptr op = osp;
  212. int code = zop_sub(op);
  213. if (code == 0) {
  214. pop(1);
  215. }
  216. return code;
  217. }
  218. /* <num1> <num2> idiv <int_quotient> */
  219. int
  220. zidiv(i_ctx_t *i_ctx_p)
  221. {
  222. os_ptr op = osp;
  223. check_type(*op, t_integer);
  224. check_type(op[-1], t_integer);
  225. if (op->value.intval == 0)
  226. return_error(e_undefinedresult);
  227. if ((op[-1].value.intval /= op->value.intval) ==
  228. MIN_INTVAL && op->value.intval == -1
  229. ) { /* Anomalous boundary case, fail. */
  230. return_error(e_rangecheck);
  231. }
  232. pop(1);
  233. return 0;
  234. }
  235. /* <int1> <int2> mod <remainder> */
  236. int
  237. zmod(i_ctx_t *i_ctx_p)
  238. {
  239. os_ptr op = osp;
  240. check_type(*op, t_integer);
  241. check_type(op[-1], t_integer);
  242. if (op->value.intval == 0)
  243. return_error(e_undefinedresult);
  244. op[-1].value.intval %= op->value.intval;
  245. pop(1);
  246. return 0;
  247. }
  248. /* <num1> neg <num2> */
  249. int
  250. zneg(i_ctx_t *i_ctx_p)
  251. {
  252. os_ptr op = osp;
  253. switch (r_type(op)) {
  254. default:
  255. return_op_typecheck(op);
  256. case t_real:
  257. op->value.realval = -op->value.realval;
  258. break;
  259. case t_integer:
  260. if (op->value.intval == MIN_INTVAL)
  261. make_real(op, -(float)MIN_INTVAL);
  262. else
  263. op->value.intval = -op->value.intval;
  264. }
  265. return 0;
  266. }
  267. /* <num1> abs <num2> */
  268. int
  269. zabs(i_ctx_t *i_ctx_p)
  270. {
  271. os_ptr op = osp;
  272. switch (r_type(op)) {
  273. default:
  274. return_op_typecheck(op);
  275. case t_real:
  276. if (op->value.realval >= 0)
  277. return 0;
  278. break;
  279. case t_integer:
  280. if (op->value.intval >= 0)
  281. return 0;
  282. break;
  283. }
  284. return zneg(i_ctx_p);
  285. }
  286. /* <num1> ceiling <num2> */
  287. int
  288. zceiling(i_ctx_t *i_ctx_p)
  289. {
  290. os_ptr op = osp;
  291. switch (r_type(op)) {
  292. default:
  293. return_op_typecheck(op);
  294. case t_real:
  295. op->value.realval = ceil(op->value.realval);
  296. case t_integer:;
  297. }
  298. return 0;
  299. }
  300. /* <num1> floor <num2> */
  301. int
  302. zfloor(i_ctx_t *i_ctx_p)
  303. {
  304. os_ptr op = osp;
  305. switch (r_type(op)) {
  306. default:
  307. return_op_typecheck(op);
  308. case t_real:
  309. op->value.realval = floor(op->value.realval);
  310. case t_integer:;
  311. }
  312. return 0;
  313. }
  314. /* <num1> round <num2> */
  315. int
  316. zround(i_ctx_t *i_ctx_p)
  317. {
  318. os_ptr op = osp;
  319. switch (r_type(op)) {
  320. default:
  321. return_op_typecheck(op);
  322. case t_real:
  323. op->value.realval = floor(op->value.realval + 0.5);
  324. case t_integer:;
  325. }
  326. return 0;
  327. }
  328. /* <num1> truncate <num2> */
  329. int
  330. ztruncate(i_ctx_t *i_ctx_p)
  331. {
  332. os_ptr op = osp;
  333. switch (r_type(op)) {
  334. default:
  335. return_op_typecheck(op);
  336. case t_real:
  337. op->value.realval =
  338. (op->value.realval < 0.0 ?
  339. ceil(op->value.realval) :
  340. floor(op->value.realval));
  341. case t_integer:;
  342. }
  343. return 0;
  344. }
  345. /* Non-standard operators */
  346. /* <int1> <int2> .bitadd <sum> */
  347. private int
  348. zbitadd(i_ctx_t *i_ctx_p)
  349. {
  350. os_ptr op = osp;
  351. check_type(*op, t_integer);
  352. check_type(op[-1], t_integer);
  353. op[-1].value.intval += op->value.intval;
  354. pop(1);
  355. return 0;
  356. }
  357. /* ------ Initialization table ------ */
  358. const op_def zarith_op_defs[] =
  359. {
  360. {"1abs", zabs},
  361. {"2add", zadd},
  362. {"2.bitadd", zbitadd},
  363. {"1ceiling", zceiling},
  364. {"2div", zdiv},
  365. {"2idiv", zidiv},
  366. {"1floor", zfloor},
  367. {"2mod", zmod},
  368. {"2mul", zmul},
  369. {"1neg", zneg},
  370. {"1round", zround},
  371. {"2sub", zsub},
  372. {"1truncate", ztruncate},
  373. op_def_end(0)
  374. };