-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy patharithmetic.c
2333 lines (2048 loc) · 64.6 KB
/
arithmetic.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1998--2023 The R Core Team.
* Copyright (C) 2003--2023 The R Foundation
* Copyright (C) 1995--1997 Robert Gentleman and Ross Ihaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
/* ====
NOTE: The [dpq]<foo>() distribution functions in math2, math3, math4 are *NOT* used from R,
==== as [dpqr]<foo>() functions are in stats,
and the C code wrappers are all in ../library/stats/src/distn.c <<< keep in SYNC !!!
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
// LDBL_EPSILON
#include <float.h>
/* interval at which to check interrupts, a guess */
#define NINTERRUPT 10000000
#ifdef __OpenBSD__
/* for definition of "struct exception" in math.h */
# define __LIBM_PRIVATE
#endif
#include <Defn.h> /*-> Arith.h -> math.h */
#ifdef __OpenBSD__
# undef __LIBM_PRIVATE
#endif
#include <Internal.h>
#define R_MSG_NA _("NaNs produced")
#define R_MSG_NONNUM_MATH _("non-numeric argument to mathematical function")
#include <Rmath.h>
#include <R_ext/Itermacros.h>
#include "arithmetic.h"
#include <errno.h>
/* Override for matherr removed for R 4.4.0 */
/* Intel compilers for Linux do have matherr, but they do not have the
defines in math.h. So we skip this for Intel */
typedef union
{
double value;
unsigned int word[2];
} ieee_double;
/* gcc had problems with static const on AIX and Solaris
Solaris was for gcc 3.1 and 3.2 under -O2 32-bit on 64-bit kernel */
#ifdef _AIX
#define CONST
#elif defined(sparc) && defined (__GNUC__) && __GNUC__ == 3
#define CONST
#else
#define CONST const
#endif
#ifdef WORDS_BIGENDIAN
static CONST int hw = 0;
static CONST int lw = 1;
#else /* !WORDS_BIGENDIAN */
static CONST int hw = 1;
static CONST int lw = 0;
#endif /* WORDS_BIGENDIAN */
static double R_ValueOfNA(void)
{
/* The gcc (3.2.1?) shipping with Red Hat Linux 9 gets this wrong
* without the volatile declaration. Thanks to Marc Schwartz. */
volatile ieee_double x;
x.word[hw] = 0x7ff00000;
x.word[lw] = 1954;
return x.value;
}
/* is a value known to be a NaN also an R NA? */
attribute_hidden int R_NaN_is_R_NA(double x)
{
ieee_double y;
y.value = x;
return (y.word[lw] == 1954);
}
int R_IsNA(double x)
{
return isnan(x) && R_NaN_is_R_NA(x);
}
int R_IsNaN(double x)
{
return isnan(x) && ! R_NaN_is_R_NA(x);
}
/* ISNAN uses isnan, which is undefined by C++ headers
This workaround is called only when ISNAN() is used
in a user code in a file with __cplusplus defined */
int R_isnancpp(double x)
{
return (isnan(x)!=0);
}
/* Mainly for use in packages */
int R_finite(double x)
{
#ifdef HAVE_WORKING_ISFINITE
return isfinite(x);
#else
return (!isnan(x) & (x != R_PosInf) & (x != R_NegInf));
#endif
}
/* Arithmetic Initialization */
attribute_hidden void InitArithmetic(void)
{
R_NaInt = INT_MIN;
R_NaReal = R_ValueOfNA();
// we assume C99, so
#ifndef OLD
R_NaN = NAN;
R_PosInf = INFINITY;
R_NegInf = -INFINITY;
#else
R_NaN = 0.0/R_Zero_Hack;
R_PosInf = 1.0/R_Zero_Hack;
R_NegInf = -1.0/R_Zero_Hack;
#endif
}
#if HAVE_LONG_DOUBLE && (SIZEOF_LONG_DOUBLE > SIZEOF_DOUBLE)
/*
# ifdef __powerpc__
// PowerPC 64 (when gcc has -mlong-double-128) fails constant folding with LDOUBLE
// Debian Bug#946836 shows it is needed also for 32-bit ppc, not just __PPC64__
// NB: 1 / LDBL_EPSILON has been seen to overflow on 'ppc64el ...
==> use eps instead of 1 / eps (and one multiplication more)
*/
# define c_eps LDBL_EPSILON
#else
# define c_eps DBL_EPSILON
#endif
/* Keep myfmod() and myfloor() in step */
static double myfmod(double x1, double x2)
{
if (x2 == 0.0) return R_NaN;
if(fabs(x2) * c_eps > 1 && R_FINITE(x1) && fabs(x1) <= fabs(x2)) {
return
(fabs(x1) == fabs(x2)) ? 0 :
((x1 < 0 && x2 > 0) ||
(x2 < 0 && x1 > 0))
? x1+x2 // differing signs
: x1 ; // "same" signs (incl. 0)
}
double q = x1 / x2;
if(R_FINITE(q) && (fabs(q) * c_eps > 1))
warning(_("probable complete loss of accuracy in modulus"));
LDOUBLE tmp = (LDOUBLE)x1 - floor(q) * (LDOUBLE)x2;
return (double) (tmp - floorl(tmp/x2) * x2);
}
static double myfloor(double x1, double x2)
{
double q = x1 / x2;
if (x2 == 0.0 || fabs(q) * c_eps > 1 || !R_FINITE(q))
return q;
if(fabs(q) < 1)
return (q < 0) ? -1
: ((x1 < 0 && x2 > 0) ||
(x1 > 0 && x2 < 0) // differing signs
? -1 : 0);
LDOUBLE tmp = (LDOUBLE)x1 - floor(q) * (LDOUBLE)x2;
return (double) (floor(q) + floorl(tmp/x2));
}
double R_pow(double x, double y) /* = x ^ y */
{
/* squaring is the most common of the specially handled cases so
check for it first. */
if(y == 2.0)
return x * x;
if(x == 1. || y == 0.)
return(1.);
if(x == 0.) {
if(y > 0.) return(0.);
else if(y < 0) return(R_PosInf);
else return(y); /* NA or NaN, we assert */
}
if (R_FINITE(x) && R_FINITE(y)) {
/* There was a special case for y == 0.5 here, but
gcc 4.3.0 -g -O2 mis-compiled it. Showed up with
100^0.5 as 3.162278, example(pbirthday) failed. */
#ifdef USE_POWL_IN_R_POW
// this is used only on 64-bit Windows (so has powl).
return powl(x, y);
#else
return pow(x, y);
#endif
}
if (ISNAN(x) || ISNAN(y))
return(x + y);
if(!R_FINITE(x)) {
if(x > 0) /* Inf ^ y */
return (y < 0.)? 0. : R_PosInf;
else { /* (-Inf) ^ y */
if(R_FINITE(y) && y == floor(y)) /* (-Inf) ^ n */
return (y < 0.) ? 0. : (myfmod(y, 2.) != 0 ? x : -x);
}
}
if(!R_FINITE(y)) {
if(x >= 0) {
if(y > 0) /* y == +Inf */
return (x >= 1) ? R_PosInf : 0.;
else /* y == -Inf */
return (x < 1) ? R_PosInf : 0.;
}
}
return R_NaN; // all other cases: (-Inf)^{+-Inf, non-int}; (neg)^{+-Inf}
}
double R_pow_di(double x, int n)
{
double xn = 1.0;
if (ISNAN(x)) return x;
if (n == NA_INTEGER) return NA_REAL;
if (n != 0) {
if (!R_FINITE(x)) return R_POW(x, (double)n);
Rboolean is_neg = (n < 0);
if(is_neg) n = -n;
for(;;) {
if(n & 01) xn *= x;
if(n >>= 1) x *= x; else break;
}
if(is_neg) xn = 1. / xn;
}
return xn;
}
/* General Base Logarithms */
SEXP R_unary(SEXP, SEXP, SEXP);
SEXP R_binary(SEXP, SEXP, SEXP, SEXP);
static SEXP logical_unary(ARITHOP_TYPE, SEXP, SEXP);
static SEXP integer_unary(ARITHOP_TYPE, SEXP, SEXP);
static SEXP real_unary(ARITHOP_TYPE, SEXP, SEXP);
static SEXP real_binary(ARITHOP_TYPE, SEXP, SEXP);
static SEXP integer_binary(ARITHOP_TYPE, SEXP, SEXP, SEXP);
#if 0
static int naflag;
static SEXP lcall;
#endif
/* Integer arithmetic support */
/* The tests using integer comparisons are a bit faster than the tests
using doubles, but they depend on a two's complement representation
(but that is almost universal). The tests that compare results to
double's depend on being able to accurately represent all int's as
double's. Since int's are almost universally 32 bit that should be
OK. */
#ifndef INT_32_BITS
/* configure checks whether int is 32 bits. If not this code will
need to be rewritten. Since 32 bit ints are pretty much universal,
we can worry about writing alternate code when the need arises.
To be safe, we signal a compiler error if int is not 32 bits. */
# error code requires that int have 32 bits
#endif
#define INTEGER_OVERFLOW_WARNING _("NAs produced by integer overflow")
#define CHECK_INTEGER_OVERFLOW(call, ans, naflag) do { \
if (naflag) { \
PROTECT(ans); \
warningcall(call, INTEGER_OVERFLOW_WARNING); \
UNPROTECT(1); \
} \
} while(0)
#define R_INT_MAX INT_MAX
#define R_INT_MIN -INT_MAX
// .. relying on fact that NA_INTEGER is outside of these
static R_INLINE int R_integer_plus(int x, int y, Rboolean *pnaflag)
{
if (x == NA_INTEGER || y == NA_INTEGER)
return NA_INTEGER;
if (((y > 0) && (x > (R_INT_MAX - y))) ||
((y < 0) && (x < (R_INT_MIN - y)))) {
if (pnaflag != NULL)
*pnaflag = TRUE;
return NA_INTEGER;
}
return x + y;
}
static R_INLINE int R_integer_minus(int x, int y, Rboolean *pnaflag)
{
if (x == NA_INTEGER || y == NA_INTEGER)
return NA_INTEGER;
if (((y < 0) && (x > (R_INT_MAX + y))) ||
((y > 0) && (x < (R_INT_MIN + y)))) {
if (pnaflag != NULL)
*pnaflag = TRUE;
return NA_INTEGER;
}
return x - y;
}
#define GOODIPROD(x, y, z) ((double) (x) * (double) (y) == (z))
static R_INLINE int R_integer_times(int x, int y, Rboolean *pnaflag)
{
if (x == NA_INTEGER || y == NA_INTEGER)
return NA_INTEGER;
else {
int z = x * y; // UBSAN will warn if this overflows (happens in bda)
if (GOODIPROD(x, y, z) && z != NA_INTEGER)
return z;
else {
if (pnaflag != NULL)
*pnaflag = TRUE;
return NA_INTEGER;
}
}
}
static R_INLINE double R_integer_divide(int x, int y)
{
if (x == NA_INTEGER || y == NA_INTEGER)
return NA_REAL;
else
return (double) x / (double) y;
}
static R_INLINE SEXP ScalarValue1(SEXP x)
{
if (NO_REFERENCES(x))
return x;
else
return allocVector(TYPEOF(x), 1);
}
static R_INLINE SEXP ScalarValue2(SEXP x, SEXP y)
{
if (NO_REFERENCES(x))
return x;
else if (NO_REFERENCES(y))
return y;
else
return allocVector(TYPEOF(x), 1);
}
/* Unary and Binary Operators */
attribute_hidden SEXP do_arith(SEXP call, SEXP op, SEXP args, SEXP env)
{
int argc;
if (args == R_NilValue)
argc = 0;
else if (CDR(args) == R_NilValue)
argc = 1;
else if (CDDR(args) == R_NilValue)
argc = 2;
else
argc = length(args);
SEXP ans,
arg1 = CAR(args),
arg2 = CADR(args);
if (ATTRIB(arg1) != R_NilValue || ATTRIB(arg2) != R_NilValue) {
if (DispatchGroup("Ops", call, op, args, env, &ans))
return ans;
}
else if (argc == 2) {
/* Handle some scaler operations immediately */
if (IS_SCALAR(arg1, REALSXP)) {
double x1 = SCALAR_DVAL(arg1);
if (IS_SCALAR(arg2, REALSXP)) {
double x2 = SCALAR_DVAL(arg2);
ans = ScalarValue2(arg1, arg2);
switch (PRIMVAL(op)) {
case PLUSOP: SET_SCALAR_DVAL(ans, x1 + x2); return ans;
case MINUSOP: SET_SCALAR_DVAL(ans, x1 - x2); return ans;
case TIMESOP: SET_SCALAR_DVAL(ans, x1 * x2); return ans;
case DIVOP: SET_SCALAR_DVAL(ans, x1 / x2); return ans;
}
}
else if (IS_SCALAR(arg2, INTSXP)) {
int i2 = SCALAR_IVAL(arg2);
double x2 = i2 != NA_INTEGER ? (double) i2 : NA_REAL;
ans = ScalarValue1(arg1);
switch (PRIMVAL(op)) {
case PLUSOP: SET_SCALAR_DVAL(ans, x1 + x2); return ans;
case MINUSOP: SET_SCALAR_DVAL(ans, x1 - x2); return ans;
case TIMESOP: SET_SCALAR_DVAL(ans, x1 * x2); return ans;
case DIVOP: SET_SCALAR_DVAL(ans, x1 / x2); return ans;
}
}
}
else if (IS_SCALAR(arg1, INTSXP)) {
int i1 = SCALAR_IVAL(arg1);
if (IS_SCALAR(arg2, REALSXP)) {
double x1 = i1 != NA_INTEGER ? (double) i1 : NA_REAL;
double x2 = SCALAR_DVAL(arg2);
ans = ScalarValue1(arg2);
switch (PRIMVAL(op)) {
case PLUSOP: SET_SCALAR_DVAL(ans, x1 + x2); return ans;
case MINUSOP: SET_SCALAR_DVAL(ans, x1 - x2); return ans;
case TIMESOP: SET_SCALAR_DVAL(ans, x1 * x2); return ans;
case DIVOP: SET_SCALAR_DVAL(ans, x1 / x2); return ans;
}
}
else if (IS_SCALAR(arg2, INTSXP)) {
Rboolean naflag = FALSE;
int i2 = SCALAR_IVAL(arg2);
switch (PRIMVAL(op)) {
case PLUSOP:
ans = ScalarValue2(arg1, arg2);
SET_SCALAR_IVAL(ans, R_integer_plus(i1, i2, &naflag));
CHECK_INTEGER_OVERFLOW(call, ans, naflag);
return ans;
case MINUSOP:
ans = ScalarValue2(arg1, arg2);
SET_SCALAR_IVAL(ans, R_integer_minus(i1, i2, &naflag));
CHECK_INTEGER_OVERFLOW(call, ans, naflag);
return ans;
case TIMESOP:
ans = ScalarValue2(arg1, arg2);
SET_SCALAR_IVAL(ans, R_integer_times(i1, i2, &naflag));
CHECK_INTEGER_OVERFLOW(call, ans, naflag);
return ans;
case DIVOP:
return ScalarReal(R_integer_divide(i1, i2));
}
}
}
}
else if (argc == 1) {
if (IS_SCALAR(arg1, REALSXP)) {
switch(PRIMVAL(op)) {
case PLUSOP: return(arg1);
case MINUSOP:
ans = ScalarValue1(arg1);
SET_SCALAR_DVAL(ans, -SCALAR_DVAL(arg1));
return ans;
}
}
else if (IS_SCALAR(arg1, INTSXP)) {
int ival;
switch(PRIMVAL(op)) {
case PLUSOP: return(arg1);
case MINUSOP:
ival = SCALAR_IVAL(arg1);
ans = ScalarValue1(arg1);
SET_SCALAR_IVAL(ans, ival == NA_INTEGER ? NA_INTEGER : -ival);
return ans;
}
}
}
if (argc == 2)
return R_binary(call, op, arg1, arg2);
else if (argc == 1)
return R_unary(call, op, arg1);
else
errorcall(call,_("operator needs one or two arguments"));
return ans; /* never used; to keep -Wall happy */
}
#define COERCE_IF_NEEDED(v, tp, vpi) do { \
if (TYPEOF(v) != (tp)) { \
int __vo__ = OBJECT(v); \
REPROTECT(v = coerceVector(v, (tp)), vpi); \
if (__vo__) SET_OBJECT(v, 1); \
} \
} while (0)
#define FIXUP_NULL_AND_CHECK_TYPES(v, vpi) do { \
switch (TYPEOF(v)) { \
case NILSXP: REPROTECT(v = allocVector(INTSXP,0), vpi); break; \
case CPLXSXP: case REALSXP: case INTSXP: case LGLSXP: break; \
default: errorcall(call, _("non-numeric argument to binary operator")); \
} \
} while (0)
attribute_hidden SEXP R_binary(SEXP call, SEXP op, SEXP x, SEXP y)
{
Rboolean xattr, yattr, xarray, yarray, xts, yts, xS4, yS4;
PROTECT_INDEX xpi, ypi;
ARITHOP_TYPE oper = (ARITHOP_TYPE) PRIMVAL(op);
int nprotect = 2; /* x and y */
PROTECT_WITH_INDEX(x, &xpi);
PROTECT_WITH_INDEX(y, &ypi);
FIXUP_NULL_AND_CHECK_TYPES(x, xpi);
FIXUP_NULL_AND_CHECK_TYPES(y, ypi);
R_xlen_t
nx = XLENGTH(x),
ny = XLENGTH(y);
if (ATTRIB(x) != R_NilValue) {
xattr = TRUE;
xarray = isArray(x);
xts = isTs(x);
xS4 = isS4(x);
}
else xattr = xarray = xts = xS4 = FALSE;
if (ATTRIB(y) != R_NilValue) {
yattr = TRUE;
yarray = isArray(y);
yts = isTs(y);
yS4 = isS4(y);
}
else yattr = yarray = yts = yS4 = FALSE;
#define R_ARITHMETIC_ARRAY_1_SPECIAL
#ifdef R_ARITHMETIC_ARRAY_1_SPECIAL
/* If either x or y is a matrix with length 1 and the other is a
vector of a different length, we want to coerce the matrix to be a vector.
Do we want to? We don't do it! BDR 2004-03-06
From 3.4.0 (Sep. 2016), this signals a warning,
and in the future we will disable these 2 clauses,
so it will give an error.
*/
/* FIXME: Danger Will Robinson.
* ----- We might be trashing arguments here.
*/
if (xarray != yarray) {
if (xarray && nx==1 && ny!=1) {
if(ny != 0)
warningcall(call, _(
"Recycling array of length 1 in array-vector arithmetic is deprecated.\n\
Use c() or as.vector() instead."));
REPROTECT(x = duplicate(x), xpi);
setAttrib(x, R_DimSymbol, R_NilValue);
}
if (yarray && ny==1 && nx!=1) {
if(nx != 0)
warningcall(call, _(
"Recycling array of length 1 in vector-array arithmetic is deprecated.\n\
Use c() or as.vector() instead."));
REPROTECT(y = duplicate(y), ypi);
setAttrib(y, R_DimSymbol, R_NilValue);
}
}
#endif
SEXP dims, xnames, ynames;
if (xarray || yarray) {
/* if one is a length-atleast-1-array and the
* other is a length-0 *non*array, then do not use array treatment */
if (xarray && yarray) {
if (!conformable(x, y))
errorcall(call, _("non-conformable arrays"));
PROTECT(dims = getAttrib(x, R_DimSymbol)); nprotect++;
}
else if (xarray && (ny != 0 || nx == 0)) {
PROTECT(dims = getAttrib(x, R_DimSymbol)); nprotect++;
}
else if (yarray && (nx != 0 || ny == 0)) {
PROTECT(dims = getAttrib(y, R_DimSymbol)); nprotect++;
} else
dims = R_NilValue;
if (xattr) {
PROTECT(xnames = getAttrib(x, R_DimNamesSymbol));
nprotect++;
}
else xnames = R_NilValue;
if (yattr) {
PROTECT(ynames = getAttrib(y, R_DimNamesSymbol));
nprotect++;
}
else ynames = R_NilValue;
}
else {
dims = R_NilValue;
if (xattr) {
PROTECT(xnames = getAttrib(x, R_NamesSymbol));
nprotect++;
}
else xnames = R_NilValue;
if (yattr) {
PROTECT(ynames = getAttrib(y, R_NamesSymbol));
nprotect++;
}
else ynames = R_NilValue;
}
SEXP klass = NULL, tsp = NULL; // -Wall
if (xts || yts) {
if (xts && yts) {
/* could check ts conformance here */
PROTECT(tsp = getAttrib(x, R_TspSymbol));
PROTECT(klass = getAttrib(x, R_ClassSymbol));
}
else if (xts) {
if (nx < ny)
ErrorMessage(call, ERROR_TSVEC_MISMATCH);
PROTECT(tsp = getAttrib(x, R_TspSymbol));
PROTECT(klass = getAttrib(x, R_ClassSymbol));
}
else { /* (yts) */
if (ny < nx)
ErrorMessage(call, ERROR_TSVEC_MISMATCH);
PROTECT(tsp = getAttrib(y, R_TspSymbol));
PROTECT(klass = getAttrib(y, R_ClassSymbol));
}
nprotect += 2;
}
if (nx > 0 && ny > 0 &&
((nx > ny) ? nx % ny : ny % nx) != 0) // mismatch
warningcall(call,
_("longer object length is not a multiple of shorter object length"));
SEXP val;
/* need to preserve object here, as *_binary copies class attributes */
if (TYPEOF(x) == CPLXSXP || TYPEOF(y) == CPLXSXP) {
COERCE_IF_NEEDED(x, CPLXSXP, xpi);
COERCE_IF_NEEDED(y, CPLXSXP, ypi);
val = complex_binary(oper, x, y);
}
else if (TYPEOF(x) == REALSXP || TYPEOF(y) == REALSXP) {
/* real_binary can handle REALSXP or INTSXP operand, but not LGLSXP. */
/* Can get a LGLSXP. In base-Ex.R on 24 Oct '06, got 8 of these. */
if (TYPEOF(x) != INTSXP) COERCE_IF_NEEDED(x, REALSXP, xpi);
if (TYPEOF(y) != INTSXP) COERCE_IF_NEEDED(y, REALSXP, ypi);
val = real_binary(oper, x, y);
}
else val = integer_binary(oper, x, y, call);
/* quick return if there are no attributes */
if (! xattr && ! yattr) {
UNPROTECT(nprotect);
return val;
}
PROTECT(val);
nprotect++;
if (dims != R_NilValue) {
setAttrib(val, R_DimSymbol, dims);
if (xnames != R_NilValue)
setAttrib(val, R_DimNamesSymbol, xnames);
else if (ynames != R_NilValue)
setAttrib(val, R_DimNamesSymbol, ynames);
}
else {
if (XLENGTH(val) == xlength(xnames))
setAttrib(val, R_NamesSymbol, xnames);
else if (XLENGTH(val) == xlength(ynames))
setAttrib(val, R_NamesSymbol, ynames);
}
if (xts || yts) { /* must set *after* dims! */
setAttrib(val, R_TspSymbol, tsp);
setAttrib(val, R_ClassSymbol, klass);
}
if(xS4 || yS4) { /* Only set the bit: no method defined! */
val = asS4(val, TRUE, TRUE);
}
UNPROTECT(nprotect);
return val;
}
attribute_hidden SEXP R_unary(SEXP call, SEXP op, SEXP s1)
{
ARITHOP_TYPE operation = (ARITHOP_TYPE) PRIMVAL(op);
switch (TYPEOF(s1)) {
case LGLSXP:
return logical_unary(operation, s1, call);
case INTSXP:
return integer_unary(operation, s1, call);
case REALSXP:
return real_unary(operation, s1, call);
case CPLXSXP:
return complex_unary(operation, s1, call);
default:
errorcall(call, _("invalid argument to unary operator"));
}
return s1; /* never used; to keep -Wall happy */
}
static SEXP logical_unary(ARITHOP_TYPE code, SEXP s1, SEXP call)
{
R_xlen_t n = XLENGTH(s1);
SEXP ans = PROTECT(allocVector(INTSXP, n));
SEXP names = PROTECT(getAttrib(s1, R_NamesSymbol));
SEXP dim = PROTECT(getAttrib(s1, R_DimSymbol));
SEXP dimnames = PROTECT(getAttrib(s1, R_DimNamesSymbol));
if(names != R_NilValue) setAttrib(ans, R_NamesSymbol, names);
if(dim != R_NilValue) setAttrib(ans, R_DimSymbol, dim);
if(dimnames != R_NilValue) setAttrib(ans, R_DimNamesSymbol, dimnames);
UNPROTECT(3);
int *pa = INTEGER(ans);
const int *px = LOGICAL_RO(s1);
switch (code) {
case PLUSOP:
for (R_xlen_t i = 0; i < n; i++) pa[i] = px[i];
break;
case MINUSOP:
for (R_xlen_t i = 0; i < n; i++) {
int x = px[i];
pa[i] = (x == NA_INTEGER) ?
NA_INTEGER : ((x == 0.0) ? 0 : -x);
}
break;
default:
errorcall(call, _("invalid unary operator"));
}
UNPROTECT(1);
return ans;
}
static SEXP integer_unary(ARITHOP_TYPE code, SEXP s1, SEXP call)
{
R_xlen_t i, n;
SEXP ans;
switch (code) {
case PLUSOP:
return s1;
case MINUSOP:
ans = NO_REFERENCES(s1) ? s1 : duplicate(s1);
int *pa = INTEGER(ans);
const int *px = INTEGER_RO(s1);
n = XLENGTH(s1);
for (i = 0; i < n; i++) {
int x = px[i];
pa[i] = (x == NA_INTEGER) ?
NA_INTEGER : ((x == 0.0) ? 0 : -x);
}
return ans;
default:
errorcall(call, _("invalid unary operator"));
}
return s1; /* never used; to keep -Wall happy */
}
static SEXP real_unary(ARITHOP_TYPE code, SEXP s1, SEXP lcall)
{
R_xlen_t i, n;
SEXP ans;
switch (code) {
case PLUSOP: return s1;
case MINUSOP:
ans = NO_REFERENCES(s1) ? s1 : duplicate(s1);
double *pa = REAL(ans);
const double *px = REAL_RO(s1);
n = XLENGTH(s1);
for (i = 0; i < n; i++)
pa[i] = -px[i];
return ans;
default:
errorcall(lcall, _("invalid unary operator"));
}
return s1; /* never used; to keep -Wall happy */
}
static SEXP integer_binary(ARITHOP_TYPE code, SEXP s1, SEXP s2, SEXP lcall)
{
R_xlen_t i, i1, i2, n, n1, n2;
int x1, x2;
SEXP ans;
Rboolean naflag = FALSE;
n1 = XLENGTH(s1);
n2 = XLENGTH(s2);
/* S4-compatibility change: if n1 or n2 is 0, result is of length 0 */
if (n1 == 0 || n2 == 0) n = 0; else n = (n1 > n2) ? n1 : n2;
if (code == DIVOP || code == POWOP)
ans = allocVector(REALSXP, n);
else
ans = R_allocOrReuseVector(s1, s2, INTSXP, n);
if (n == 0) return(ans);
PROTECT(ans);
switch (code) {
case PLUSOP:
{
int *pa = INTEGER(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
pa[i] = R_integer_plus(x1, x2, &naflag);
});
if (naflag)
warningcall(lcall, INTEGER_OVERFLOW_WARNING);
}
break;
case MINUSOP:
{
int *pa = INTEGER(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
pa[i] = R_integer_minus(x1, x2, &naflag);
});
if (naflag)
warningcall(lcall, INTEGER_OVERFLOW_WARNING);
}
break;
case TIMESOP:
{
int *pa = INTEGER(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
pa[i] = R_integer_times(x1, x2, &naflag);
});
if (naflag)
warningcall(lcall, INTEGER_OVERFLOW_WARNING);
}
break;
case DIVOP:
{
double *pa = REAL(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
pa[i] = R_integer_divide(x1, x2);
});
}
break;
case POWOP:
{
double *pa = REAL(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
if((x1 = px1[i1]) == 1 || (x2 = px2[i2]) == 0)
pa[i] = 1.;
else if (x1 == NA_INTEGER || x2 == NA_INTEGER)
pa[i] = NA_REAL;
else
pa[i] = R_POW((double) x1, (double) x2);
});
}
break;
case MODOP:
{
int *pa = INTEGER(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
if (x1 == NA_INTEGER || x2 == NA_INTEGER || x2 == 0)
pa[i] = NA_INTEGER;
else {
pa[i] = /* till 0.63.2: x1 % x2 */
(x1 >= 0 && x2 > 0) ? x1 % x2 :
(int)myfmod((double)x1,(double)x2);
}
});
}
break;
case IDIVOP:
{
int *pa = INTEGER(ans);
const int *px1 = INTEGER_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2, {
x1 = px1[i1];
x2 = px2[i2];
/* This had x %/% 0 == 0 prior to 2.14.1, but
it seems conventionally to be undefined */
if (x1 == NA_INTEGER || x2 == NA_INTEGER || x2 == 0)
pa[i] = NA_INTEGER;
else
pa[i] = (int) floor((double)x1 / (double)x2);
});
}
break;
}
UNPROTECT(1);
/* quick return if there are no attributes */
if (ATTRIB(s1) == R_NilValue && ATTRIB(s2) == R_NilValue)
return ans;
/* Copy attributes from longer argument. */
if (ans != s2 && n == n2 && ATTRIB(s2) != R_NilValue)
copyMostAttrib(s2, ans);
if (ans != s1 && n == n1 && ATTRIB(s1) != R_NilValue)
copyMostAttrib(s1, ans); /* Done 2nd so s1's attrs overwrite s2's */
return ans;
}
#define R_INTEGER(x) (double) ((x) == NA_INTEGER ? NA_REAL : (x))
static SEXP real_binary(ARITHOP_TYPE code, SEXP s1, SEXP s2)
{
R_xlen_t i, i1, i2, n, n1, n2;
SEXP ans;
/* Note: "s1" and "s2" are protected above. */
n1 = XLENGTH(s1);
n2 = XLENGTH(s2);
/* S4-compatibility change: if n1 or n2 is 0, result is of length 0 */
if (n1 == 0 || n2 == 0) return(allocVector(REALSXP, 0));
n = (n1 > n2) ? n1 : n2;
PROTECT(ans = R_allocOrReuseVector(s1, s2, REALSXP, n));
switch (code) {
case PLUSOP:
if(TYPEOF(s1) == REALSXP && TYPEOF(s2) == REALSXP) {
double *da = REAL(ans);
const double *dx = REAL_RO(s1);
const double *dy = REAL_RO(s2);
if (n2 == 1) {
double tmp = dy[0];
R_ITERATE_CHECK(NINTERRUPT, n, i, da[i] = dx[i] + tmp;);
}
else if (n1 == 1) {
double tmp = dx[0];
R_ITERATE_CHECK(NINTERRUPT, n, i, da[i] = tmp + dy[i];);
}
else if (n1 == n2)
R_ITERATE_CHECK(NINTERRUPT, n, i, da[i] = dx[i] + dy[i];);
else
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2,
da[i] = dx[i1] + dy[i2];);
}
else if(TYPEOF(s1) == INTSXP ) {
double *da = REAL(ans);
const int *px1 = INTEGER_RO(s1);
const double *px2 = REAL_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2,
da[i] = R_INTEGER(px1[i1]) + px2[i2];);
}
else if(TYPEOF(s2) == INTSXP ) {
double *da = REAL(ans);
const double *px1 = REAL_RO(s1);
const int *px2 = INTEGER_RO(s2);
MOD_ITERATE2_CHECK(NINTERRUPT, n, n1, n2, i, i1, i2,
da[i] = px1[i1] + R_INTEGER(px2[i2]););
}
break;
case MINUSOP:
if(TYPEOF(s1) == REALSXP && TYPEOF(s2) == REALSXP) {
double *da = REAL(ans);
const double *dx = REAL_RO(s1);