| 1 | #ifndef _f2cP_h_ |
| 2 | #define _f2cP_h_ |
| 3 | /* f2cP.h |
| 4 | * |
| 5 | * Copyright (C) 2020 David Weenink, 2026 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "melder.h" |
| 22 | #include "cblas.h" |
| 23 | |
| 24 | static inline double d_abs (double *x) { |
| 25 | return abs (*x); |
| 26 | } |
| 27 | |
| 28 | static inline double d_acos (double *x) { |
| 29 | return acos (*x); |
| 30 | } |
| 31 | |
| 32 | static inline double d_asin (double *x) { |
| 33 | return asin (*x); |
| 34 | } |
| 35 | |
| 36 | static inline double d_atan (double *x) { |
| 37 | return atan (*x); |
| 38 | } |
| 39 | |
| 40 | static inline double d_atn2 (double *x, double *y) { |
| 41 | return atan2 (*x,*y); |
| 42 | } |
| 43 | |
| 44 | static inline double d_cos (double *x) { |
| 45 | return cos (*x); |
| 46 | } |
| 47 | |
| 48 | static inline double d_cosh (double *x) { |
| 49 | return cosh (*x); |
| 50 | } |
| 51 | |
| 52 | static inline double d_dim (double *a, double *b) { |
| 53 | return ( *a > *b ? *a - *b : 0 ); |
| 54 | } |
| 55 | |
| 56 | static inline double d_exp (double *x) { |
| 57 | return exp (*x); |
| 58 | } |
| 59 | |
| 60 | static inline double d_int (double *x) { |
| 61 | return ( *x>0 ? floor(*x) : -floor(- *x) ); |
| 62 | } |
| 63 | |
| 64 | static inline double d_lg10 (double *x) { |
| 65 | return log10 (*x); |
| 66 | } |
| 67 | |
| 68 | static inline double d_log (double *x) { |
| 69 | return log (*x); |
| 70 | } |
| 71 | |
| 72 | // TODO djmw is this std::fmod (*x, *y) ?? |
| 73 | static inline double d_mod (double *x, double *y) { |
| 74 | #ifdef IEEE_drem |
| 75 | double ya = *y; |
| 76 | if (ya < 0.0) |
| 77 | ya = -ya; |
| 78 | double xa = *x; |
| 79 | double z = drem (xa, ya); |
| 80 | if (xa > 0) { |
| 81 | if (z < 0) |
| 82 | z += ya; |
| 83 | } |
| 84 | else if (z > 0) |
| 85 | z -= ya; |
| 86 | return z; |
| 87 | #else |
| 88 | double quotient = *x / *y; |
| 89 | if (quotient >= 0) |
| 90 | quotient = floor (quotient); |
| 91 | else |
| 92 | quotient = -floor(-quotient); |
| 93 | return (*x - (*y) * quotient ); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | static inline double d_prod (float *x, float *y) { |
| 98 | return (*x) * (*y); |
| 99 | } |
| 100 | |
| 101 | static inline double d_sign (double *a, double *b) { |
| 102 | double x = (*a >= 0 ? *a : - *a); |
| 103 | return ( *b >= 0 ? x : -x ); |
| 104 | } |
| 105 | |
| 106 | static inline double d_sin (double *x) { |
| 107 | return sin (*x); |
| 108 | } |
| 109 | |
| 110 | static inline double d_sinh (double *x) { |
| 111 | return sinh (*x); |
| 112 | } |
| 113 | |
| 114 | static inline double d_sqrt (double *x) { |
| 115 | return sqrt (*x); |
| 116 | } |
| 117 | |
| 118 | static inline double d_tan (double *x) { |
| 119 | return tan (*x); |
| 120 | } |
| 121 | |
| 122 | static inline double d_tanh (double *x) { |
| 123 | return tanh (*x); |
| 124 | } |
| 125 | |
| 126 | static inline double derf_ (double *x) { |
| 127 | return erf (*x); |
| 128 | } |
| 129 | |
| 130 | static inline double derfc_ (double *x) { |
| 131 | return erfc (*x); |
| 132 | } |
| 133 | |
| 134 | static inline integer i_dnnt (double *x) { |
| 135 | return (integer)(*x >= 0. ? floor (*x + .5) : -floor(.5 - *x)); |
| 136 | } |
| 137 | |
| 138 | static inline integer i_nint (float *x) { |
| 139 | return (integer)(*x >= 0.0 ? floor(*x + 0.5) : -floor(0.5 - *x)); |
| 140 | } |
| 141 | |
| 142 | bool lsame_(const char *ca, const char *cb); |
| 143 | |
| 144 | bool lsamen_(integer *n, const char *ca, const char *cb); |
| 145 | |
| 146 | static inline double pow_dd (double *ap, double *bp) { |
| 147 | return pow (*ap, *bp); |
| 148 | } |
| 149 | |
| 150 | static inline double pow_di (double *ap, integer *bp) { |
| 151 | double pow = 1.0; |
| 152 | double x = *ap; |
| 153 | integer n = *bp; |
| 154 | if (n != 0) { |
| 155 | if (n < 0) { |
| 156 | n = -n; |
| 157 | x = 1.0 / x; |
| 158 | } |
| 159 | for(unsigned long u = n; ; ) { |
| 160 | if (u & 01) |
| 161 | pow *= x; |
| 162 | if (u >>= 1) |
| 163 | x *= x; |
| 164 | else |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | return pow; |
| 169 | } |
| 170 | |
| 171 | static inline integer pow_ii (integer *ap, integer *bp) { |
| 172 | integer pow, x = *ap, n = *bp; |
| 173 | |
| 174 | if (n <= 0) { |
| 175 | if (n == 0 || x == 1) |
| 176 | return 1; |
| 177 | if (x != -1) |
| 178 | return x == 0 ? 1/x : 0; |
| 179 | n = -n; |
| 180 | } |
| 181 | unsigned long u = n; |
| 182 | for (pow = 1; ; ) { |
| 183 | if(u & 01) |
| 184 | pow *= x; |
| 185 | if(u >>= 1) |
| 186 | x *= x; |
| 187 | else |
| 188 | break; |
| 189 | } |
| 190 | return pow; |
| 191 | } |
| 192 | |
| 193 | static inline double pow_ri (float *ap, integer *bp) { |
| 194 | double pow = 1.0, x = *ap; |
| 195 | integer n = *bp; |
| 196 | if (n != 0) { |
| 197 | if (n < 0) { |
| 198 | n = -n; |
| 199 | x = 1/x; |
| 200 | } |
| 201 | for (unsigned long u = n; ; ) { |
| 202 | if(u & 01) |
| 203 | pow *= x; |
| 204 | if(u >>= 1) |
| 205 | x *= x; |
| 206 | else |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | return pow; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | Unless compiled with -DNO_OVERWRITE, this variant of s_copy allows the |
| 215 | target of an assignment to appear on its right-hand side (contrary |
| 216 | to the Fortran 77 Standard, but in accordance with Fortran 90), as in a(2:5) = a(4:7). |
| 217 | */ |
| 218 | static inline void s_copy (char *a, const char *b, integer la, integer lb) { |
| 219 | char *aend = a + la; |
| 220 | if(la <= lb) |
| 221 | #ifndef NO_OVERWRITE |
| 222 | if (a <= b || a >= b + la) |
| 223 | #endif |
| 224 | while(a < aend) |
| 225 | *a++ = *b++; |
| 226 | #ifndef NO_OVERWRITE |
| 227 | else |
| 228 | for(b += la; a < aend; ) |
| 229 | *--aend = *--b; |
| 230 | #endif |
| 231 | |
| 232 | else { |
| 233 | char *bend = const_cast<char *>(b) + lb; |
| 234 | #ifndef NO_OVERWRITE |
| 235 | if (a <= b || a >= bend) |
| 236 | #endif |
| 237 | while(b < bend) |
| 238 | *a++ = *b++; |
| 239 | #ifndef NO_OVERWRITE |
| 240 | else { |
| 241 | a += lb; |
| 242 | while(b < bend) |
| 243 | *--a = *--bend; |
| 244 | a += lb; |
| 245 | } |
| 246 | #endif |
| 247 | while(a < aend) |
| 248 | *a++ = ' '; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static inline void s_cat (char *lp, char *rpp[], integer rnp [], integer *np, integer ll) { |
| 253 | char *rp; |
| 254 | integer n = *np; |
| 255 | #ifndef NO_OVERWRITE |
| 256 | integer L, m; |
| 257 | char *lp0, *lp1; |
| 258 | |
| 259 | lp0 = 0; |
| 260 | lp1 = lp; |
| 261 | L = ll; |
| 262 | integer i = 0; |
| 263 | while (i < n) { |
| 264 | rp = rpp [i]; |
| 265 | m = rnp [i++]; |
| 266 | if (rp >= lp1 || rp + m <= lp) { |
| 267 | if ((L -= m) <= 0) { |
| 268 | n = i; |
| 269 | break; |
| 270 | } |
| 271 | lp1 += m; |
| 272 | continue; |
| 273 | } |
| 274 | lp0 = lp; |
| 275 | lp = lp1 = Melder_malloc_f (char, L = ll); |
| 276 | break; |
| 277 | } |
| 278 | lp1 = lp; |
| 279 | #endif /* NO_OVERWRITE */ |
| 280 | for(i = 0 ; i < n ; ++ i) { |
| 281 | integer nc = ll; |
| 282 | if(rnp [i] < nc) |
| 283 | nc = rnp [i]; |
| 284 | ll -= nc; |
| 285 | rp = rpp [i]; |
| 286 | while(--nc >= 0) |
| 287 | *lp++ = *rp++; |
| 288 | } |
| 289 | while(--ll >= 0) |
| 290 | *lp++ = ' '; |
| 291 | #ifndef NO_OVERWRITE |
| 292 | if (lp0) { |
| 293 | memcpy(lp0, lp1, L); |
| 294 | Melder_free (lp1); |
| 295 | } |
| 296 | #endif |
| 297 | } |
| 298 | |
| 299 | static inline integer s_cmp (const char *a0, const char *b0, integer la, integer lb) { |
| 300 | unsigned char *a = (unsigned char *)a0; |
| 301 | unsigned char *b = (unsigned char *)b0; |
| 302 | unsigned char *aend = a + la; |
| 303 | unsigned char *bend = b + lb; |
| 304 | if(la <= lb) { |
| 305 | while(a < aend) |
| 306 | if(*a != *b) |
| 307 | return( *a - *b ); |
| 308 | else { |
| 309 | ++a; |
| 310 | ++b; |
| 311 | } |
| 312 | while(b < bend) |
| 313 | if(*b != ' ') |
| 314 | return( ' ' - *b ); |
| 315 | else ++b; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static inline void xerbla_ (const char *src, integer *info) { |
| 321 | Melder_throw (Melder_peek8to32_u (src), U": parameter ", *info, U" not correct!"); |
| 322 | } |
| 323 | |
| 324 | #endif /* _f2cP_h_ */ |