home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZARITH.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  298 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zarith.c */
  20. /* Arithmetic operators */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /****** NOTE: none of the arithmetic operators  ******/
  28. /****** currently check for floating exceptions ******/
  29.  
  30. /* Imported operators */
  31. extern int zcvi(P1(os_ptr));
  32.  
  33. /* Define max and min values for what will fit in value.intval. */
  34. #define min_intval min_long
  35. #define max_intval max_long
  36. #define max_half_intval ((1 << (size_of(long) / 2 - 1)) - 1)
  37.  
  38. /* Macro for accessing next-to-top stack element */
  39. #define opm1 (op-1)
  40. /* Macros for generating non-integer cases for arithmetic operations. */
  41. /* 'frob' is one of the arithmetic operators, +, -, or *. */
  42. #define non_int_cases(frob,frob_equals)\
  43.  switch ( r_type(op) ) {\
  44.   default: return_error(e_typecheck);\
  45.   case t_real: switch ( r_type(opm1) ) {\
  46.    default: return_error(e_typecheck);\
  47.    case t_real: op[-1].value.realval frob_equals op->value.realval; break;\
  48.    case t_integer: make_real(opm1, op[-1].value.intval frob op->value.realval);\
  49.   } break;\
  50.   case t_integer: switch ( r_type(opm1) ) {\
  51.    default: return_error(e_typecheck);\
  52.    case t_real: op[-1].value.realval frob_equals op->value.intval; break;\
  53.    case t_integer:
  54. #define end_cases()\
  55.   } }
  56.  
  57. /* <num1> <num2> add <sum> */
  58. /* We make this into a separate procedure because */
  59. /* the interpreter will almost always call it directly. */
  60. int
  61. zop_add(register os_ptr op)
  62. {    non_int_cases(+, +=)
  63.        {    long int2 = op->value.intval;
  64.         if ( ((op[-1].value.intval += int2) ^ int2) < 0 &&
  65.              ((op[-1].value.intval - int2) ^ int2) >= 0
  66.            )
  67.            {    /* Overflow, convert to real */
  68.             make_real(opm1, (float)(op[-1].value.intval - int2) + int2);
  69.            }
  70.        }
  71.     end_cases()
  72.     return 0;
  73. }
  74. int
  75. zadd(os_ptr op)
  76. {    int code = zop_add(op);
  77.     if ( code == 0 ) { pop(1); }
  78.     return code;
  79. }
  80.  
  81. /* <num1> <num2> div <real_quotient> */
  82. int
  83. zdiv(register os_ptr op)
  84. {    register os_ptr op1 = op - 1;
  85.     /* We can't use the non_int_cases macro, */
  86.     /* because we have to check explicitly for op == 0. */
  87.     switch ( r_type(op) )
  88.        {
  89.     default:
  90.         return_error(e_typecheck);
  91.     case t_real:
  92.         if ( op->value.realval == 0 )
  93.             return_error(e_undefinedresult);
  94.         switch ( r_type(op1) )
  95.            {
  96.         default:
  97.             return_error(e_typecheck);
  98.         case t_real:
  99.             op1->value.realval /= op->value.realval;
  100.             break;
  101.         case t_integer:
  102.             make_real(op1, op1->value.intval / op->value.realval);
  103.            }
  104.         break;
  105.     case t_integer:
  106.         if ( op->value.intval == 0 )
  107.             return_error(e_undefinedresult);
  108.         switch ( r_type(op1) )
  109.            {
  110.         default:
  111.             return_error(e_typecheck);
  112.         case t_real:
  113.             op1->value.realval /= op->value.intval; break;
  114.         case t_integer:
  115.             make_real(op1, (float)op1->value.intval / op->value.intval);
  116.            }
  117.        }
  118.     pop(1);
  119.     return 0;
  120. }
  121.  
  122. /* <num1> <num2> mul <product> */
  123. int
  124. zmul(register os_ptr op)
  125. {    non_int_cases(*, *=)
  126.        {    long int1 = op[-1].value.intval;
  127.         long int2 = op->value.intval;
  128.         long abs1 = (int1 >= 0 ? int1 : - int1);
  129.         long abs2 = (int2 >= 0 ? int2 : - int2);
  130.         float fprod;
  131.         if (    (abs1 > max_half_intval || abs2 > max_half_intval) &&
  132.             /* At least one of the operands is very large. */
  133.             /* Check for integer overflow. */
  134.             abs1 != 0 &&
  135.             abs2 > max_intval / abs1 &&
  136.             /* Check for the boundary case */
  137.             (fprod = (float)int1 * int2,
  138.              (int1 * int2 != min_intval ||
  139.              fprod != (float)min_intval))
  140.            )
  141.             make_real(opm1, fprod);
  142.         else
  143.             op[-1].value.intval = int1 * int2;
  144.        }
  145.     end_cases()
  146.     pop(1);
  147.     return 0;
  148. }
  149.  
  150. /* <num1> <num2> sub <difference> */
  151. /* We make this into a separate procedure because */
  152. /* the interpreter will almost always call it directly. */
  153. int
  154. zop_sub(register os_ptr op)
  155. {    non_int_cases(-, -=)
  156.        {    long int1 = op[-1].value.intval;
  157.         if ( (int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
  158.              (int1 ^ op->value.intval) < 0
  159.            )
  160.            {    /* Overflow, convert to real */
  161.             make_real(opm1, (float)int1 - op->value.intval);
  162.            }
  163.        }
  164.     end_cases()
  165.     return 0;
  166. }
  167. int
  168. zsub(os_ptr op)
  169. {    int code = zop_sub(op);
  170.     if ( code == 0 ) { pop(1); }
  171.     return code;
  172. }
  173.  
  174. /* <num1> <num2> idiv <int_quotient> */
  175. int
  176. zidiv(register os_ptr op)
  177. {    register os_ptr op1 = op - 1;
  178.     if ( !(r_has_type(op1, t_integer) && r_has_type(op, t_integer)) )
  179.         return_error(e_typecheck);
  180.     if ( op->value.intval == 0 )
  181.         return_error(e_undefinedresult);
  182.     if ( (op1->value.intval /= op->value.intval) ==
  183.         min_intval && op->value.intval == -1
  184.        )
  185.        {    /* Anomalous boundary case, fail. */
  186.         return_error(e_rangecheck);
  187.        }
  188.     pop(1);
  189.     return 0;
  190. }
  191.  
  192. /* <int1> <int2> mod <remainder> */
  193. int
  194. zmod(register os_ptr op)
  195. {    check_type(op[-1], t_integer);
  196.     check_type(*op, t_integer);
  197.     if ( op->value.intval == 0 )
  198.         return_error(e_undefinedresult);
  199.     op[-1].value.intval %= op->value.intval;
  200.     pop(1);
  201.     return 0;
  202. }
  203.  
  204. /* <num1> neg <num2> */
  205. int
  206. zneg(register os_ptr op)
  207. {    switch ( r_type(op) )
  208.        {
  209.     default:
  210.         return_error(e_typecheck);
  211.     case t_real:
  212.         op->value.realval = -op->value.realval;
  213.         break;
  214.     case t_integer:
  215.         if ( op->value.intval == min_intval )
  216.             make_real(op, -(float)min_intval);
  217.         else
  218.             op->value.intval = -op->value.intval;
  219.        }
  220.     return 0;
  221. }
  222.  
  223. /* <num1> ceiling <num2> */
  224. int
  225. zceiling(register os_ptr op)
  226. {    switch ( r_type(op) )
  227.        {
  228.     default:
  229.         return_error(e_typecheck);
  230.     case t_real:
  231.         op->value.realval = ceil(op->value.realval);
  232.     case t_integer: ;
  233.        }
  234.     return 0;
  235. }
  236.  
  237. /* <num1> floor <num2> */
  238. int
  239. zfloor(register os_ptr op)
  240. {    switch ( r_type(op) )
  241.        {
  242.     default:
  243.         return_error(e_typecheck);
  244.     case t_real:
  245.         op->value.realval = floor(op->value.realval);
  246.     case t_integer: ;
  247.        }
  248.     return 0;
  249. }
  250.  
  251. /* <num1> round <num2> */
  252. int
  253. zround(register os_ptr op)
  254. {    switch ( r_type(op) )
  255.        {
  256.     default:
  257.         return_error(e_typecheck);
  258.     case t_real:
  259.         op->value.realval = floor(op->value.realval + 0.5);
  260.     case t_integer: ;
  261.        }
  262.     return 0;
  263. }
  264.  
  265. /* <num1> truncate <num2> */
  266. int
  267. ztruncate(register os_ptr op)
  268. {    switch ( r_type(op) )
  269.        {
  270.     default:
  271.         return_error(e_typecheck);
  272.     case t_real:
  273.         op->value.realval =
  274.             (op->value.realval < 0.0 ?
  275.                 ceil(op->value.realval) :
  276.                 floor(op->value.realval));
  277.     case t_integer: ;
  278.        }
  279.     return 0;
  280. }
  281.  
  282. /* ------ Initialization table ------ */
  283.  
  284. op_def zarith_op_defs[] = {
  285.     {"2add", zadd},
  286.     {"1ceiling", zceiling},
  287.     {"2div", zdiv},
  288.     {"2idiv", zidiv},
  289.     {"1floor", zfloor},
  290.     {"2mod", zmod},
  291.     {"2mul", zmul},
  292.     {"1neg", zneg},
  293.     {"1round", zround},
  294.     {"2sub", zsub},
  295.     {"1truncate", ztruncate},
  296.     op_def_end(0)
  297. };
  298.