gumath 0.2.0dev5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +61 -0
  3. data/Gemfile +5 -0
  4. data/History.md +0 -0
  5. data/README.md +5 -0
  6. data/Rakefile +105 -0
  7. data/ext/ruby_gumath/examples.c +126 -0
  8. data/ext/ruby_gumath/extconf.rb +97 -0
  9. data/ext/ruby_gumath/functions.c +106 -0
  10. data/ext/ruby_gumath/gufunc_object.c +79 -0
  11. data/ext/ruby_gumath/gufunc_object.h +55 -0
  12. data/ext/ruby_gumath/gumath/AUTHORS.txt +5 -0
  13. data/ext/ruby_gumath/gumath/INSTALL.txt +42 -0
  14. data/ext/ruby_gumath/gumath/LICENSE.txt +29 -0
  15. data/ext/ruby_gumath/gumath/MANIFEST.in +3 -0
  16. data/ext/ruby_gumath/gumath/Makefile.in +62 -0
  17. data/ext/ruby_gumath/gumath/README.rst +20 -0
  18. data/ext/ruby_gumath/gumath/config.guess +1530 -0
  19. data/ext/ruby_gumath/gumath/config.h.in +52 -0
  20. data/ext/ruby_gumath/gumath/config.sub +1782 -0
  21. data/ext/ruby_gumath/gumath/configure +5049 -0
  22. data/ext/ruby_gumath/gumath/configure.ac +167 -0
  23. data/ext/ruby_gumath/gumath/doc/_static/copybutton.js +66 -0
  24. data/ext/ruby_gumath/gumath/doc/conf.py +26 -0
  25. data/ext/ruby_gumath/gumath/doc/gumath/functions.rst +62 -0
  26. data/ext/ruby_gumath/gumath/doc/gumath/index.rst +26 -0
  27. data/ext/ruby_gumath/gumath/doc/index.rst +45 -0
  28. data/ext/ruby_gumath/gumath/doc/libgumath/data-structures.rst +130 -0
  29. data/ext/ruby_gumath/gumath/doc/libgumath/functions.rst +78 -0
  30. data/ext/ruby_gumath/gumath/doc/libgumath/index.rst +25 -0
  31. data/ext/ruby_gumath/gumath/doc/libgumath/kernels.rst +41 -0
  32. data/ext/ruby_gumath/gumath/doc/releases/index.rst +11 -0
  33. data/ext/ruby_gumath/gumath/install-sh +527 -0
  34. data/ext/ruby_gumath/gumath/libgumath/Makefile.in +170 -0
  35. data/ext/ruby_gumath/gumath/libgumath/Makefile.vc +160 -0
  36. data/ext/ruby_gumath/gumath/libgumath/apply.c +201 -0
  37. data/ext/ruby_gumath/gumath/libgumath/extending/bfloat16.c +130 -0
  38. data/ext/ruby_gumath/gumath/libgumath/extending/examples.c +176 -0
  39. data/ext/ruby_gumath/gumath/libgumath/extending/graph.c +393 -0
  40. data/ext/ruby_gumath/gumath/libgumath/extending/pdist.c +140 -0
  41. data/ext/ruby_gumath/gumath/libgumath/extending/quaternion.c +156 -0
  42. data/ext/ruby_gumath/gumath/libgumath/func.c +177 -0
  43. data/ext/ruby_gumath/gumath/libgumath/gumath.h +205 -0
  44. data/ext/ruby_gumath/gumath/libgumath/kernels/binary.c +547 -0
  45. data/ext/ruby_gumath/gumath/libgumath/kernels/unary.c +449 -0
  46. data/ext/ruby_gumath/gumath/libgumath/nploops.c +219 -0
  47. data/ext/ruby_gumath/gumath/libgumath/tbl.c +223 -0
  48. data/ext/ruby_gumath/gumath/libgumath/thread.c +175 -0
  49. data/ext/ruby_gumath/gumath/libgumath/xndloops.c +130 -0
  50. data/ext/ruby_gumath/gumath/python/extending.py +24 -0
  51. data/ext/ruby_gumath/gumath/python/gumath/__init__.py +74 -0
  52. data/ext/ruby_gumath/gumath/python/gumath/_gumath.c +577 -0
  53. data/ext/ruby_gumath/gumath/python/gumath/examples.c +93 -0
  54. data/ext/ruby_gumath/gumath/python/gumath/functions.c +77 -0
  55. data/ext/ruby_gumath/gumath/python/gumath/pygumath.h +95 -0
  56. data/ext/ruby_gumath/gumath/python/test_gumath.py +405 -0
  57. data/ext/ruby_gumath/gumath/setup.py +298 -0
  58. data/ext/ruby_gumath/gumath/vcbuild/INSTALL.txt +36 -0
  59. data/ext/ruby_gumath/gumath/vcbuild/vcbuild32.bat +21 -0
  60. data/ext/ruby_gumath/gumath/vcbuild/vcbuild64.bat +21 -0
  61. data/ext/ruby_gumath/gumath/vcbuild/vcclean.bat +10 -0
  62. data/ext/ruby_gumath/gumath/vcbuild/vcdistclean.bat +11 -0
  63. data/ext/ruby_gumath/include/gumath.h +205 -0
  64. data/ext/ruby_gumath/include/ruby_gumath.h +41 -0
  65. data/ext/ruby_gumath/lib/libgumath.a +0 -0
  66. data/ext/ruby_gumath/lib/libgumath.so +1 -0
  67. data/ext/ruby_gumath/lib/libgumath.so.0 +1 -0
  68. data/ext/ruby_gumath/lib/libgumath.so.0.2.0dev3 +0 -0
  69. data/ext/ruby_gumath/ruby_gumath.c +295 -0
  70. data/ext/ruby_gumath/ruby_gumath.h +41 -0
  71. data/ext/ruby_gumath/ruby_gumath_internal.h +45 -0
  72. data/ext/ruby_gumath/util.c +68 -0
  73. data/ext/ruby_gumath/util.h +48 -0
  74. data/gumath.gemspec +47 -0
  75. data/lib/gumath.rb +7 -0
  76. data/lib/gumath/version.rb +5 -0
  77. data/lib/ruby_gumath.so +0 -0
  78. metadata +206 -0
@@ -0,0 +1,41 @@
1
+ /* BSD 3-Clause License
2
+ *
3
+ * Copyright (c) 2018, Quansight and Sameer Deshmukh
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * * Neither the name of the copyright holder nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ #ifndef RUBY_GUMATH_H
33
+ #define RUBY_GUMATH_H
34
+
35
+ /* Classes */
36
+ VALUE cGumath;
37
+
38
+ int rb_gumath_add_functions(VALUE module, const gm_tbl_t *tbl);
39
+ #define GUMATH_FUNCTION_HASH rb_intern("@gumath_functions")
40
+
41
+ #endif /* RUBY_GUMATH_H */
Binary file
@@ -0,0 +1 @@
1
+ ext/ruby_gumath/lib/libgumath.so.0.2.0dev3
@@ -0,0 +1 @@
1
+ ext/ruby_gumath/lib/libgumath.so.0.2.0dev3
@@ -0,0 +1,295 @@
1
+ /* BSD 3-Clause License
2
+ *
3
+ * Copyright (c) 2018, Quansight and Sameer Deshmukh
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * * Neither the name of the copyright holder nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ #include "ruby_gumath_internal.h"
32
+
33
+ /* libxnd.so is not linked without at least one xnd symbol. */
34
+ const void *dummy = NULL;
35
+
36
+ /****************************************************************************/
37
+ /* Class globals */
38
+ /****************************************************************************/
39
+
40
+ /* Function table */
41
+ static gm_tbl_t *table = NULL;
42
+
43
+ /* Maximum number of threads */
44
+ static int64_t max_threads = 1;
45
+ static int initialized = 0;
46
+ extern VALUE cGumath;
47
+
48
+ /****************************************************************************/
49
+ /* Error handling */
50
+ /****************************************************************************/
51
+
52
+ VALUE
53
+ seterr(ndt_context_t *ctx)
54
+ {
55
+ return rb_ndtypes_set_error(ctx);
56
+ }
57
+
58
+ /****************************************************************************/
59
+ /* Instance methods */
60
+ /****************************************************************************/
61
+
62
+ static VALUE
63
+ Gumath_GufuncObject_call(int argc, VALUE *argv, VALUE self)
64
+ {
65
+ NDT_STATIC_CONTEXT(ctx);
66
+ xnd_t stack[NDT_MAX_ARGS];
67
+ const ndt_t *in_types[NDT_MAX_ARGS];
68
+ gm_kernel_t kernel;
69
+ ndt_apply_spec_t spec = ndt_apply_spec_empty;
70
+ GufuncObject *self_p;
71
+ VALUE result[NDT_MAX_ARGS];
72
+ int i, k;
73
+ size_t nin = argc;
74
+
75
+ if (argc > NDT_MAX_ARGS) {
76
+ rb_raise(rb_eArgError, "too many arguments.");
77
+ }
78
+
79
+ /* Prepare arguments for sending into gumath function. */
80
+ for (i = 0; i < argc; i++) {
81
+ if (!rb_xnd_check_type(argv[i])) {
82
+ VALUE str = rb_funcall(argv[i], rb_intern("inspect"), 0, NULL);
83
+ rb_raise(rb_eArgError, "Args must be XND. Received %s.", RSTRING_PTR(str));
84
+ }
85
+
86
+ stack[i] = *rb_xnd_const_xnd(argv[i]);
87
+ in_types[i] = stack[i].type;
88
+ }
89
+
90
+ /* Select the gumath function to be called from the function table. */
91
+ GET_GUOBJ(self, self_p);
92
+
93
+ kernel = gm_select(&spec, self_p->table, self_p->name, in_types, argc, stack, &ctx);
94
+ if (kernel.set == NULL) {
95
+ seterr(&ctx);
96
+ raise_error();
97
+ }
98
+
99
+ if (spec.nbroadcast > 0) {
100
+ for (i = 0; i < argc; i++) {
101
+ stack[i].type = spec.broadcast[i];
102
+ }
103
+ }
104
+
105
+ /* Populate output values with empty XND objects. */
106
+ for (i = 0; i < spec.nout; i++) {
107
+ if (ndt_is_concrete(spec.out[i])) {
108
+ VALUE x = rb_xnd_empty_from_type(spec.out[i]);
109
+ if (x == NULL) {
110
+ ndt_apply_spec_clear(&spec);
111
+ rb_raise(rb_eNoMemError, "could not allocate empty XND object.");
112
+ }
113
+ result[i] = x;
114
+ stack[nin+i] = *rb_xnd_const_xnd(x);
115
+ }
116
+ else {
117
+ result[i] = NULL;
118
+ stack[nin+i] = xnd_error;
119
+ }
120
+ }
121
+
122
+ /* Actually call the kernel function with prepared input and output args. */
123
+ #ifdef HAVE_PTHREAD_H
124
+ if (gm_apply_thread(&kernel, stack, spec.outer_dims, spec.flags,
125
+ max_threads, &ctx) < 0) {
126
+ seterr(&ctx);
127
+ raise_error();
128
+ }
129
+ #else
130
+ if (gm_apply(&kernel, stack, spec.outer_dims, &ctx) < 0) {
131
+ seterr(&ctx);
132
+ raise_error();
133
+ }
134
+ #endif
135
+
136
+ /* Prepare output XND objects. */
137
+ for (i = 0; i < spec.nout; i++) {
138
+ if (ndt_is_abstract(spec.out[i])) {
139
+ ndt_del(spec.out[i]);
140
+ VALUE x = rb_xnd_from_xnd(&stack[nin+i]);
141
+ stack[nin+i] = xnd_error;
142
+ if (x == NULL) {
143
+ for (k = i+i; k < spec.nout; k++) {
144
+ if (ndt_is_abstract(spec.out[k])) {
145
+ xnd_del_buffer(&stack[nin+k], XND_OWN_ALL);
146
+ }
147
+ }
148
+ }
149
+ result[i] = x;
150
+ }
151
+ }
152
+
153
+ if (spec.nbroadcast > 0) {
154
+ for (i = 0; i < nin; ++i) {
155
+ ndt_del(spec.broadcast[i]);
156
+ }
157
+ }
158
+
159
+ /* Return result */
160
+ switch(spec.nout) {
161
+ case 0: return Qnil;
162
+ case 1: return result[0];
163
+ default: {
164
+ VALUE tuple = array_new(spec.nout);
165
+ for (i = 0; i < spec.nout; ++i) {
166
+ rb_ary_store(tuple, i, result[i]);
167
+ }
168
+ return tuple;
169
+ }
170
+ }
171
+ }
172
+
173
+ /****************************************************************************/
174
+ /* Singleton methods */
175
+ /****************************************************************************/
176
+
177
+ static VALUE
178
+ Gumath_s_unsafe_add_kernel(int argc, VALUE *argv, VALUE klass)
179
+ {
180
+ /* TODO: implement this. */
181
+ }
182
+
183
+ static VALUE
184
+ Gumath_s_get_max_threads(VALUE klass)
185
+ {
186
+ return INT2NUM(max_threads);
187
+ }
188
+
189
+ static VALUE
190
+ Gumath_s_set_max_threads(VALUE klass, VALUE threads)
191
+ {
192
+ Check_Type(threads, T_FIXNUM);
193
+
194
+ max_threads = NUM2INT(threads);
195
+ }
196
+
197
+ /****************************************************************************/
198
+ /* Other functions */
199
+ /****************************************************************************/
200
+
201
+ static void
202
+ init_max_threads(void)
203
+ {
204
+ VALUE rb_max_threads = rb_funcall(rb_const_get(rb_cObject, rb_intern("Etc")),
205
+ rb_intern("nprocessors"), 0, NULL);
206
+ max_threads = NUM2INT(rb_max_threads);
207
+ }
208
+
209
+ /****************************************************************************/
210
+ /* C-API */
211
+ /****************************************************************************/
212
+
213
+ struct map_args {
214
+ VALUE module;
215
+ const gm_tbl_t *table;
216
+ };
217
+
218
+ /* Function called by libgumath that will load function kernels from function
219
+ table of type gm_tbl_t into a Ruby module. Don't call this directly use
220
+ rb_gumath_add_functions.
221
+ */
222
+ int
223
+ add_function(const gm_func_t *f, void *args)
224
+ {
225
+ struct map_args *a = (struct map_args *)args;
226
+ VALUE func, func_hash;
227
+
228
+ func = GufuncObject_alloc(a->table, f->name);
229
+ if (func == NULL) {
230
+ return -1;
231
+ }
232
+
233
+ func_hash = rb_ivar_get(a->module, GUMATH_FUNCTION_HASH);
234
+ rb_hash_aset(func_hash, ID2SYM(rb_intern(f->name)), func);
235
+
236
+ return 0;
237
+ }
238
+
239
+ /* C API call for adding functions from a gumath kernel table to */
240
+ int
241
+ rb_gumath_add_functions(VALUE module, const gm_tbl_t *tbl)
242
+ {
243
+ struct map_args args = {module, tbl};
244
+
245
+ if (gm_tbl_map(tbl, add_function, &args) < 0) {
246
+ return -1;
247
+ }
248
+ }
249
+
250
+ void Init_ruby_gumath(void)
251
+ {
252
+ NDT_STATIC_CONTEXT(ctx);
253
+
254
+ if (!initialized) {
255
+ dummy = &xnd_error;
256
+
257
+ gm_init();
258
+
259
+ if (!xnd_exists()) {
260
+ rb_raise(rb_eLoadError, "Need XND for gumath.");
261
+ }
262
+
263
+ if (!ndt_exists()) {
264
+ rb_raise(rb_eLoadError, "Need NDT for gumath.");
265
+ }
266
+
267
+ table = gm_tbl_new(&ctx);
268
+ if (table == NULL) {
269
+ seterr(&ctx);
270
+ raise_error();
271
+ }
272
+
273
+ init_max_threads();
274
+
275
+ initialized = 1;
276
+ }
277
+
278
+ cGumath = rb_define_class("Gumath", rb_cObject);
279
+ cGumath_GufuncObject = rb_define_class_under(cGumath, "GufuncObject", rb_cObject);
280
+
281
+ /* Class: Gumath */
282
+
283
+ /* Singleton methods */
284
+ rb_define_singleton_method(cGumath, "unsafe_add_kernel", Gumath_s_unsafe_add_kernel, -1);
285
+ rb_define_singleton_method(cGumath, "get_max_threads", Gumath_s_get_max_threads, 0);
286
+ rb_define_singleton_method(cGumath, "set_max_threads", Gumath_s_set_max_threads, 1);
287
+
288
+ /* Class: Gumath::GufuncObject */
289
+
290
+ /* Instance methods */
291
+ rb_define_method(cGumath_GufuncObject, "call", Gumath_GufuncObject_call,-1);
292
+
293
+ Init_gumath_functions();
294
+ Init_gumath_examples();
295
+ }
@@ -0,0 +1,41 @@
1
+ /* BSD 3-Clause License
2
+ *
3
+ * Copyright (c) 2018, Quansight and Sameer Deshmukh
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * * Neither the name of the copyright holder nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ #ifndef RUBY_GUMATH_H
33
+ #define RUBY_GUMATH_H
34
+
35
+ /* Classes */
36
+ VALUE cGumath;
37
+
38
+ int rb_gumath_add_functions(VALUE module, const gm_tbl_t *tbl);
39
+ #define GUMATH_FUNCTION_HASH rb_intern("@gumath_functions")
40
+
41
+ #endif /* RUBY_GUMATH_H */
@@ -0,0 +1,45 @@
1
+ /* BSD 3-Clause License
2
+ *
3
+ * Copyright (c) 2018, Quansight and Sameer Deshmukh
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * * Neither the name of the copyright holder nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ #ifndef RUBY_GUMATH_INTERNAL_H
33
+ #define RUBY_GUMATH_INTERNAL_H
34
+
35
+ #include <ruby.h>
36
+ #include "ndtypes.h"
37
+ #include "ruby_ndtypes.h"
38
+ #include "xnd.h"
39
+ #include "ruby_xnd.h"
40
+ #include "gumath.h"
41
+ #include "gufunc_object.h"
42
+ #include "ruby_gumath.h"
43
+ #include "util.h"
44
+
45
+ #endif /* RUBY_GUMATH_INTERNAL_H */
@@ -0,0 +1,68 @@
1
+ /* BSD 3-Clause License
2
+ *
3
+ * Copyright (c) 2018, Quansight and Sameer Deshmukh
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ *
12
+ * * Redistributions in binary form must reproduce the above copyright notice,
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ *
16
+ * * Neither the name of the copyright holder nor the names of its
17
+ * contributors may be used to endorse or promote products derived from
18
+ * this software without specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ /*
33
+ Utility functions for gumath.
34
+ */
35
+ #include "ruby_gumath_internal.h"
36
+
37
+ VALUE
38
+ array_new(int64_t size)
39
+ {
40
+ #if SIZE_MAX < INT64_MAX
41
+ size_t n = safe_downcast(size);
42
+ return n < 0 ? NULL : rb_ary_new2(n);
43
+ #else
44
+ return rb_ary_new2(size);
45
+ #endif
46
+ }
47
+
48
+ int
49
+ xnd_exists(void)
50
+ {
51
+ return RTEST(rb_const_get(rb_cObject, rb_intern("XND")));
52
+ }
53
+
54
+ int
55
+ ndt_exists(void)
56
+ {
57
+ return RTEST(rb_const_get(rb_cObject, rb_intern("NDT")));
58
+ }
59
+
60
+ /* Raise an error stored in $!. Clears it before raising. */
61
+ void
62
+ raise_error(void)
63
+ {
64
+ VALUE exeception = rb_errinfo();
65
+
66
+ rb_set_errinfo(Qnil);
67
+ rb_exc_raise(exeception);
68
+ }