cumo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +27 -0
  3. data/.travis.yml +5 -0
  4. data/3rd_party/mkmf-cu/.gitignore +36 -0
  5. data/3rd_party/mkmf-cu/Gemfile +3 -0
  6. data/3rd_party/mkmf-cu/LICENSE +21 -0
  7. data/3rd_party/mkmf-cu/README.md +36 -0
  8. data/3rd_party/mkmf-cu/Rakefile +11 -0
  9. data/3rd_party/mkmf-cu/bin/mkmf-cu-nvcc +4 -0
  10. data/3rd_party/mkmf-cu/lib/mkmf-cu.rb +32 -0
  11. data/3rd_party/mkmf-cu/lib/mkmf-cu/cli.rb +80 -0
  12. data/3rd_party/mkmf-cu/lib/mkmf-cu/nvcc.rb +157 -0
  13. data/3rd_party/mkmf-cu/mkmf-cu.gemspec +16 -0
  14. data/3rd_party/mkmf-cu/test/test_mkmf-cu.rb +67 -0
  15. data/CODE_OF_CONDUCT.md +46 -0
  16. data/Gemfile +8 -0
  17. data/LICENSE.txt +82 -0
  18. data/README.md +252 -0
  19. data/Rakefile +43 -0
  20. data/bench/broadcast_fp32.rb +138 -0
  21. data/bench/cumo_bench.rb +193 -0
  22. data/bench/numo_bench.rb +138 -0
  23. data/bench/reduction_fp32.rb +117 -0
  24. data/bin/console +14 -0
  25. data/bin/setup +8 -0
  26. data/cumo.gemspec +32 -0
  27. data/ext/cumo/cuda/cublas.c +278 -0
  28. data/ext/cumo/cuda/driver.c +421 -0
  29. data/ext/cumo/cuda/memory_pool.cpp +185 -0
  30. data/ext/cumo/cuda/memory_pool_impl.cpp +308 -0
  31. data/ext/cumo/cuda/memory_pool_impl.hpp +370 -0
  32. data/ext/cumo/cuda/memory_pool_impl_test.cpp +554 -0
  33. data/ext/cumo/cuda/nvrtc.c +207 -0
  34. data/ext/cumo/cuda/runtime.c +167 -0
  35. data/ext/cumo/cumo.c +148 -0
  36. data/ext/cumo/depend.erb +58 -0
  37. data/ext/cumo/extconf.rb +179 -0
  38. data/ext/cumo/include/cumo.h +25 -0
  39. data/ext/cumo/include/cumo/compat.h +23 -0
  40. data/ext/cumo/include/cumo/cuda/cublas.h +153 -0
  41. data/ext/cumo/include/cumo/cuda/cumo_thrust.hpp +187 -0
  42. data/ext/cumo/include/cumo/cuda/cumo_thrust_complex.hpp +79 -0
  43. data/ext/cumo/include/cumo/cuda/driver.h +22 -0
  44. data/ext/cumo/include/cumo/cuda/memory_pool.h +28 -0
  45. data/ext/cumo/include/cumo/cuda/nvrtc.h +22 -0
  46. data/ext/cumo/include/cumo/cuda/runtime.h +40 -0
  47. data/ext/cumo/include/cumo/indexer.h +238 -0
  48. data/ext/cumo/include/cumo/intern.h +142 -0
  49. data/ext/cumo/include/cumo/intern_fwd.h +38 -0
  50. data/ext/cumo/include/cumo/intern_kernel.h +6 -0
  51. data/ext/cumo/include/cumo/narray.h +429 -0
  52. data/ext/cumo/include/cumo/narray_kernel.h +149 -0
  53. data/ext/cumo/include/cumo/ndloop.h +95 -0
  54. data/ext/cumo/include/cumo/reduce_kernel.h +126 -0
  55. data/ext/cumo/include/cumo/template.h +158 -0
  56. data/ext/cumo/include/cumo/template_kernel.h +77 -0
  57. data/ext/cumo/include/cumo/types/bit.h +40 -0
  58. data/ext/cumo/include/cumo/types/bit_kernel.h +34 -0
  59. data/ext/cumo/include/cumo/types/complex.h +402 -0
  60. data/ext/cumo/include/cumo/types/complex_kernel.h +414 -0
  61. data/ext/cumo/include/cumo/types/complex_macro.h +382 -0
  62. data/ext/cumo/include/cumo/types/complex_macro_kernel.h +186 -0
  63. data/ext/cumo/include/cumo/types/dcomplex.h +46 -0
  64. data/ext/cumo/include/cumo/types/dcomplex_kernel.h +13 -0
  65. data/ext/cumo/include/cumo/types/dfloat.h +47 -0
  66. data/ext/cumo/include/cumo/types/dfloat_kernel.h +14 -0
  67. data/ext/cumo/include/cumo/types/float_def.h +34 -0
  68. data/ext/cumo/include/cumo/types/float_def_kernel.h +39 -0
  69. data/ext/cumo/include/cumo/types/float_macro.h +191 -0
  70. data/ext/cumo/include/cumo/types/float_macro_kernel.h +158 -0
  71. data/ext/cumo/include/cumo/types/int16.h +24 -0
  72. data/ext/cumo/include/cumo/types/int16_kernel.h +23 -0
  73. data/ext/cumo/include/cumo/types/int32.h +24 -0
  74. data/ext/cumo/include/cumo/types/int32_kernel.h +19 -0
  75. data/ext/cumo/include/cumo/types/int64.h +24 -0
  76. data/ext/cumo/include/cumo/types/int64_kernel.h +19 -0
  77. data/ext/cumo/include/cumo/types/int8.h +24 -0
  78. data/ext/cumo/include/cumo/types/int8_kernel.h +19 -0
  79. data/ext/cumo/include/cumo/types/int_macro.h +67 -0
  80. data/ext/cumo/include/cumo/types/int_macro_kernel.h +48 -0
  81. data/ext/cumo/include/cumo/types/real_accum.h +486 -0
  82. data/ext/cumo/include/cumo/types/real_accum_kernel.h +101 -0
  83. data/ext/cumo/include/cumo/types/robj_macro.h +80 -0
  84. data/ext/cumo/include/cumo/types/robj_macro_kernel.h +0 -0
  85. data/ext/cumo/include/cumo/types/robject.h +27 -0
  86. data/ext/cumo/include/cumo/types/robject_kernel.h +7 -0
  87. data/ext/cumo/include/cumo/types/scomplex.h +46 -0
  88. data/ext/cumo/include/cumo/types/scomplex_kernel.h +13 -0
  89. data/ext/cumo/include/cumo/types/sfloat.h +48 -0
  90. data/ext/cumo/include/cumo/types/sfloat_kernel.h +14 -0
  91. data/ext/cumo/include/cumo/types/uint16.h +25 -0
  92. data/ext/cumo/include/cumo/types/uint16_kernel.h +20 -0
  93. data/ext/cumo/include/cumo/types/uint32.h +25 -0
  94. data/ext/cumo/include/cumo/types/uint32_kernel.h +20 -0
  95. data/ext/cumo/include/cumo/types/uint64.h +25 -0
  96. data/ext/cumo/include/cumo/types/uint64_kernel.h +20 -0
  97. data/ext/cumo/include/cumo/types/uint8.h +25 -0
  98. data/ext/cumo/include/cumo/types/uint8_kernel.h +20 -0
  99. data/ext/cumo/include/cumo/types/uint_macro.h +58 -0
  100. data/ext/cumo/include/cumo/types/uint_macro_kernel.h +38 -0
  101. data/ext/cumo/include/cumo/types/xint_macro.h +169 -0
  102. data/ext/cumo/include/cumo/types/xint_macro_kernel.h +88 -0
  103. data/ext/cumo/narray/SFMT-params.h +97 -0
  104. data/ext/cumo/narray/SFMT-params19937.h +46 -0
  105. data/ext/cumo/narray/SFMT.c +620 -0
  106. data/ext/cumo/narray/SFMT.h +167 -0
  107. data/ext/cumo/narray/array.c +638 -0
  108. data/ext/cumo/narray/data.c +961 -0
  109. data/ext/cumo/narray/gen/cogen.rb +56 -0
  110. data/ext/cumo/narray/gen/cogen_kernel.rb +58 -0
  111. data/ext/cumo/narray/gen/def/bit.rb +37 -0
  112. data/ext/cumo/narray/gen/def/dcomplex.rb +39 -0
  113. data/ext/cumo/narray/gen/def/dfloat.rb +37 -0
  114. data/ext/cumo/narray/gen/def/int16.rb +36 -0
  115. data/ext/cumo/narray/gen/def/int32.rb +36 -0
  116. data/ext/cumo/narray/gen/def/int64.rb +36 -0
  117. data/ext/cumo/narray/gen/def/int8.rb +36 -0
  118. data/ext/cumo/narray/gen/def/robject.rb +37 -0
  119. data/ext/cumo/narray/gen/def/scomplex.rb +39 -0
  120. data/ext/cumo/narray/gen/def/sfloat.rb +37 -0
  121. data/ext/cumo/narray/gen/def/uint16.rb +36 -0
  122. data/ext/cumo/narray/gen/def/uint32.rb +36 -0
  123. data/ext/cumo/narray/gen/def/uint64.rb +36 -0
  124. data/ext/cumo/narray/gen/def/uint8.rb +36 -0
  125. data/ext/cumo/narray/gen/erbpp2.rb +346 -0
  126. data/ext/cumo/narray/gen/narray_def.rb +268 -0
  127. data/ext/cumo/narray/gen/spec.rb +425 -0
  128. data/ext/cumo/narray/gen/tmpl/accum.c +86 -0
  129. data/ext/cumo/narray/gen/tmpl/accum_binary.c +121 -0
  130. data/ext/cumo/narray/gen/tmpl/accum_binary_kernel.cu +61 -0
  131. data/ext/cumo/narray/gen/tmpl/accum_index.c +119 -0
  132. data/ext/cumo/narray/gen/tmpl/accum_index_kernel.cu +66 -0
  133. data/ext/cumo/narray/gen/tmpl/accum_kernel.cu +12 -0
  134. data/ext/cumo/narray/gen/tmpl/alloc_func.c +107 -0
  135. data/ext/cumo/narray/gen/tmpl/allocate.c +37 -0
  136. data/ext/cumo/narray/gen/tmpl/aref.c +66 -0
  137. data/ext/cumo/narray/gen/tmpl/aref_cpu.c +50 -0
  138. data/ext/cumo/narray/gen/tmpl/aset.c +56 -0
  139. data/ext/cumo/narray/gen/tmpl/binary.c +162 -0
  140. data/ext/cumo/narray/gen/tmpl/binary2.c +70 -0
  141. data/ext/cumo/narray/gen/tmpl/binary2_kernel.cu +15 -0
  142. data/ext/cumo/narray/gen/tmpl/binary_kernel.cu +31 -0
  143. data/ext/cumo/narray/gen/tmpl/binary_s.c +45 -0
  144. data/ext/cumo/narray/gen/tmpl/binary_s_kernel.cu +15 -0
  145. data/ext/cumo/narray/gen/tmpl/bincount.c +181 -0
  146. data/ext/cumo/narray/gen/tmpl/cast.c +44 -0
  147. data/ext/cumo/narray/gen/tmpl/cast_array.c +13 -0
  148. data/ext/cumo/narray/gen/tmpl/class.c +9 -0
  149. data/ext/cumo/narray/gen/tmpl/class_kernel.cu +6 -0
  150. data/ext/cumo/narray/gen/tmpl/clip.c +121 -0
  151. data/ext/cumo/narray/gen/tmpl/coerce_cast.c +10 -0
  152. data/ext/cumo/narray/gen/tmpl/complex_accum_kernel.cu +129 -0
  153. data/ext/cumo/narray/gen/tmpl/cond_binary.c +68 -0
  154. data/ext/cumo/narray/gen/tmpl/cond_binary_kernel.cu +18 -0
  155. data/ext/cumo/narray/gen/tmpl/cond_unary.c +46 -0
  156. data/ext/cumo/narray/gen/tmpl/cum.c +50 -0
  157. data/ext/cumo/narray/gen/tmpl/each.c +47 -0
  158. data/ext/cumo/narray/gen/tmpl/each_with_index.c +70 -0
  159. data/ext/cumo/narray/gen/tmpl/ewcomp.c +79 -0
  160. data/ext/cumo/narray/gen/tmpl/ewcomp_kernel.cu +19 -0
  161. data/ext/cumo/narray/gen/tmpl/extract.c +22 -0
  162. data/ext/cumo/narray/gen/tmpl/extract_cpu.c +26 -0
  163. data/ext/cumo/narray/gen/tmpl/extract_data.c +53 -0
  164. data/ext/cumo/narray/gen/tmpl/eye.c +105 -0
  165. data/ext/cumo/narray/gen/tmpl/eye_kernel.cu +19 -0
  166. data/ext/cumo/narray/gen/tmpl/fill.c +52 -0
  167. data/ext/cumo/narray/gen/tmpl/fill_kernel.cu +29 -0
  168. data/ext/cumo/narray/gen/tmpl/float_accum_kernel.cu +106 -0
  169. data/ext/cumo/narray/gen/tmpl/format.c +62 -0
  170. data/ext/cumo/narray/gen/tmpl/format_to_a.c +49 -0
  171. data/ext/cumo/narray/gen/tmpl/frexp.c +38 -0
  172. data/ext/cumo/narray/gen/tmpl/gemm.c +203 -0
  173. data/ext/cumo/narray/gen/tmpl/init_class.c +20 -0
  174. data/ext/cumo/narray/gen/tmpl/init_module.c +12 -0
  175. data/ext/cumo/narray/gen/tmpl/inspect.c +21 -0
  176. data/ext/cumo/narray/gen/tmpl/lib.c +50 -0
  177. data/ext/cumo/narray/gen/tmpl/lib_kernel.cu +24 -0
  178. data/ext/cumo/narray/gen/tmpl/logseq.c +102 -0
  179. data/ext/cumo/narray/gen/tmpl/logseq_kernel.cu +31 -0
  180. data/ext/cumo/narray/gen/tmpl/map_with_index.c +98 -0
  181. data/ext/cumo/narray/gen/tmpl/median.c +66 -0
  182. data/ext/cumo/narray/gen/tmpl/minmax.c +47 -0
  183. data/ext/cumo/narray/gen/tmpl/module.c +9 -0
  184. data/ext/cumo/narray/gen/tmpl/module_kernel.cu +1 -0
  185. data/ext/cumo/narray/gen/tmpl/new_dim0.c +15 -0
  186. data/ext/cumo/narray/gen/tmpl/new_dim0_kernel.cu +8 -0
  187. data/ext/cumo/narray/gen/tmpl/poly.c +50 -0
  188. data/ext/cumo/narray/gen/tmpl/pow.c +97 -0
  189. data/ext/cumo/narray/gen/tmpl/pow_kernel.cu +29 -0
  190. data/ext/cumo/narray/gen/tmpl/powint.c +17 -0
  191. data/ext/cumo/narray/gen/tmpl/qsort.c +212 -0
  192. data/ext/cumo/narray/gen/tmpl/rand.c +168 -0
  193. data/ext/cumo/narray/gen/tmpl/rand_norm.c +121 -0
  194. data/ext/cumo/narray/gen/tmpl/real_accum_kernel.cu +75 -0
  195. data/ext/cumo/narray/gen/tmpl/seq.c +112 -0
  196. data/ext/cumo/narray/gen/tmpl/seq_kernel.cu +43 -0
  197. data/ext/cumo/narray/gen/tmpl/set2.c +57 -0
  198. data/ext/cumo/narray/gen/tmpl/sort.c +48 -0
  199. data/ext/cumo/narray/gen/tmpl/sort_index.c +111 -0
  200. data/ext/cumo/narray/gen/tmpl/store.c +41 -0
  201. data/ext/cumo/narray/gen/tmpl/store_array.c +187 -0
  202. data/ext/cumo/narray/gen/tmpl/store_array_kernel.cu +58 -0
  203. data/ext/cumo/narray/gen/tmpl/store_bit.c +86 -0
  204. data/ext/cumo/narray/gen/tmpl/store_bit_kernel.cu +66 -0
  205. data/ext/cumo/narray/gen/tmpl/store_from.c +81 -0
  206. data/ext/cumo/narray/gen/tmpl/store_from_kernel.cu +58 -0
  207. data/ext/cumo/narray/gen/tmpl/store_kernel.cu +3 -0
  208. data/ext/cumo/narray/gen/tmpl/store_numeric.c +9 -0
  209. data/ext/cumo/narray/gen/tmpl/to_a.c +43 -0
  210. data/ext/cumo/narray/gen/tmpl/unary.c +132 -0
  211. data/ext/cumo/narray/gen/tmpl/unary2.c +60 -0
  212. data/ext/cumo/narray/gen/tmpl/unary_kernel.cu +72 -0
  213. data/ext/cumo/narray/gen/tmpl/unary_ret2.c +34 -0
  214. data/ext/cumo/narray/gen/tmpl/unary_s.c +86 -0
  215. data/ext/cumo/narray/gen/tmpl/unary_s_kernel.cu +58 -0
  216. data/ext/cumo/narray/gen/tmpl_bit/allocate.c +24 -0
  217. data/ext/cumo/narray/gen/tmpl_bit/aref.c +54 -0
  218. data/ext/cumo/narray/gen/tmpl_bit/aref_cpu.c +57 -0
  219. data/ext/cumo/narray/gen/tmpl_bit/aset.c +56 -0
  220. data/ext/cumo/narray/gen/tmpl_bit/binary.c +98 -0
  221. data/ext/cumo/narray/gen/tmpl_bit/bit_count.c +64 -0
  222. data/ext/cumo/narray/gen/tmpl_bit/bit_count_cpu.c +88 -0
  223. data/ext/cumo/narray/gen/tmpl_bit/bit_count_kernel.cu +76 -0
  224. data/ext/cumo/narray/gen/tmpl_bit/bit_reduce.c +133 -0
  225. data/ext/cumo/narray/gen/tmpl_bit/each.c +48 -0
  226. data/ext/cumo/narray/gen/tmpl_bit/each_with_index.c +70 -0
  227. data/ext/cumo/narray/gen/tmpl_bit/extract.c +30 -0
  228. data/ext/cumo/narray/gen/tmpl_bit/extract_cpu.c +29 -0
  229. data/ext/cumo/narray/gen/tmpl_bit/fill.c +69 -0
  230. data/ext/cumo/narray/gen/tmpl_bit/format.c +64 -0
  231. data/ext/cumo/narray/gen/tmpl_bit/format_to_a.c +51 -0
  232. data/ext/cumo/narray/gen/tmpl_bit/inspect.c +21 -0
  233. data/ext/cumo/narray/gen/tmpl_bit/mask.c +136 -0
  234. data/ext/cumo/narray/gen/tmpl_bit/none_p.c +14 -0
  235. data/ext/cumo/narray/gen/tmpl_bit/store_array.c +108 -0
  236. data/ext/cumo/narray/gen/tmpl_bit/store_bit.c +70 -0
  237. data/ext/cumo/narray/gen/tmpl_bit/store_from.c +60 -0
  238. data/ext/cumo/narray/gen/tmpl_bit/to_a.c +47 -0
  239. data/ext/cumo/narray/gen/tmpl_bit/unary.c +81 -0
  240. data/ext/cumo/narray/gen/tmpl_bit/where.c +90 -0
  241. data/ext/cumo/narray/gen/tmpl_bit/where2.c +95 -0
  242. data/ext/cumo/narray/index.c +880 -0
  243. data/ext/cumo/narray/kwargs.c +153 -0
  244. data/ext/cumo/narray/math.c +142 -0
  245. data/ext/cumo/narray/narray.c +1948 -0
  246. data/ext/cumo/narray/ndloop.c +2105 -0
  247. data/ext/cumo/narray/rand.c +45 -0
  248. data/ext/cumo/narray/step.c +474 -0
  249. data/ext/cumo/narray/struct.c +886 -0
  250. data/lib/cumo.rb +3 -0
  251. data/lib/cumo/cuda.rb +11 -0
  252. data/lib/cumo/cuda/compile_error.rb +36 -0
  253. data/lib/cumo/cuda/compiler.rb +161 -0
  254. data/lib/cumo/cuda/device.rb +47 -0
  255. data/lib/cumo/cuda/link_state.rb +31 -0
  256. data/lib/cumo/cuda/module.rb +40 -0
  257. data/lib/cumo/cuda/nvrtc_program.rb +27 -0
  258. data/lib/cumo/linalg.rb +12 -0
  259. data/lib/cumo/narray.rb +2 -0
  260. data/lib/cumo/narray/extra.rb +1278 -0
  261. data/lib/erbpp.rb +294 -0
  262. data/lib/erbpp/line_number.rb +137 -0
  263. data/lib/erbpp/narray_def.rb +381 -0
  264. data/numo-narray-version +1 -0
  265. data/run.gdb +7 -0
  266. metadata +353 -0
@@ -0,0 +1,45 @@
1
+ #include "ruby.h"
2
+ #include "cumo/narray.h"
3
+ #include "SFMT.h"
4
+
5
+ #ifdef HAVE_UNISTD_H
6
+ #include <unistd.h>
7
+ #endif
8
+ #include <time.h>
9
+ #ifdef HAVE_SYS_TIME_H
10
+ #include <sys/time.h>
11
+ #endif
12
+
13
+ static u_int64_t
14
+ random_seed()
15
+ {
16
+ static int n = 0;
17
+ struct timeval tv;
18
+
19
+ gettimeofday(&tv, 0);
20
+ return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
21
+ }
22
+
23
+ static VALUE
24
+ nary_s_srand(int argc, VALUE *argv, VALUE obj)
25
+ {
26
+ VALUE vseed;
27
+ u_int64_t seed;
28
+
29
+ //rb_secure(4);
30
+ if (rb_scan_args(argc, argv, "01", &vseed) == 0) {
31
+ seed = random_seed();
32
+ }
33
+ else {
34
+ seed = NUM2UINT64(vseed);
35
+ }
36
+ init_gen_rand(seed);
37
+
38
+ return Qnil;
39
+ }
40
+
41
+ void
42
+ Init_cumo_nary_rand() {
43
+ rb_define_singleton_method(cNArray, "srand", nary_s_srand, -1);
44
+ init_gen_rand(0);
45
+ }
@@ -0,0 +1,474 @@
1
+ /*
2
+ step.c
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 2007,2013 by Masahiro TANAKA
5
+ */
6
+ #include <ruby.h>
7
+ #include <math.h>
8
+
9
+ #include "cumo/narray.h"
10
+
11
+ #if defined(__FreeBSD__) && __FreeBSD__ < 4
12
+ #include <floatingpoint.h>
13
+ #endif
14
+
15
+ #ifdef HAVE_FLOAT_H
16
+ #include <float.h>
17
+ #endif
18
+
19
+ #ifdef HAVE_IEEEFP_H
20
+ #include <ieeefp.h>
21
+ #endif
22
+
23
+ #ifndef DBL_EPSILON
24
+ #define DBL_EPSILON 2.2204460492503131e-16
25
+ #endif
26
+
27
+ static ID id_beg, id_end, id_len, id_step, id_excl;
28
+
29
+ //#define EXCL(r) RTEST(rb_ivar_get((r), id_excl))
30
+ #define EXCL(r) RTEST(rb_funcall((r), rb_intern("exclude_end?"), 0))
31
+
32
+ #define SET_EXCL(r,v) rb_ivar_set((r), id_excl, (v) ? Qtrue : Qfalse)
33
+
34
+ static void
35
+ step_init(
36
+ VALUE self,
37
+ VALUE beg,
38
+ VALUE end,
39
+ VALUE step,
40
+ VALUE len,
41
+ VALUE excl
42
+ )
43
+ {
44
+ if (RTEST(len)) {
45
+ if (!(FIXNUM_P(len) || TYPE(len)==T_BIGNUM)) {
46
+ rb_raise(rb_eArgError, "length must be Integer");
47
+ }
48
+ if (RTEST(rb_funcall(len,rb_intern("<"),1,INT2FIX(0)))) {
49
+ rb_raise(rb_eRangeError,"length must be non negative");
50
+ }
51
+ }
52
+ rb_ivar_set(self, id_beg, beg);
53
+ rb_ivar_set(self, id_end, end);
54
+ rb_ivar_set(self, id_len, len);
55
+ rb_ivar_set(self, id_step, step);
56
+ SET_EXCL(self, excl);
57
+ }
58
+
59
+ static VALUE
60
+ nary_step_new2(
61
+ VALUE range,
62
+ VALUE step,
63
+ VALUE len
64
+ )
65
+ {
66
+ VALUE beg, end, excl;
67
+ VALUE self = rb_obj_alloc(na_cStep);
68
+
69
+ //beg = rb_ivar_get(range, id_beg);
70
+ beg = rb_funcall(range, id_beg, 0);
71
+ //end = rb_ivar_get(range, id_end);
72
+ end = rb_funcall(range, id_end, 0);
73
+ excl = rb_funcall(range, rb_intern("exclude_end?"), 0);
74
+
75
+ step_init(self, beg, end, step, len, excl);
76
+ return self;
77
+ }
78
+
79
+
80
+ /*
81
+ * call-seq:
82
+ * Step.new(start, end, step=nil, length=nil) => step
83
+ * Step.new(range, step=nil, length=nil) => step
84
+ *
85
+ * Constructs a step using three parameters among <i>start</i>,
86
+ * <i>end</i>, <i>step</i> and <i>length</i>. <i>start</i>,
87
+ * <i>end</i> parameters can be replaced with <i>range</i>. If the
88
+ * <i>step</i> is omitted (or supplied with nil), then calculated
89
+ * from <i>length</i> or definded as 1.
90
+ */
91
+
92
+ static VALUE
93
+ step_initialize( int argc, VALUE *argv, VALUE self )
94
+ {
95
+ VALUE a, b=Qnil, c=Qnil, d=Qnil, e=Qnil;
96
+
97
+ rb_scan_args(argc, argv, "13", &a, &b, &c, &d);
98
+ /* Selfs are immutable, so that they should be initialized only once. */
99
+ if (rb_ivar_defined(self, id_beg)) {
100
+ rb_name_error(rb_intern("initialize"), "`initialize' called twice");
101
+ }
102
+ if (rb_obj_is_kind_of(a,rb_cRange)) {
103
+ if (argc>3) {
104
+ rb_raise(rb_eArgError, "extra argument");
105
+ }
106
+ d = c;
107
+ c = b;
108
+ e = rb_funcall(a, rb_intern("exclude_end?"), 0);
109
+ //b = rb_ivar_get(a, id_end);
110
+ b = rb_funcall(a, id_end, 0);
111
+ //a = rb_ivar_get(a, id_beg);
112
+ a = rb_funcall(a, id_beg, 0);
113
+ }
114
+ step_init(self, a, b, c, d, e);
115
+ return Qnil;
116
+ }
117
+
118
+ /*
119
+ * call-seq:
120
+ * step.begin => obj
121
+ * step.first => obj
122
+ *
123
+ * Returns the start of <i>step</i>.
124
+ */
125
+
126
+ static VALUE
127
+ step_first( VALUE self )
128
+ {
129
+ return rb_ivar_get(self, id_beg);
130
+ }
131
+
132
+ /*
133
+ * call-seq:
134
+ * step.end => obj
135
+ * step.last => obj
136
+ *
137
+ * Returns the object that defines the end of <i>step</i>.
138
+ */
139
+
140
+ static VALUE
141
+ step_last( VALUE self )
142
+ {
143
+ return rb_ivar_get(self, id_end);
144
+ }
145
+
146
+ /*
147
+ * call-seq:
148
+ * step.length => obj
149
+ * step.size => obj
150
+ *
151
+ * Returns the length of <i>step</i>.
152
+ */
153
+
154
+ static VALUE
155
+ step_length( VALUE self )
156
+ {
157
+ return rb_ivar_get(self, id_len);
158
+ }
159
+
160
+ /*
161
+ * call-seq:
162
+ * step.step => obj
163
+ *
164
+ * Returns the step of <i>step</i>.
165
+ */
166
+
167
+ static VALUE
168
+ step_step( VALUE self )
169
+ {
170
+ return rb_ivar_get(self, id_step);
171
+ }
172
+
173
+ /*
174
+ * call-seq:
175
+ * step.exclude_end? => true or false
176
+ *
177
+ * Returns <code>true</code> if <i>step</i> excludes its end value.
178
+ */
179
+ static VALUE
180
+ step_exclude_end_p(VALUE self)
181
+ {
182
+ return RTEST(rb_ivar_get(self, id_excl)) ? Qtrue : Qfalse;
183
+ }
184
+
185
+
186
+ /*
187
+ * call-seq:
188
+ * step.parameters([array_size]) => [start,step,length]
189
+ *
190
+ * Returns the iteration parameters of <i>step</i>. If
191
+ * <i>array_sizse</i> is given, negative array index is considered.
192
+ */
193
+
194
+ void
195
+ nary_step_array_index(VALUE self, size_t ary_size,
196
+ size_t *plen, ssize_t *pbeg, ssize_t *pstep)
197
+ {
198
+ size_t len;
199
+ ssize_t beg=0, step=1;
200
+ VALUE vbeg, vend, vstep, vlen;
201
+ ssize_t end=ary_size;
202
+
203
+ //vbeg = rb_ivar_get(self, id_beg);
204
+ //vend = rb_ivar_get(self, id_end);
205
+ vlen = rb_ivar_get(self, id_len);
206
+ vstep = rb_ivar_get(self, id_step);
207
+ vbeg = rb_funcall(self, id_beg, 0);
208
+ vend = rb_funcall(self, id_end, 0);
209
+ //vlen = rb_funcall(self, id_len, 0);
210
+ //vstep = rb_funcall(self, id_step, 0);
211
+
212
+ if (RTEST(vbeg)) {
213
+ beg = NUM2SSIZET(vbeg);
214
+ if (beg<0) {
215
+ beg += ary_size;
216
+ }
217
+ }
218
+ if (RTEST(vend)) {
219
+ end = NUM2SSIZET(vend);
220
+ if (end<0) {
221
+ end += ary_size;
222
+ }
223
+ }
224
+
225
+ //puts("pass 1");
226
+
227
+ if (RTEST(vlen)) {
228
+ len = NUM2SIZET(vlen);
229
+ if (len>0) {
230
+ if (RTEST(vstep)) {
231
+ step = NUM2SSIZET(step);
232
+ if (RTEST(vbeg)) {
233
+ if (RTEST(vend)) {
234
+ rb_raise( rb_eStandardError, "verbose Step object" );
235
+ } else {
236
+ end = beg + step*(len-1);
237
+ }
238
+ } else {
239
+ if (RTEST(vend)) {
240
+ if (EXCL(self)) {
241
+ if (step>0) end--;
242
+ if (step<0) end++;
243
+ }
244
+ beg = end - step*(len-1);
245
+ } else {
246
+ beg = 0;
247
+ end = step*(len-1);
248
+ }
249
+ }
250
+ } else { // no step
251
+ step = 1;
252
+ if (RTEST(vbeg)) {
253
+ if (RTEST(vend)) {
254
+ if (EXCL(self)) {
255
+ if (beg<end) end--;
256
+ if (beg>end) end++;
257
+ }
258
+ if (len>1)
259
+ step = (end-beg)/(len-1);
260
+ } else {
261
+ end = beg + (len-1);
262
+ }
263
+ } else {
264
+ if (RTEST(vend)) {
265
+ if (EXCL(self)) {
266
+ end--;
267
+ }
268
+ beg = end - (len-1);
269
+ } else {
270
+ beg = 0;
271
+ end = len-1;
272
+ }
273
+ }
274
+ }
275
+ }
276
+ } else { // no len
277
+ if (RTEST(vstep)) {
278
+ step = NUM2SSIZET(vstep);
279
+ } else {
280
+ step = 1;
281
+ }
282
+ if (step>0) {
283
+ if (!RTEST(vbeg)) {
284
+ beg = 0;
285
+ }
286
+ if (!RTEST(vend)) {
287
+ end = ary_size-1;
288
+ }
289
+ else if (EXCL(self)) {
290
+ end--;
291
+ }
292
+ if (beg<=end) {
293
+ len = (end-beg)/step+1;
294
+ } else {
295
+ len = 0;
296
+ }
297
+ } else if (step<0) {
298
+ if (!RTEST(vbeg)) {
299
+ beg = ary_size-1;
300
+ }
301
+ if (!RTEST(vend)) {
302
+ end = 0;
303
+ }
304
+ else if (EXCL(self)) {
305
+ end++;
306
+ }
307
+ if (beg>=end) {
308
+ len = (beg-end)/(-step)+1;
309
+ } else {
310
+ len = 0;
311
+ }
312
+ } else {
313
+ rb_raise( rb_eStandardError, "step must be non-zero" );
314
+ }
315
+ }
316
+
317
+ //puts("pass 2");
318
+
319
+ if (beg<0 || beg>=(ssize_t)ary_size ||
320
+ end<0 || end>=(ssize_t)ary_size) {
321
+ rb_raise( rb_eRangeError,
322
+ "beg=%"SZF"d,end=%"SZF"d is out of array size (%"SZF"u)",
323
+ beg, end, ary_size );
324
+ }
325
+ if (plen) *plen = len;
326
+ if (pbeg) *pbeg = beg;
327
+ if (pstep) *pstep = step;
328
+ }
329
+
330
+
331
+ void
332
+ nary_step_sequence( VALUE self, size_t *plen, double *pbeg, double *pstep )
333
+ {
334
+ VALUE vbeg, vend, vstep, vlen;
335
+ double dbeg, dend, dstep=1, dsize, err;
336
+ size_t size, n;
337
+
338
+ //vbeg = rb_ivar_get(self, id_beg);
339
+ vbeg = rb_funcall(self, id_beg, 0);
340
+ dbeg = NUM2DBL(vbeg);
341
+
342
+ //vend = rb_ivar_get(self, id_end);
343
+ vend = rb_funcall(self, id_end, 0);
344
+
345
+ vlen = rb_ivar_get(self, id_len);
346
+ vstep = rb_ivar_get(self, id_step);
347
+ //vlen = rb_funcall(self, id_len ,0);
348
+ //vstep = rb_funcall(self, id_step,0);
349
+
350
+ if (RTEST(vlen)) {
351
+ size = NUM2SIZET(vlen);
352
+
353
+ if (!RTEST(vstep)) {
354
+ if (RTEST(vend)) {
355
+ dend = NUM2DBL(vend);
356
+ if (EXCL(self)) {
357
+ n = size;
358
+ } else {
359
+ n = size-1;
360
+ }
361
+ if (n>0) {
362
+ dstep = (dend-dbeg)/n;
363
+ } else {
364
+ dstep = 1;
365
+ }
366
+ } else {
367
+ dstep = 1;
368
+ }
369
+ }
370
+ } else {
371
+ if (!RTEST(vstep)) {
372
+ dstep = 1;
373
+ } else {
374
+ dstep = NUM2DBL(vstep);
375
+ }
376
+ if (RTEST(vend)) {
377
+ dend = NUM2DBL(vend);
378
+ err = (fabs(dbeg)+fabs(dend)+fabs(dend-dbeg))/fabs(dstep)*DBL_EPSILON;
379
+ if (err>0.5) err=0.5;
380
+ dsize = (dend-dbeg)/dstep;
381
+ if (EXCL(self))
382
+ dsize -= err;
383
+ else
384
+ dsize += err;
385
+ dsize = floor(dsize) + 1;
386
+ if (dsize<0) dsize=0;
387
+ if (isinf(dsize) || isnan(dsize)) {
388
+ rb_raise(rb_eArgError, "not finite size");
389
+ }
390
+ size = dsize;
391
+ } else {
392
+ rb_raise(rb_eArgError, "cannot determine length argument");
393
+ }
394
+ }
395
+
396
+ if (plen) *plen = size;
397
+ if (pbeg) *pbeg = dbeg;
398
+ if (pstep) *pstep = dstep;
399
+ }
400
+
401
+ /*
402
+ static VALUE
403
+ step_each( VALUE self )
404
+ {
405
+ VALUE a;
406
+ double beg, step;
407
+ size_t i, size;
408
+
409
+ a = nary_step_parameters( self, Qnil );
410
+ beg = NUM2DBL(RARRAY_PTR(a)[0]);
411
+ step = NUM2DBL(RARRAY_PTR(a)[1]);
412
+ size = NUM2SIZET(RARRAY_PTR(a)[2]);
413
+
414
+ for (i=0; i<size; i++) {
415
+ rb_yield(rb_float_new(beg+i*step));
416
+ }
417
+ return self;
418
+ }
419
+ */
420
+
421
+ static VALUE
422
+ range_with_step( VALUE range, VALUE step )
423
+ {
424
+ return nary_step_new2( range, step, Qnil );
425
+ }
426
+
427
+ static VALUE
428
+ range_with_length( VALUE range, VALUE len )
429
+ {
430
+ return nary_step_new2( range, Qnil, len );
431
+ }
432
+
433
+
434
+ static VALUE
435
+ nary_s_step( int argc, VALUE *argv, VALUE mod )
436
+ {
437
+ VALUE self = rb_obj_alloc(na_cStep);
438
+ step_initialize(argc, argv, self);
439
+ return self;
440
+ }
441
+
442
+
443
+ void
444
+ Init_cumo_nary_step()
445
+ {
446
+ na_cStep = rb_define_class_under(cNArray, "Step", rb_cObject);
447
+ rb_include_module(na_cStep, rb_mEnumerable);
448
+ rb_define_method(na_cStep, "initialize", step_initialize, -1);
449
+
450
+ //rb_define_method(na_cStep, "each", step_each, 0);
451
+
452
+ rb_define_method(na_cStep, "first", step_first, 0);
453
+ rb_define_method(na_cStep, "last", step_last, 0);
454
+ rb_define_method(na_cStep, "begin", step_first, 0);
455
+ rb_define_method(na_cStep, "end", step_last, 0);
456
+ rb_define_method(na_cStep, "step", step_step, 0);
457
+ rb_define_method(na_cStep, "length", step_length, 0);
458
+ rb_define_method(na_cStep, "size", step_length, 0);
459
+ rb_define_method(na_cStep, "exclude_end?", step_exclude_end_p, 0);
460
+ //rb_define_method(na_cStep, "to_s", step_to_s, 0);
461
+ //rb_define_method(na_cStep, "inspect", step_inspect, 0);
462
+ //rb_define_method(na_cStep, "parameters", nary_step_parameters, 1);
463
+
464
+ rb_define_method(rb_cRange, "%", range_with_step, 1);
465
+ rb_define_method(rb_cRange, "*", range_with_length, 1);
466
+
467
+ rb_define_singleton_method(cNArray, "step", nary_s_step, -1);
468
+
469
+ id_beg = rb_intern("begin");
470
+ id_end = rb_intern("end");
471
+ id_len = rb_intern("length");
472
+ id_step = rb_intern("step");
473
+ id_excl = rb_intern("excl");
474
+ }