defs.h /size: 3381 b    last modification: 2025-02-21 11:03
1/* Library libcerf:
2 *   compute complex error functions,
3 *   along with Dawson, Faddeeva and Voigt functions
4 *
5 * File defs.h:
6 *   Language-dependent includes.
7 *
8 * Copyright:
9 *   (C) 2012 Massachusetts Institute of Technology
10 *   (C) 2013 Forschungszentrum Jülich GmbH
11 *
12 * Licence:
13 *   MIT Licence.
14 *   See ../COPYING
15 *
16 * Authors:
17 *   Steven G. Johnson, Massachusetts Institute of Technology, 2012, core author
18 *   Joachim Wuttke, Forschungszentrum Jülich, 2013, package maintainer
19 *
20 * Website:
21 *   http://apps.jcns.fz-juelich.de/libcerf
22 */
23
24/*
25
26    This file is patched by Mojca Miklavec and Hans Hagen for usage in LuaMetaTeX where we use
27    only C and also want to compile with the Microsoft compiler. So, when updating this library
28    one has to check for changes. Not that we expect many as this is a rather stable library.
29
30    In the other files there are a few macros used that deal with the multiplication and addition
31    of complex and real nmbers. Of course the original code is kept as-is.
32
33*/
34
35# ifndef __CERF_C_H
36#   define __CERF_C_H
37
38# define _GNU_SOURCE // enable GNU libc NAN extension if possible
39
40/*
41    Constructing complex numbers like 0+i*NaN is problematic in C99
42    without the C11 CMPLX macro, because 0.+I*NAN may give NaN+i*NAN if
43    I is a complex (rather than imaginary) constant.  For some reason,
44    however, it works fine in (pre-4.7) gcc if I define Inf and NaN as
45    1/0 and 0/0 (and only if I compile with optimization -O1 or more),
46    but not if I use the INFINITY or NAN macros.
47*/
48
49/*
50    __builtin_complex was introduced in gcc 4.7, but the C11 CMPLX
51    macro may not be defined unless we are using a recent (2012) version
52    of glibc and compile with -std=c11... note that icc lies about being
53    gcc and probably doesn't have this builtin(?), so exclude icc
54    explicitly.
55*/
56
57# if (_MSC_VER)
58    # define C(a,b) _Cbuild((double)(a), (double)(b))
59    # define Inf    INFINITY
60    # define NaN    NAN
61# else
62    # define C(a,b) ((a) + I*(b))
63    # define Inf    (1./0.)
64    # define NaN    (0./0.)
65# endif
66
67# include <complex.h>
68
69# if (_MSC_VER)
70
71    # define _cerf_cmplx _Dcomplex
72
73    static _Dcomplex complex_neg   (_Dcomplex x)              { return _Cmulcr(x, -1.0); }
74    static _Dcomplex complex_add_cc(_Dcomplex x, _Dcomplex y) { return _Cbuild(creal(x) + creal(y), cimag(x) + cimag(y)); }
75    static _Dcomplex complex_add_rc(double    x, _Dcomplex y) { return _Cbuild(x + creal(y), x + cimag(y)); }
76    static _Dcomplex complex_sub_cc(_Dcomplex x, _Dcomplex y) { return _Cbuild(creal(x) - creal(y), cimag(x) - cimag(y)); }
77    static _Dcomplex complex_sub_rc(double    x, _Dcomplex y) { return _Cbuild(x - creal(y), x - cimag(y)); }
78    static _Dcomplex complex_mul_cc(_Dcomplex x, _Dcomplex y) { return _Cmulcc((y), (x)); }
79    static _Dcomplex complex_mul_rc(double    x, _Dcomplex y) { return _Cmulcr((y), (x)); }
80    static _Dcomplex complex_mul_cr(_Dcomplex x, double    y) { return _Cmulcr((x), (y)); }
81
82# else
83
84    typedef double _Complex _cerf_cmplx;
85
86    # define complex_neg(x)       (-x)
87    # define complex_add_cc(x,y)  (x+y)
88    # define complex_add_rc(x,y)  (x+y)
89    # define complex_sub_cc(x,y)  (x-y)
90    # define complex_sub_rc(x,y)  (x-y)
91    # define complex_mul_cc(x,y)  (x*y)
92    # define complex_mul_rc(x,y)  (x*y)
93    # define complex_mul_cr(x,y)  (x*y)
94
95# endif
96
97# endif
98