Coverage Report

Created: 2025-06-10 07:19

/src/ghostpdl/psi/zarith.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Arithmetic operators */
18
#include "math_.h"
19
#include "ghost.h"
20
#include "oper.h"
21
#include "store.h"
22
#include "gsstate.h"
23
24
/*
25
 * Many of the procedures in this file are public only so they can be
26
 * called from the FunctionType 4 interpreter (zfunc4.c).
27
 */
28
29
static int mul_64_64_overflowcheck(int64_t abc, int64_t def, int64_t *res);
30
31
/* <num1> <num2> add <sum> */
32
/* We make this into a separate procedure because */
33
/* the interpreter will almost always call it directly. */
34
int
35
zop_add(i_ctx_t *i_ctx_p)
36
43.1M
{
37
43.1M
    register os_ptr op = osp;
38
43.1M
    float result;
39
40
43.1M
    check_op(2);
41
43.1M
    switch (r_type(op)) {
42
0
    default:
43
0
        return_op_typecheck(op);
44
7.92M
    case t_real:
45
7.92M
        switch (r_type(op - 1)) {
46
0
        default:
47
0
            return_op_typecheck(op - 1);
48
4.32M
        case t_real:
49
4.32M
            result = op[-1].value.realval + op->value.realval;
50
4.32M
#ifdef HAVE_ISINF
51
4.32M
            if (isinf(result))
52
0
                return_error(gs_error_undefinedresult);
53
4.32M
#endif
54
4.32M
#ifdef HAVE_ISNAN
55
4.32M
            if (isnan(result))
56
0
                return_error(gs_error_undefinedresult);
57
4.32M
#endif
58
4.32M
            op[-1].value.realval = result;
59
4.32M
            break;
60
3.60M
        case t_integer:
61
3.60M
            make_real(op - 1, (double)op[-1].value.intval + op->value.realval);
62
7.92M
        }
63
7.92M
        break;
64
35.2M
    case t_integer:
65
35.2M
        switch (r_type(op - 1)) {
66
0
        default:
67
0
            return_op_typecheck(op - 1);
68
7.31M
        case t_real:
69
7.31M
            result = op[-1].value.realval + (double)op->value.intval;
70
7.31M
#ifdef HAVE_ISINF
71
7.31M
            if (isinf(result))
72
0
                return_error(gs_error_undefinedresult);
73
7.31M
#endif
74
7.31M
#ifdef HAVE_ISNAN
75
7.31M
            if (isnan(result))
76
0
                return_error(gs_error_undefinedresult);
77
7.31M
#endif
78
7.31M
            op[-1].value.realval = result;
79
7.31M
            break;
80
27.8M
        case t_integer: {
81
27.8M
            if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory)) {
82
0
                ps_int32 int1 = (ps_int32)op[-1].value.intval;
83
0
                ps_int32 int2 = (ps_int32)op->value.intval;
84
85
0
                if (((int1 += int2) ^ int2) < 0 &&
86
0
                    ((int1 - int2) ^ int2) >= 0
87
0
                    ) {                     /* Overflow, convert to real */
88
0
                    make_real(op - 1, (double)(int1 - int2) + int2);
89
0
                }
90
0
                else {
91
0
                    op[-1].value.intval = (ps_int)int1;
92
0
                }
93
0
            }
94
27.8M
            else {
95
27.8M
                ps_int int2 = op->value.intval;
96
97
27.8M
                if (((op[-1].value.intval += int2) ^ int2) < 0 &&
98
27.8M
                    ((op[-1].value.intval - int2) ^ int2) >= 0
99
27.8M
                    ) {                     /* Overflow, convert to real */
100
32
                    make_real(op - 1, (double)(op[-1].value.intval - int2) + int2);
101
32
                }
102
27.8M
            }
103
27.8M
        }
104
35.2M
        }
105
43.1M
    }
106
43.1M
    return 0;
107
43.1M
}
108
int
109
zadd(i_ctx_t *i_ctx_p)
110
0
{
111
0
    int code = zop_add(i_ctx_p);
112
113
0
    if (code == 0) {
114
0
        pop(1);
115
0
    }
116
0
    return code;
117
0
}
118
119
/* <num1> <num2> div <real_quotient> */
120
int
121
zdiv(i_ctx_t *i_ctx_p)
122
5.35M
{
123
5.35M
    os_ptr op = osp;
124
5.35M
    os_ptr op1 = op - 1;
125
5.35M
    float result;
126
127
5.35M
    check_op(2);
128
    /* We can't use the non_int_cases macro, */
129
    /* because we have to check explicitly for op == 0. */
130
5.35M
    switch (r_type(op)) {
131
0
        default:
132
0
            return_op_typecheck(op);
133
161k
        case t_real:
134
161k
            if (op->value.realval == 0)
135
0
                return_error(gs_error_undefinedresult);
136
161k
            switch (r_type(op1)) {
137
0
                default:
138
0
                    return_op_typecheck(op1);
139
142k
                case t_real:
140
142k
                    result = op1->value.realval / op->value.realval;
141
142k
#ifdef HAVE_ISINF
142
142k
                    if (isinf(result))
143
0
                        return_error(gs_error_undefinedresult);
144
142k
#endif
145
142k
#ifdef HAVE_ISNAN
146
142k
                    if (isnan(result))
147
0
                        return_error(gs_error_undefinedresult);
148
142k
#endif
149
142k
                    op1->value.realval = result;
150
142k
                    break;
151
19.6k
                case t_integer:
152
19.6k
                    result = (double)op1->value.intval / op->value.realval;
153
19.6k
#ifdef HAVE_ISINF
154
19.6k
                    if (isinf(result))
155
0
                        return_error(gs_error_undefinedresult);
156
19.6k
#endif
157
19.6k
#ifdef HAVE_ISNAN
158
19.6k
                    if (isnan(result))
159
0
                        return_error(gs_error_undefinedresult);
160
19.6k
#endif
161
161k
                    make_real(op1, result);
162
161k
            }
163
161k
            break;
164
5.19M
        case t_integer:
165
5.19M
            if (op->value.intval == 0)
166
0
                return_error(gs_error_undefinedresult);
167
5.19M
            switch (r_type(op1)) {
168
1
                default:
169
1
                    return_op_typecheck(op1);
170
4.42M
                case t_real:
171
4.42M
                    result = op1->value.realval / (double)op->value.intval;
172
4.42M
#ifdef HAVE_ISINF
173
4.42M
                    if (isinf(result))
174
0
                        return_error(gs_error_undefinedresult);
175
4.42M
#endif
176
4.42M
#ifdef HAVE_ISNAN
177
4.42M
                    if (isnan(result))
178
0
                        return_error(gs_error_undefinedresult);
179
4.42M
#endif
180
4.42M
                    op1->value.realval = result;
181
4.42M
                    break;
182
766k
                case t_integer:
183
766k
                    result = (double)op1->value.intval / (double)op->value.intval;
184
766k
#ifdef HAVE_ISINF
185
766k
                    if (isinf(result))
186
0
                        return_error(gs_error_undefinedresult);
187
766k
#endif
188
766k
#ifdef HAVE_ISNAN
189
766k
                    if (isnan(result))
190
0
                        return_error(gs_error_undefinedresult);
191
766k
#endif
192
5.19M
                    make_real(op1, result);
193
5.19M
            }
194
5.35M
    }
195
5.35M
    pop(1);
196
5.35M
    return 0;
197
5.35M
}
198
199
/*
200
To detect 64bit x 64bit multiplication overflowing, consider
201
breaking the numbers down into 32bit chunks.
202
203
  abc = (a<<64) + (b<<32) + c
204
      (where a is 0 or -1, and b and c are 32bit unsigned.
205
206
Similarly:
207
208
  def = (d<<64) + (b<<32) + f
209
210
Then:
211
212
  abc.def = ((a<<64) + (b<<32) + c) * ((d<<64) + (e<<32) + f)
213
          = (a<<64).def + (d<<64).abc + (b<<32).(e<<32) +
214
            (b<<32).f + (e<<32).c + cf
215
          = (a.def + d.abc + b.e)<<64 + (b.f + e.c)<<32 + cf
216
217
*/
218
219
static int mul_64_64_overflowcheck(int64_t abc, int64_t def, int64_t *res)
220
690k
{
221
690k
  uint32_t b = (abc>>32);
222
690k
  uint32_t c = (uint32_t)abc;
223
690k
  uint32_t e = (def>>32);
224
690k
  uint32_t f = (uint32_t)def;
225
690k
  uint64_t low, mid, high, bf, ec;
226
227
  /* Low contribution */
228
690k
  low = (uint64_t)c * (uint64_t)f;
229
  /* Mid contributions */
230
690k
  bf = (uint64_t)b * (uint64_t)f;
231
690k
  ec = (uint64_t)e * (uint64_t)c;
232
  /* Top contribution */
233
690k
  high = (uint64_t)b * (uint64_t)e;
234
690k
  if (abc < 0)
235
35
      high -= def;
236
690k
  if (def < 0)
237
4
      high -= abc;
238
  /* How do we check for carries from 64bit unsigned adds?
239
   *  x + y >= (1<<64) == x >= (1<<64) - y
240
   *                   == x >  (1<<64) - y - 1
241
   * if we consider just 64bits, this is:
242
   * x > NOT y
243
   */
244
690k
  if (bf > ~ec)
245
1
      high += ((uint64_t)1)<<32;
246
690k
  mid = bf + ec;
247
690k
  if (low > ~(mid<<32))
248
1
      high += 1;
249
690k
  high += (mid>>32);
250
690k
  low += (mid<<32);
251
252
690k
  *res = low;
253
254
690k
  return (int64_t)low < 0 ? high != -1 : high != 0;
255
690k
}
256
257
/* <num1> <num2> mul <product> */
258
int
259
zmul(i_ctx_t *i_ctx_p)
260
15.7M
{
261
15.7M
    os_ptr op = osp;
262
15.7M
    float result;
263
264
15.7M
    switch (r_type(op)) {
265
1
    default:
266
1
        return_op_typecheck(op);
267
7.72M
    case t_real:
268
7.72M
        switch (r_type(op - 1)) {
269
0
        default:
270
0
            return_op_typecheck(op - 1);
271
7.66M
        case t_real:
272
7.66M
            result = op[-1].value.realval * op->value.realval;
273
7.66M
#ifdef HAVE_ISINF
274
7.66M
            if (isinf(result))
275
4
                return_error(gs_error_undefinedresult);
276
7.66M
#endif
277
7.66M
#ifdef HAVE_ISNAN
278
7.66M
            if (isnan(result))
279
0
                return_error(gs_error_undefinedresult);
280
7.66M
#endif
281
7.66M
            op[-1].value.realval = result;
282
7.66M
            break;
283
57.0k
        case t_integer:
284
57.0k
            result = (double)op[-1].value.intval * op->value.realval;
285
57.0k
            make_real(op - 1, result);
286
7.72M
        }
287
7.72M
        break;
288
7.99M
    case t_integer:
289
7.99M
        switch (r_type(op - 1)) {
290
1
        default:
291
1
            return_op_typecheck(op - 1);
292
7.30M
        case t_real:
293
7.30M
            result = op[-1].value.realval * (double)op->value.intval;
294
7.30M
#ifdef HAVE_ISINF
295
7.30M
            if (isinf(result))
296
0
                return_error(gs_error_undefinedresult);
297
7.30M
#endif
298
7.30M
#ifdef HAVE_ISNAN
299
7.30M
            if (isnan(result))
300
0
                return_error(gs_error_undefinedresult);
301
7.30M
#endif
302
7.30M
            op[-1].value.realval = result;
303
7.30M
            break;
304
690k
        case t_integer: {
305
690k
            if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory)) {
306
0
                double ab = (double)op[-1].value.intval * op->value.intval;
307
0
                if (ab > (double)MAX_PS_INT32)       /* (double)0x7fffffff */
308
0
                    make_real(op - 1, ab);
309
0
                else if (ab < (double)MIN_PS_INT32) /* (double)(int)0x80000000 */
310
0
                    make_real(op - 1, ab);
311
0
                else
312
0
                    op[-1].value.intval = (ps_int)ab;
313
0
            }
314
690k
            else {
315
690k
                int64_t result;
316
690k
                if (mul_64_64_overflowcheck(op[-1].value.intval, op->value.intval, &result)) {
317
0
                    double ab = (double)op[-1].value.intval * op->value.intval;
318
0
                    make_real(op - 1, ab);
319
690k
                } else {
320
690k
                    op[-1].value.intval = result;
321
690k
                }
322
690k
            }
323
690k
        }
324
7.99M
        }
325
15.7M
    }
326
15.7M
    pop(1);
327
15.7M
    return 0;
328
15.7M
}
329
330
/* <num1> <num2> sub <difference> */
331
/* We make this into a separate procedure because */
332
/* the interpreter will almost always call it directly. */
333
int
334
zop_sub(i_ctx_t *i_ctx_p)
335
8.63M
{
336
8.63M
    register os_ptr op = osp;
337
338
8.63M
    check_op(2);
339
8.63M
    switch (r_type(op)) {
340
2
    default:
341
2
        return_op_typecheck(op);
342
3.87M
    case t_real:
343
3.87M
        switch (r_type(op - 1)) {
344
0
        default:
345
0
            return_op_typecheck(op - 1);
346
31.2k
        case t_real:
347
31.2k
            op[-1].value.realval -= op->value.realval;
348
31.2k
            break;
349
3.84M
        case t_integer:
350
3.84M
            make_real(op - 1, (double)op[-1].value.intval - op->value.realval);
351
3.87M
        }
352
3.87M
        break;
353
4.75M
    case t_integer:
354
4.75M
        switch (r_type(op - 1)) {
355
1
        default:
356
1
            return_op_typecheck(op - 1);
357
3.66k
        case t_real:
358
3.66k
            op[-1].value.realval -= (double)op->value.intval;
359
3.66k
            break;
360
4.75M
        case t_integer: {
361
4.75M
            if (sizeof(ps_int) != 4 && gs_currentcpsimode(imemory)) {
362
0
                ps_int32 int1 = (ps_int)op[-1].value.intval;
363
0
                ps_int32 int2 = (ps_int)op->value.intval;
364
0
                ps_int32 int3;
365
366
0
                if ((int1 ^ (int3 = int1 - int2)) < 0 &&
367
0
                    (int1 ^ int2) < 0
368
0
                    ) {                     /* Overflow, convert to real */
369
0
                    make_real(op - 1, (float)int1 - op->value.intval);
370
0
                }
371
0
                else {
372
0
                    op[-1].value.intval = (ps_int)int3;
373
0
                }
374
0
            }
375
4.75M
            else {
376
4.75M
                ps_int int1 = op[-1].value.intval;
377
378
4.75M
                if ((int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
379
4.75M
                    (int1 ^ op->value.intval) < 0
380
4.75M
                    ) {                     /* Overflow, convert to real */
381
0
                    make_real(op - 1, (float)int1 - op->value.intval);
382
0
                }
383
4.75M
            }
384
4.75M
        }
385
4.75M
        }
386
8.63M
    }
387
8.63M
    return 0;
388
8.63M
}
389
int
390
zsub(i_ctx_t *i_ctx_p)
391
0
{
392
0
    int code = zop_sub(i_ctx_p);
393
394
0
    if (code == 0) {
395
0
        pop(1);
396
0
    }
397
0
    return code;
398
0
}
399
400
/* <num1> <num2> idiv <int_quotient> */
401
int
402
zidiv(i_ctx_t *i_ctx_p)
403
2.56M
{
404
2.56M
    os_ptr op = osp;
405
406
2.56M
    check_op(2);
407
2.56M
    check_type(*op, t_integer);
408
2.56M
    check_type(op[-1], t_integer);
409
2.56M
    if (sizeof(ps_int) && gs_currentcpsimode(imemory)) {
410
0
        int tmpval;
411
0
        if ((op->value.intval == 0) || (op[-1].value.intval == (ps_int)MIN_PS_INT32 && op->value.intval == -1)) {
412
            /* Anomalous boundary case: -MININT / -1, fail. */
413
0
            return_error(gs_error_undefinedresult);
414
0
        }
415
0
        tmpval = (int)op[-1].value.intval / op->value.intval;
416
0
        op[-1].value.intval = (int64_t)tmpval;
417
0
    }
418
2.56M
    else {
419
2.56M
        if ((op->value.intval == 0) || (op[-1].value.intval == MIN_PS_INT && op->value.intval == -1)) {
420
            /* Anomalous boundary case: -MININT / -1, fail. */
421
0
            return_error(gs_error_undefinedresult);
422
0
        }
423
2.56M
        op[-1].value.intval /= op->value.intval;
424
2.56M
    }
425
2.56M
    pop(1);
426
2.56M
    return 0;
427
2.56M
}
428
429
/* <int1> <int2> mod <remainder> */
430
int
431
zmod(i_ctx_t *i_ctx_p)
432
4.20M
{
433
4.20M
    os_ptr op = osp;
434
435
4.20M
    check_op(2);
436
4.20M
    check_type(*op, t_integer);
437
4.20M
    check_type(op[-1], t_integer);
438
4.20M
    if (op->value.intval == 0 || op[-1].value.intval == MIN_PS_INT)
439
1
        return_error(gs_error_undefinedresult);
440
4.20M
    op[-1].value.intval %= op->value.intval;
441
4.20M
    pop(1);
442
4.20M
    return 0;
443
4.20M
}
444
445
/* <num1> neg <num2> */
446
int
447
zneg(i_ctx_t *i_ctx_p)
448
422k
{
449
422k
    os_ptr op = osp;
450
451
422k
    check_op(1);
452
422k
    switch (r_type(op)) {
453
1
        default:
454
1
            return_op_typecheck(op);
455
123k
        case t_real:
456
123k
            op->value.realval = -op->value.realval;
457
123k
            break;
458
299k
        case t_integer:
459
299k
            if (sizeof(ps_int) != 32 && gs_currentcpsimode(imemory)) {
460
0
                if (((unsigned int)op->value.intval) == MIN_PS_INT32)
461
0
                    make_real(op, -(float)(ps_uint32)MIN_PS_INT32);
462
0
                else
463
0
                    op->value.intval = -op->value.intval;
464
0
            }
465
299k
            else {
466
299k
                if (op->value.intval == MIN_PS_INT)
467
299k
                    make_real(op, -(float)MIN_PS_INT);
468
299k
                else
469
299k
                    op->value.intval = -op->value.intval;
470
299k
            }
471
422k
    }
472
422k
    return 0;
473
422k
}
474
475
/* <num1> abs <num2> */
476
int
477
zabs(i_ctx_t *i_ctx_p)
478
4.27M
{
479
4.27M
    os_ptr op = osp;
480
481
4.27M
    check_op(1);
482
4.27M
    switch (r_type(op)) {
483
14
        default:
484
14
            return_op_typecheck(op);
485
172k
        case t_real:
486
172k
            if (op->value.realval >= 0)
487
89.1k
                return 0;
488
83.5k
            break;
489
4.09M
        case t_integer:
490
4.09M
            if (op->value.intval >= 0)
491
4.09M
                return 0;
492
12
            break;
493
4.27M
    }
494
83.5k
    return zneg(i_ctx_p);
495
4.27M
}
496
497
/* <num1> ceiling <num2> */
498
int
499
zceiling(i_ctx_t *i_ctx_p)
500
14
{
501
14
    os_ptr op = osp;
502
503
14
    check_op(1);
504
13
    switch (r_type(op)) {
505
0
        default:
506
0
            return_op_typecheck(op);
507
10
        case t_real:
508
10
            op->value.realval = ceil(op->value.realval);
509
13
        case t_integer:;
510
13
    }
511
13
    return 0;
512
13
}
513
514
/* <num1> floor <num2> */
515
int
516
zfloor(i_ctx_t *i_ctx_p)
517
35
{
518
35
    os_ptr op = osp;
519
520
35
    check_op(1);
521
34
    switch (r_type(op)) {
522
0
        default:
523
0
            return_op_typecheck(op);
524
21
        case t_real:
525
21
            op->value.realval = floor(op->value.realval);
526
34
        case t_integer:;
527
34
    }
528
34
    return 0;
529
34
}
530
531
/* <num1> round <num2> */
532
int
533
zround(i_ctx_t *i_ctx_p)
534
449
{
535
449
    os_ptr op = osp;
536
537
449
    check_op(1);
538
448
    switch (r_type(op)) {
539
0
        default:
540
0
            return_op_typecheck(op);
541
68
        case t_real:
542
68
            op->value.realval = floor(op->value.realval + 0.5);
543
448
        case t_integer:;
544
448
    }
545
448
    return 0;
546
448
}
547
548
/* <num1> truncate <num2> */
549
int
550
ztruncate(i_ctx_t *i_ctx_p)
551
610k
{
552
610k
    os_ptr op = osp;
553
554
610k
    check_op(1);
555
610k
    switch (r_type(op)) {
556
0
        default:
557
0
            return_op_typecheck(op);
558
94
        case t_real:
559
94
            op->value.realval =
560
94
                (op->value.realval < 0.0 ?
561
3
                 ceil(op->value.realval) :
562
94
                 floor(op->value.realval));
563
610k
        case t_integer:;
564
610k
    }
565
610k
    return 0;
566
610k
}
567
568
/* Non-standard operators */
569
570
/* <int1> <int2> .bitadd <sum> */
571
static int
572
zbitadd(i_ctx_t *i_ctx_p)
573
0
{
574
0
    os_ptr op = osp;
575
576
0
    check_op(2);
577
0
    check_type(*op, t_integer);
578
0
    check_type(op[-1], t_integer);
579
0
    op[-1].value.intval += op->value.intval;
580
0
    pop(1);
581
0
    return 0;
582
0
}
583
584
/* ------ Initialization table ------ */
585
586
const op_def zarith_op_defs[] =
587
{
588
    {"1abs", zabs},
589
    {"2add", zadd},
590
    {"2.bitadd", zbitadd},
591
    {"1ceiling", zceiling},
592
    {"2div", zdiv},
593
    {"2idiv", zidiv},
594
    {"1floor", zfloor},
595
    {"2mod", zmod},
596
    {"2mul", zmul},
597
    {"1neg", zneg},
598
    {"1round", zround},
599
    {"2sub", zsub},
600
    {"1truncate", ztruncate},
601
    op_def_end(0)
602
};