1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* Copyright (C) 2001-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Mathematical operators */
#include "math_.h"
#include "ghost.h"
#include "gxfarith.h"
#include "oper.h"
#include "store.h"
/*
* Many of the procedures in this file are public only so they can be
* called from the FunctionType 4 interpreter (zfunc4.c).
*/
/*
* Define the current state of random number generator for operators. We
* have to implement this ourselves because the Unix rand doesn't provide
* anything equivalent to rrand. Note that the value always lies in the
* range [0..0x7ffffffe], even if longs are longer than 32 bits.
*
* The state must be public so that context switching can save and
* restore it. (Even though the Red Book doesn't mention this,
* we verified with Adobe that this is the case.)
*/
#define zrand_state (i_ctx_p->rand_state)
/* Initialize the random number generator. */
const long rand_state_initial = 1;
/****** NOTE: none of these operators currently ******/
/****** check for floating over- or underflow. ******/
/* <num> sqrt <real> */
int
zsqrt(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double num;
int code = real_param(op, &num);
if (code < 0)
return code;
if (num < 0.0)
return_error(gs_error_rangecheck);
make_real(op, sqrt(num));
return 0;
}
/* <num> arccos <real> */
static int
zarccos(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double num, result;
int code = real_param(op, &num);
if (code < 0)
return code;
result = acos(num) * radians_to_degrees;
make_real(op, result);
return 0;
}
/* <num> arcsin <real> */
static int
zarcsin(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double num, result;
int code = real_param(op, &num);
if (code < 0)
return code;
result = asin(num) * radians_to_degrees;
make_real(op, result);
return 0;
}
/* <num> <denom> atan <real> */
int
zatan(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double args[2];
double result;
int code = num_params(op, 2, args);
if (code < 0)
return code;
code = gs_atan2_degrees(args[0], args[1], &result);
if (code < 0)
return code;
make_real(op - 1, result);
pop(1);
return 0;
}
/* <num> cos <real> */
int
zcos(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double angle;
int code = real_param(op, &angle);
if (code < 0)
return code;
make_real(op, gs_cos_degrees(angle));
return 0;
}
/* <num> sin <real> */
int
zsin(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double angle;
int code = real_param(op, &angle);
if (code < 0)
return code;
make_real(op, gs_sin_degrees(angle));
return 0;
}
/* <base> <exponent> exp <real> */
int
zexp(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double args[2];
double result;
double ipart;
int code = num_params(op, 2, args);
if (code < 0)
return code;
if (args[0] == 0.0 && args[1] < 0)
return_error(gs_error_undefinedresult);
if (args[0] < 0.0 && modf(args[1], &ipart) != 0.0)
return_error(gs_error_undefinedresult);
if (args[0] == 0.0 && args[1] == 0.0)
result = 1.0; /* match Adobe; can't rely on C library */
else
result = pow(args[0], args[1]);
#ifdef HAVE_ISINF
if (isinf((op - 1)->value.realval))
return_error(gs_error_undefinedresult);
#endif
make_real(op - 1, result);
pop(1);
return 0;
}
/* <posnum> ln <real> */
int
zln(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double num;
int code = real_param(op, &num);
if (code < 0)
return code;
if (num <= 0.0)
return_error(gs_error_rangecheck);
make_real(op, log(num));
return 0;
}
/* <posnum> log <real> */
int
zlog(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
double num;
int code = real_param(op, &num);
if (code < 0)
return code;
if (num <= 0.0)
return_error(gs_error_rangecheck);
make_real(op, log10(num));
return 0;
}
/* - rand <int> */
static int
zrand(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
/*
* We use an algorithm from CACM 31 no. 10, pp. 1192-1201,
* October 1988. According to a posting by Ed Taft on
* comp.lang.postscript, Level 2 (Adobe) PostScript interpreters
* use this algorithm too:
* x[n+1] = (16807 * x[n]) mod (2^31 - 1)
*/
#define A 16807
#define M 0x7fffffff
#define Q 127773 /* M / A */
#define R 2836 /* M % A */
zrand_state = A * (zrand_state % Q) - R * (zrand_state / Q);
/* Note that zrand_state cannot be 0 here. */
if (zrand_state <= 0)
zrand_state += M;
#undef A
#undef M
#undef Q
#undef R
push(1);
make_int(op, zrand_state);
return 0;
}
/* <int> srand - */
static int
zsrand(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
int state;
check_type(*op, t_integer);
state = op->value.intval;
/*
* The following somewhat bizarre adjustments are according to
* public information from Adobe describing their implementation.
*/
if (state < 1)
state = -(state % 0x7ffffffe) + 1;
else if (state > 0x7ffffffe)
state = 0x7ffffffe;
zrand_state = state;
pop(1);
return 0;
}
/* - rrand <int> */
static int
zrrand(i_ctx_t *i_ctx_p)
{
os_ptr op = osp;
push(1);
make_int(op, zrand_state);
return 0;
}
/* ------ Initialization procedure ------ */
const op_def zmath_op_defs[] =
{
{"1arccos", zarccos}, /* extension */
{"1arcsin", zarcsin}, /* extension */
{"2atan", zatan},
{"1cos", zcos},
{"2exp", zexp},
{"1ln", zln},
{"1log", zlog},
{"0rand", zrand},
{"0rrand", zrrand},
{"1sin", zsin},
{"1sqrt", zsqrt},
{"1srand", zsrand},
op_def_end(0)
};
|