Coverage Report

Created: 2025-06-10 06:59

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