|
| 1 | +# %% and %/% |
| 2 | + |
| 3 | + Code |
| 4 | + fn |
| 5 | + Output |
| 6 | + function(a, b) { |
| 7 | + declare(type(a = double(n)), type(b = double(n))) |
| 8 | + a %% b |
| 9 | + } |
| 10 | + <environment: 0x0> |
| 11 | + Code |
| 12 | + cat(fsub) |
| 13 | + Output |
| 14 | + subroutine fn(a, b, out_, a__len_) bind(c) |
| 15 | + use iso_c_binding, only: c_double, c_ptrdiff_t |
| 16 | + implicit none |
| 17 | + |
| 18 | + ! manifest start |
| 19 | + ! sizes |
| 20 | + integer(c_ptrdiff_t), intent(in), value :: a__len_ |
| 21 | + |
| 22 | + ! args |
| 23 | + real(c_double), intent(in) :: a(a__len_) |
| 24 | + real(c_double), intent(in) :: b(a__len_) |
| 25 | + real(c_double), intent(out) :: out_(a__len_) |
| 26 | + ! manifest end |
| 27 | + |
| 28 | + |
| 29 | + out_ = modulo(a, b) |
| 30 | + end subroutine |
| 31 | + Code |
| 32 | + cat(cwrapper) |
| 33 | + Output |
| 34 | + #define R_NO_REMAP |
| 35 | + #include <R.h> |
| 36 | + #include <Rinternals.h> |
| 37 | + |
| 38 | + |
| 39 | + extern void fn( |
| 40 | + const double* const a__, |
| 41 | + const double* const b__, |
| 42 | + double* const out___, |
| 43 | + const R_xlen_t a__len_); |
| 44 | + |
| 45 | + SEXP fn_(SEXP _args) { |
| 46 | + // a |
| 47 | + _args = CDR(_args); |
| 48 | + SEXP a = CAR(_args); |
| 49 | + if (TYPEOF(a) != REALSXP) { |
| 50 | + Rf_error("typeof(a) must be 'double', not '%s'", R_typeToChar(a)); |
| 51 | + } |
| 52 | + const double* const a__ = REAL(a); |
| 53 | + const R_xlen_t a__len_ = Rf_xlength(a); |
| 54 | + |
| 55 | + // b |
| 56 | + _args = CDR(_args); |
| 57 | + SEXP b = CAR(_args); |
| 58 | + if (TYPEOF(b) != REALSXP) { |
| 59 | + Rf_error("typeof(b) must be 'double', not '%s'", R_typeToChar(b)); |
| 60 | + } |
| 61 | + const double* const b__ = REAL(b); |
| 62 | + const R_xlen_t b__len_ = Rf_xlength(b); |
| 63 | + |
| 64 | + if (a__len_ != b__len_) |
| 65 | + Rf_error("length(b) must equal length(a)," |
| 66 | + " but are %0.f and %0.f", |
| 67 | + (double)b__len_, (double)a__len_); |
| 68 | + const R_xlen_t out___len_ = a__len_; |
| 69 | + SEXP out_ = PROTECT(Rf_allocVector(REALSXP, out___len_)); |
| 70 | + double* out___ = REAL(out_); |
| 71 | + |
| 72 | + fn( |
| 73 | + a__, |
| 74 | + b__, |
| 75 | + out___, |
| 76 | + a__len_); |
| 77 | + |
| 78 | + UNPROTECT(1); |
| 79 | + return out_; |
| 80 | + } |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | + Code |
| 85 | + fn |
| 86 | + Output |
| 87 | + function(a, b) { |
| 88 | + declare(type(a = double(n)), type(b = double(n))) |
| 89 | + a %/% b |
| 90 | + } |
| 91 | + <environment: 0x0> |
| 92 | + Code |
| 93 | + cat(fsub) |
| 94 | + Output |
| 95 | + subroutine fn(a, b, out_, a__len_) bind(c) |
| 96 | + use iso_c_binding, only: c_double, c_ptrdiff_t |
| 97 | + implicit none |
| 98 | + |
| 99 | + ! manifest start |
| 100 | + ! sizes |
| 101 | + integer(c_ptrdiff_t), intent(in), value :: a__len_ |
| 102 | + |
| 103 | + ! args |
| 104 | + real(c_double), intent(in) :: a(a__len_) |
| 105 | + real(c_double), intent(in) :: b(a__len_) |
| 106 | + real(c_double), intent(out) :: out_(a__len_) |
| 107 | + ! manifest end |
| 108 | + |
| 109 | + |
| 110 | + out_ = floor(a / b) |
| 111 | + end subroutine |
| 112 | + Code |
| 113 | + cat(cwrapper) |
| 114 | + Output |
| 115 | + #define R_NO_REMAP |
| 116 | + #include <R.h> |
| 117 | + #include <Rinternals.h> |
| 118 | + |
| 119 | + |
| 120 | + extern void fn( |
| 121 | + const double* const a__, |
| 122 | + const double* const b__, |
| 123 | + double* const out___, |
| 124 | + const R_xlen_t a__len_); |
| 125 | + |
| 126 | + SEXP fn_(SEXP _args) { |
| 127 | + // a |
| 128 | + _args = CDR(_args); |
| 129 | + SEXP a = CAR(_args); |
| 130 | + if (TYPEOF(a) != REALSXP) { |
| 131 | + Rf_error("typeof(a) must be 'double', not '%s'", R_typeToChar(a)); |
| 132 | + } |
| 133 | + const double* const a__ = REAL(a); |
| 134 | + const R_xlen_t a__len_ = Rf_xlength(a); |
| 135 | + |
| 136 | + // b |
| 137 | + _args = CDR(_args); |
| 138 | + SEXP b = CAR(_args); |
| 139 | + if (TYPEOF(b) != REALSXP) { |
| 140 | + Rf_error("typeof(b) must be 'double', not '%s'", R_typeToChar(b)); |
| 141 | + } |
| 142 | + const double* const b__ = REAL(b); |
| 143 | + const R_xlen_t b__len_ = Rf_xlength(b); |
| 144 | + |
| 145 | + if (a__len_ != b__len_) |
| 146 | + Rf_error("length(b) must equal length(a)," |
| 147 | + " but are %0.f and %0.f", |
| 148 | + (double)b__len_, (double)a__len_); |
| 149 | + const R_xlen_t out___len_ = a__len_; |
| 150 | + SEXP out_ = PROTECT(Rf_allocVector(REALSXP, out___len_)); |
| 151 | + double* out___ = REAL(out_); |
| 152 | + |
| 153 | + fn( |
| 154 | + a__, |
| 155 | + b__, |
| 156 | + out___, |
| 157 | + a__len_); |
| 158 | + |
| 159 | + UNPROTECT(1); |
| 160 | + return out_; |
| 161 | + } |
| 162 | + |
0 commit comments