summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/missing.h
blob: 11b58c099d54717dccb25050fe9a2401b3c23b80 (plain)
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
#ifndef MISSING_H
#define MISSING_H 1

#if defined(__cplusplus)
extern "C" {
#if 0
} /* satisfy cc-mode */
#endif
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#ifdef HAVE_MATH_H
# include <math.h>
#endif

#ifndef RB_UNUSED_VAR
# if defined(_MSC_VER) && _MSC_VER >= 1911
#  define RB_UNUSED_VAR(x) x [[maybe_unused]]

# elif defined(__has_cpp_attribute) && __has_cpp_attribute(maybe_unused)
#  define RB_UNUSED_VAR(x) x [[maybe_unused]]

# elif defined(__has_c_attribute) && __has_c_attribute(maybe_unused)
#  define RB_UNUSED_VAR(x) x [[maybe_unused]]

# elif defined(__GNUC__)
#  define RB_UNUSED_VAR(x) x __attribute__ ((unused))

# else
#  define RB_UNUSED_VAR(x) x
# endif
#endif /* RB_UNUSED_VAR */

#if defined(_MSC_VER) && _MSC_VER >= 1310
# define HAVE___ASSUME

#elif defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300
# define HAVE___ASSUME
#endif

#ifndef UNREACHABLE
# if __has_builtin(__builtin_unreachable)
#  define UNREACHABLE __builtin_unreachable()

# elif HAVE___ASSUME
#  define UNREACHABLE __assume(0)

# else
#  define UNREACHABLE		/* unreachable */
# endif
#endif /* UNREACHABLE */

/* bool */

#if defined(__bool_true_false_are_defined)
# /* Take that. */

#elif defined(HAVE_STDBOOL_H)
# include <stdbool.h>

#else
typedef unsigned char _Bool;
# define bool _Bool
# define true  ((_Bool)+1)
# define false ((_Bool)-1)
# define __bool_true_false_are_defined
#endif

/* abs */

#ifndef HAVE_LABS
static inline long
labs(long const x)
{
    if (x < 0) return -x;
    return x;
}
#endif

#ifndef HAVE_LLABS
static inline LONG_LONG
llabs(LONG_LONG const x)
{
    if (x < 0) return -x;
    return x;
}
#endif

#ifdef vabs
# undef vabs
#endif
#if SIZEOF_VALUE <= SIZEOF_INT
# define vabs abs
#elif SIZEOF_VALUE <= SIZEOF_LONG
# define vabs labs
#elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
# define vabs llabs
#endif

/* finite */

#ifndef HAVE_FINITE
static int
finite(double)
{
    return !isnan(n) && !isinf(n);
}
#endif

#ifndef isfinite
# ifndef HAVE_ISFINITE
#  define HAVE_ISFINITE 1
#  define isfinite(x) finite(x)
# endif
#endif

/* dtoa */
char *BigDecimal_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve);

/* rational */

#ifndef HAVE_RB_RATIONAL_NUM
static inline VALUE
rb_rational_num(VALUE rat)
{
#ifdef HAVE_TYPE_STRUCT_RRATIONAL
    return RRATIONAL(rat)->num;
#else
    return rb_funcall(rat, rb_intern("numerator"), 0);
#endif
}
#endif

#ifndef HAVE_RB_RATIONAL_DEN
static inline VALUE
rb_rational_den(VALUE rat)
{
#ifdef HAVE_TYPE_STRUCT_RRATIONAL
    return RRATIONAL(rat)->den;
#else
    return rb_funcall(rat, rb_intern("denominator"), 0);
#endif
}
#endif

/* complex */

#ifndef HAVE_RB_COMPLEX_REAL
static inline VALUE
rb_complex_real(VALUE cmp)
{
#ifdef HAVE_TYPE_STRUCT_RCOMPLEX
  return RCOMPLEX(cmp)->real;
#else
  return rb_funcall(cmp, rb_intern("real"), 0);
#endif
}
#endif

#ifndef HAVE_RB_COMPLEX_IMAG
static inline VALUE
rb_complex_imag(VALUE cmp)
{
# ifdef HAVE_TYPE_STRUCT_RCOMPLEX
  return RCOMPLEX(cmp)->imag;
# else
  return rb_funcall(cmp, rb_intern("imag"), 0);
# endif
}
#endif

/* array */

#ifndef FIX_CONST_VALUE_PTR
# if defined(__fcc__) || defined(__fcc_version) || \
    defined(__FCC__) || defined(__FCC_VERSION)
/* workaround for old version of Fujitsu C Compiler (fcc) */
#  define FIX_CONST_VALUE_PTR(x) ((const VALUE *)(x))
# else
#  define FIX_CONST_VALUE_PTR(x) (x)
# endif
#endif

#ifndef HAVE_RB_ARRAY_CONST_PTR
static inline const VALUE *
rb_array_const_ptr(VALUE a)
{
    return FIX_CONST_VALUE_PTR((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ?
	RARRAY(a)->as.ary : RARRAY(a)->as.heap.ptr);
}
#endif

#ifndef RARRAY_CONST_PTR
# define RARRAY_CONST_PTR(a) rb_array_const_ptr(a)
#endif

#ifndef RARRAY_AREF
# define RARRAY_AREF(a, i) (RARRAY_CONST_PTR(a)[i])
#endif

/* symbol */

#ifndef HAVE_RB_SYM2STR
static inline VALUE
rb_sym2str(VALUE sym)
{
    return rb_id2str(SYM2ID(sym));
}
#endif

/* st */

#ifndef ST2FIX
# undef RB_ST2FIX
# define RB_ST2FIX(h) LONG2FIX((long)(h))
# define ST2FIX(h) RB_ST2FIX(h)
#endif

/* warning */

#if !defined(HAVE_RB_CATEGORY_WARN) || !defined(HAVE_CONST_RB_WARN_CATEGORY_DEPRECATED)
#   define rb_category_warn(category, ...) rb_warn(__VA_ARGS__)
#endif

#if defined(__cplusplus)
#if 0
{ /* satisfy cc-mode */
#endif
}  /* extern "C" { */
#endif

#endif /* MISSING_H */