Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zarith.c
Line
Count
Source
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
605M
{
37
605M
    register os_ptr op = osp;
38
605M
    float result;
39
40
605M
    check_op(2);
41
605M
    switch (r_type(op)) {
42
11
    default:
43
11
        return_op_typecheck(op);
44
52.7M
    case t_real:
45
52.7M
        switch (r_type(op - 1)) {
46
14
        default:
47
14
            return_op_typecheck(op - 1);
48
29.2M
        case t_real:
49
29.2M
            result = op[-1].value.realval + op->value.realval;
50
29.2M
#ifdef HAVE_ISINF
51
29.2M
            if (isinf(result))
52
3
                return_error(gs_error_undefinedresult);
53
29.2M
#endif
54
29.2M
#ifdef HAVE_ISNAN
55
29.2M
            if (isnan(result))
56
0
                return_error(gs_error_undefinedresult);
57
29.2M
#endif
58
29.2M
            op[-1].value.realval = result;
59
29.2M
            break;
60
23.4M
        case t_integer:
61
23.4M
            make_real(op - 1, (double)op[-1].value.intval + op->value.realval);
62
52.7M
        }
63
52.7M
        break;
64
552M
    case t_integer:
65
552M
        switch (r_type(op - 1)) {
66
9
        default:
67
9
            return_op_typecheck(op - 1);
68
59.8M
        case t_real:
69
59.8M
            result = op[-1].value.realval + (double)op->value.intval;
70
59.8M
#ifdef HAVE_ISINF
71
59.8M
            if (isinf(result))
72
2
                return_error(gs_error_undefinedresult);
73
59.8M
#endif
74
59.8M
#ifdef HAVE_ISNAN
75
59.8M
            if (isnan(result))
76
0
                return_error(gs_error_undefinedresult);
77
59.8M
#endif
78
59.8M
            op[-1].value.realval = result;
79
59.8M
            break;
80
493M
        case t_integer: {
81
493M
            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
493M
            else {
95
493M
                ps_int int2 = op->value.intval;
96
97
493M
                if (((op[-1].value.intval += int2) ^ int2) < 0 &&
98
2.30k
                    ((op[-1].value.intval - int2) ^ int2) >= 0
99
493M
                    ) {                     /* Overflow, convert to real */
100
349
                    make_real(op - 1, (double)(op[-1].value.intval - int2) + int2);
101
349
                }
102
493M
            }
103
493M
        }
104
552M
        }
105
605M
    }
106
605M
    return 0;
107
605M
}
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
36.7M
{
123
36.7M
    os_ptr op = osp;
124
36.7M
    os_ptr op1 = op - 1;
125
36.7M
    float result;
126
127
36.7M
    check_op(2);
128
    /* We can't use the non_int_cases macro, */
129
    /* because we have to check explicitly for op == 0. */
130
36.7M
    switch (r_type(op)) {
131
10
        default:
132
10
            return_op_typecheck(op);
133
1.99M
        case t_real:
134
1.99M
            if (op->value.realval == 0)
135
7
                return_error(gs_error_undefinedresult);
136
1.99M
            switch (r_type(op1)) {
137
6
                default:
138
6
                    return_op_typecheck(op1);
139
1.91M
                case t_real:
140
1.91M
                    result = op1->value.realval / op->value.realval;
141
1.91M
#ifdef HAVE_ISINF
142
1.91M
                    if (isinf(result))
143
5
                        return_error(gs_error_undefinedresult);
144
1.91M
#endif
145
1.91M
#ifdef HAVE_ISNAN
146
1.91M
                    if (isnan(result))
147
1
                        return_error(gs_error_undefinedresult);
148
1.91M
#endif
149
1.91M
                    op1->value.realval = result;
150
1.91M
                    break;
151
81.9k
                case t_integer:
152
81.9k
                    result = (double)op1->value.intval / op->value.realval;
153
81.9k
#ifdef HAVE_ISINF
154
81.9k
                    if (isinf(result))
155
0
                        return_error(gs_error_undefinedresult);
156
81.9k
#endif
157
81.9k
#ifdef HAVE_ISNAN
158
81.9k
                    if (isnan(result))
159
0
                        return_error(gs_error_undefinedresult);
160
81.9k
#endif
161
1.99M
                    make_real(op1, result);
162
1.99M
            }
163
1.99M
            break;
164
34.7M
        case t_integer:
165
34.7M
            if (op->value.intval == 0)
166
12
                return_error(gs_error_undefinedresult);
167
34.7M
            switch (r_type(op1)) {
168
7
                default:
169
7
                    return_op_typecheck(op1);
170
26.4M
                case t_real:
171
26.4M
                    result = op1->value.realval / (double)op->value.intval;
172
26.4M
#ifdef HAVE_ISINF
173
26.4M
                    if (isinf(result))
174
0
                        return_error(gs_error_undefinedresult);
175
26.4M
#endif
176
26.4M
#ifdef HAVE_ISNAN
177
26.4M
                    if (isnan(result))
178
0
                        return_error(gs_error_undefinedresult);
179
26.4M
#endif
180
26.4M
                    op1->value.realval = result;
181
26.4M
                    break;
182
8.30M
                case t_integer:
183
8.30M
                    result = (double)op1->value.intval / (double)op->value.intval;
184
8.30M
#ifdef HAVE_ISINF
185
8.30M
                    if (isinf(result))
186
0
                        return_error(gs_error_undefinedresult);
187
8.30M
#endif
188
8.30M
#ifdef HAVE_ISNAN
189
8.30M
                    if (isnan(result))
190
0
                        return_error(gs_error_undefinedresult);
191
8.30M
#endif
192
34.7M
                    make_real(op1, result);
193
34.7M
            }
194
36.7M
    }
195
36.7M
    pop(1);
196
36.7M
    return 0;
197
36.7M
}
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
18.2M
{
221
18.2M
  uint32_t b = (abc>>32);
222
18.2M
  uint32_t c = (uint32_t)abc;
223
18.2M
  uint32_t e = (def>>32);
224
18.2M
  uint32_t f = (uint32_t)def;
225
18.2M
  uint64_t low, mid, high, bf, ec;
226
227
  /* Low contribution */
228
18.2M
  low = (uint64_t)c * (uint64_t)f;
229
  /* Mid contributions */
230
18.2M
  bf = (uint64_t)b * (uint64_t)f;
231
18.2M
  ec = (uint64_t)e * (uint64_t)c;
232
  /* Top contribution */
233
18.2M
  high = (uint64_t)b * (uint64_t)e;
234
18.2M
  if (abc < 0)
235
347
      high -= def;
236
18.2M
  if (def < 0)
237
43
      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
18.2M
  if (bf > ~ec)
245
14
      high += ((uint64_t)1)<<32;
246
18.2M
  mid = bf + ec;
247
18.2M
  if (low > ~(mid<<32))
248
14
      high += 1;
249
18.2M
  high += (mid>>32);
250
18.2M
  low += (mid<<32);
251
252
18.2M
  *res = low;
253
254
18.2M
  return (int64_t)low < 0 ? high != -1 : high != 0;
255
18.2M
}
256
257
/* <num1> <num2> mul <product> */
258
int
259
zmul(i_ctx_t *i_ctx_p)
260
133M
{
261
133M
    os_ptr op = osp;
262
133M
    float result;
263
264
133M
    switch (r_type(op)) {
265
22
    default:
266
22
        return_op_typecheck(op);
267
66.1M
    case t_real:
268
66.1M
        switch (r_type(op - 1)) {
269
12
        default:
270
12
            return_op_typecheck(op - 1);
271
65.1M
        case t_real:
272
65.1M
            result = op[-1].value.realval * op->value.realval;
273
65.1M
#ifdef HAVE_ISINF
274
65.1M
            if (isinf(result))
275
23
                return_error(gs_error_undefinedresult);
276
65.1M
#endif
277
65.1M
#ifdef HAVE_ISNAN
278
65.1M
            if (isnan(result))
279
0
                return_error(gs_error_undefinedresult);
280
65.1M
#endif
281
65.1M
            op[-1].value.realval = result;
282
65.1M
            break;
283
922k
        case t_integer:
284
922k
            result = (double)op[-1].value.intval * op->value.realval;
285
922k
            make_real(op - 1, result);
286
66.1M
        }
287
66.1M
        break;
288
67.4M
    case t_integer:
289
67.4M
        switch (r_type(op - 1)) {
290
10
        default:
291
10
            return_op_typecheck(op - 1);
292
49.1M
        case t_real:
293
49.1M
            result = op[-1].value.realval * (double)op->value.intval;
294
49.1M
#ifdef HAVE_ISINF
295
49.1M
            if (isinf(result))
296
3
                return_error(gs_error_undefinedresult);
297
49.1M
#endif
298
49.1M
#ifdef HAVE_ISNAN
299
49.1M
            if (isnan(result))
300
0
                return_error(gs_error_undefinedresult);
301
49.1M
#endif
302
49.1M
            op[-1].value.realval = result;
303
49.1M
            break;
304
18.2M
        case t_integer: {
305
18.2M
            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
18.2M
            else {
315
18.2M
                int64_t result;
316
18.2M
                if (mul_64_64_overflowcheck(op[-1].value.intval, op->value.intval, &result)) {
317
1
                    double ab = (double)op[-1].value.intval * op->value.intval;
318
1
                    make_real(op - 1, ab);
319
18.2M
                } else {
320
18.2M
                    op[-1].value.intval = result;
321
18.2M
                }
322
18.2M
            }
323
18.2M
        }
324
67.4M
        }
325
133M
    }
326
133M
    pop(1);
327
133M
    return 0;
328
133M
}
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
101M
{
336
101M
    register os_ptr op = osp;
337
338
101M
    check_op(2);
339
101M
    switch (r_type(op)) {
340
89
    default:
341
89
        return_op_typecheck(op);
342
25.8M
    case t_real:
343
25.8M
        switch (r_type(op - 1)) {
344
15
        default:
345
15
            return_op_typecheck(op - 1);
346
346k
        case t_real:
347
346k
            op[-1].value.realval -= op->value.realval;
348
346k
            break;
349
25.4M
        case t_integer:
350
25.4M
            make_real(op - 1, (double)op[-1].value.intval - op->value.realval);
351
25.8M
        }
352
25.8M
        break;
353
75.7M
    case t_integer:
354
75.7M
        switch (r_type(op - 1)) {
355
7
        default:
356
7
            return_op_typecheck(op - 1);
357
64.7k
        case t_real:
358
64.7k
            op[-1].value.realval -= (double)op->value.intval;
359
64.7k
            break;
360
75.7M
        case t_integer: {
361
75.7M
            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
75.7M
            else {
376
75.7M
                ps_int int1 = op[-1].value.intval;
377
378
75.7M
                if ((int1 ^ (op[-1].value.intval = int1 - op->value.intval)) < 0 &&
379
99.0k
                    (int1 ^ op->value.intval) < 0
380
75.7M
                    ) {                     /* Overflow, convert to real */
381
0
                    make_real(op - 1, (float)int1 - op->value.intval);
382
0
                }
383
75.7M
            }
384
75.7M
        }
385
75.7M
        }
386
101M
    }
387
101M
    return 0;
388
101M
}
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
45.9M
{
404
45.9M
    os_ptr op = osp;
405
406
45.9M
    check_op(2);
407
45.9M
    check_type(*op, t_integer);
408
45.9M
    check_type(op[-1], t_integer);
409
45.9M
    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
45.9M
    else {
419
45.9M
        if ((op->value.intval == 0) || (op[-1].value.intval == MIN_PS_INT && op->value.intval == -1)) {
420
            /* Anomalous boundary case: -MININT / -1, fail. */
421
5
            return_error(gs_error_undefinedresult);
422
5
        }
423
45.9M
        op[-1].value.intval /= op->value.intval;
424
45.9M
    }
425
45.9M
    pop(1);
426
45.9M
    return 0;
427
45.9M
}
428
429
/* <int1> <int2> mod <remainder> */
430
int
431
zmod(i_ctx_t *i_ctx_p)
432
7.16M
{
433
7.16M
    os_ptr op = osp;
434
435
7.16M
    check_op(2);
436
7.16M
    check_type(*op, t_integer);
437
7.16M
    check_type(op[-1], t_integer);
438
7.16M
    if (op->value.intval == 0 || op[-1].value.intval == MIN_PS_INT)
439
7
        return_error(gs_error_undefinedresult);
440
7.16M
    op[-1].value.intval %= op->value.intval;
441
7.16M
    pop(1);
442
7.16M
    return 0;
443
7.16M
}
444
445
/* <num1> neg <num2> */
446
int
447
zneg(i_ctx_t *i_ctx_p)
448
5.28M
{
449
5.28M
    os_ptr op = osp;
450
451
5.28M
    check_op(1);
452
5.28M
    switch (r_type(op)) {
453
8
        default:
454
8
            return_op_typecheck(op);
455
2.23M
        case t_real:
456
2.23M
            op->value.realval = -op->value.realval;
457
2.23M
            break;
458
3.04M
        case t_integer:
459
3.04M
            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
3.04M
            else {
466
3.04M
                if (op->value.intval == MIN_PS_INT)
467
3.04M
                    make_real(op, -(float)MIN_PS_INT);
468
3.04M
                else
469
3.04M
                    op->value.intval = -op->value.intval;
470
3.04M
            }
471
5.28M
    }
472
5.28M
    return 0;
473
5.28M
}
474
475
/* <num1> abs <num2> */
476
int
477
zabs(i_ctx_t *i_ctx_p)
478
10.2M
{
479
10.2M
    os_ptr op = osp;
480
481
10.2M
    check_op(1);
482
10.2M
    switch (r_type(op)) {
483
87
        default:
484
87
            return_op_typecheck(op);
485
3.22M
        case t_real:
486
3.22M
            if (op->value.realval >= 0)
487
1.78M
                return 0;
488
1.44M
            break;
489
7.00M
        case t_integer:
490
7.00M
            if (op->value.intval >= 0)
491
7.00M
                return 0;
492
128
            break;
493
10.2M
    }
494
1.44M
    return zneg(i_ctx_p);
495
10.2M
}
496
497
/* <num1> ceiling <num2> */
498
int
499
zceiling(i_ctx_t *i_ctx_p)
500
228
{
501
228
    os_ptr op = osp;
502
503
228
    check_op(1);
504
216
    switch (r_type(op)) {
505
11
        default:
506
11
            return_op_typecheck(op);
507
120
        case t_real:
508
120
            op->value.realval = ceil(op->value.realval);
509
205
        case t_integer:;
510
216
    }
511
205
    return 0;
512
216
}
513
514
/* <num1> floor <num2> */
515
int
516
zfloor(i_ctx_t *i_ctx_p)
517
276
{
518
276
    os_ptr op = osp;
519
520
276
    check_op(1);
521
265
    switch (r_type(op)) {
522
9
        default:
523
9
            return_op_typecheck(op);
524
121
        case t_real:
525
121
            op->value.realval = floor(op->value.realval);
526
256
        case t_integer:;
527
265
    }
528
256
    return 0;
529
265
}
530
531
/* <num1> round <num2> */
532
int
533
zround(i_ctx_t *i_ctx_p)
534
5.17k
{
535
5.17k
    os_ptr op = osp;
536
537
5.17k
    check_op(1);
538
5.16k
    switch (r_type(op)) {
539
13
        default:
540
13
            return_op_typecheck(op);
541
869
        case t_real:
542
869
            op->value.realval = floor(op->value.realval + 0.5);
543
5.15k
        case t_integer:;
544
5.16k
    }
545
5.15k
    return 0;
546
5.16k
}
547
548
/* <num1> truncate <num2> */
549
int
550
ztruncate(i_ctx_t *i_ctx_p)
551
32.3k
{
552
32.3k
    os_ptr op = osp;
553
554
32.3k
    check_op(1);
555
32.3k
    switch (r_type(op)) {
556
7
        default:
557
7
            return_op_typecheck(op);
558
1.43k
        case t_real:
559
1.43k
            op->value.realval =
560
1.43k
                (op->value.realval < 0.0 ?
561
13
                 ceil(op->value.realval) :
562
1.43k
                 floor(op->value.realval));
563
32.3k
        case t_integer:;
564
32.3k
    }
565
32.3k
    return 0;
566
32.3k
}
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
};