numo-narray 0.9.0.1-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +47 -0
  4. data/Rakefile +41 -0
  5. data/ext/numo/narray/SFMT-params.h +97 -0
  6. data/ext/numo/narray/SFMT-params19937.h +46 -0
  7. data/ext/numo/narray/SFMT.c +620 -0
  8. data/ext/numo/narray/SFMT.h +157 -0
  9. data/ext/numo/narray/array.c +525 -0
  10. data/ext/numo/narray/data.c +901 -0
  11. data/ext/numo/narray/depend.erb +33 -0
  12. data/ext/numo/narray/extconf.rb +117 -0
  13. data/ext/numo/narray/gen/bit.erb.c +811 -0
  14. data/ext/numo/narray/gen/cogen.rb +18 -0
  15. data/ext/numo/narray/gen/def/dcomplex.rb +32 -0
  16. data/ext/numo/narray/gen/def/dfloat.rb +30 -0
  17. data/ext/numo/narray/gen/def/int16.rb +29 -0
  18. data/ext/numo/narray/gen/def/int32.rb +29 -0
  19. data/ext/numo/narray/gen/def/int64.rb +29 -0
  20. data/ext/numo/narray/gen/def/int8.rb +29 -0
  21. data/ext/numo/narray/gen/def/robject.rb +30 -0
  22. data/ext/numo/narray/gen/def/scomplex.rb +32 -0
  23. data/ext/numo/narray/gen/def/sfloat.rb +30 -0
  24. data/ext/numo/narray/gen/def/uint16.rb +29 -0
  25. data/ext/numo/narray/gen/def/uint32.rb +29 -0
  26. data/ext/numo/narray/gen/def/uint64.rb +29 -0
  27. data/ext/numo/narray/gen/def/uint8.rb +29 -0
  28. data/ext/numo/narray/gen/dtype.erb.c +328 -0
  29. data/ext/numo/narray/gen/tmpl/accum.c +36 -0
  30. data/ext/numo/narray/gen/tmpl/accum_binary.c +75 -0
  31. data/ext/numo/narray/gen/tmpl/accum_index.c +58 -0
  32. data/ext/numo/narray/gen/tmpl/allocate.c +35 -0
  33. data/ext/numo/narray/gen/tmpl/aref.c +51 -0
  34. data/ext/numo/narray/gen/tmpl/aset.c +61 -0
  35. data/ext/numo/narray/gen/tmpl/binary.c +53 -0
  36. data/ext/numo/narray/gen/tmpl/binary2.c +55 -0
  37. data/ext/numo/narray/gen/tmpl/binary_s.c +34 -0
  38. data/ext/numo/narray/gen/tmpl/bit_binary.c +94 -0
  39. data/ext/numo/narray/gen/tmpl/bit_count.c +82 -0
  40. data/ext/numo/narray/gen/tmpl/bit_unary.c +77 -0
  41. data/ext/numo/narray/gen/tmpl/cast.c +37 -0
  42. data/ext/numo/narray/gen/tmpl/cast_array.c +79 -0
  43. data/ext/numo/narray/gen/tmpl/cast_numeric.c +22 -0
  44. data/ext/numo/narray/gen/tmpl/coerce_cast.c +8 -0
  45. data/ext/numo/narray/gen/tmpl/cond_binary.c +51 -0
  46. data/ext/numo/narray/gen/tmpl/cond_unary.c +45 -0
  47. data/ext/numo/narray/gen/tmpl/cum.c +42 -0
  48. data/ext/numo/narray/gen/tmpl/each.c +43 -0
  49. data/ext/numo/narray/gen/tmpl/each_with_index.c +64 -0
  50. data/ext/numo/narray/gen/tmpl/extract.c +23 -0
  51. data/ext/numo/narray/gen/tmpl/eye.c +91 -0
  52. data/ext/numo/narray/gen/tmpl/fill.c +38 -0
  53. data/ext/numo/narray/gen/tmpl/format.c +60 -0
  54. data/ext/numo/narray/gen/tmpl/format_to_a.c +47 -0
  55. data/ext/numo/narray/gen/tmpl/head.c +25 -0
  56. data/ext/numo/narray/gen/tmpl/inspect.c +16 -0
  57. data/ext/numo/narray/gen/tmpl/map_with_index.c +94 -0
  58. data/ext/numo/narray/gen/tmpl/median.c +44 -0
  59. data/ext/numo/narray/gen/tmpl/minmax.c +47 -0
  60. data/ext/numo/narray/gen/tmpl/poly.c +49 -0
  61. data/ext/numo/narray/gen/tmpl/pow.c +74 -0
  62. data/ext/numo/narray/gen/tmpl/powint.c +17 -0
  63. data/ext/numo/narray/gen/tmpl/qsort.c +149 -0
  64. data/ext/numo/narray/gen/tmpl/rand.c +33 -0
  65. data/ext/numo/narray/gen/tmpl/rand_norm.c +46 -0
  66. data/ext/numo/narray/gen/tmpl/robj_allocate.c +32 -0
  67. data/ext/numo/narray/gen/tmpl/seq.c +61 -0
  68. data/ext/numo/narray/gen/tmpl/set2.c +56 -0
  69. data/ext/numo/narray/gen/tmpl/sort.c +36 -0
  70. data/ext/numo/narray/gen/tmpl/sort_index.c +86 -0
  71. data/ext/numo/narray/gen/tmpl/store.c +31 -0
  72. data/ext/numo/narray/gen/tmpl/store_array.c +5 -0
  73. data/ext/numo/narray/gen/tmpl/store_from.c +53 -0
  74. data/ext/numo/narray/gen/tmpl/store_numeric.c +22 -0
  75. data/ext/numo/narray/gen/tmpl/to_a.c +41 -0
  76. data/ext/numo/narray/gen/tmpl/unary.c +58 -0
  77. data/ext/numo/narray/gen/tmpl/unary2.c +58 -0
  78. data/ext/numo/narray/gen/tmpl/unary_s.c +57 -0
  79. data/ext/numo/narray/index.c +822 -0
  80. data/ext/numo/narray/kwarg.c +79 -0
  81. data/ext/numo/narray/math.c +140 -0
  82. data/ext/numo/narray/narray.c +1539 -0
  83. data/ext/numo/narray/ndloop.c +1928 -0
  84. data/ext/numo/narray/numo/compat.h +23 -0
  85. data/ext/numo/narray/numo/intern.h +112 -0
  86. data/ext/numo/narray/numo/narray.h +411 -0
  87. data/ext/numo/narray/numo/ndloop.h +99 -0
  88. data/ext/numo/narray/numo/template.h +140 -0
  89. data/ext/numo/narray/numo/types/bit.h +19 -0
  90. data/ext/numo/narray/numo/types/complex.h +410 -0
  91. data/ext/numo/narray/numo/types/complex_macro.h +205 -0
  92. data/ext/numo/narray/numo/types/dcomplex.h +11 -0
  93. data/ext/numo/narray/numo/types/dfloat.h +12 -0
  94. data/ext/numo/narray/numo/types/float_def.h +34 -0
  95. data/ext/numo/narray/numo/types/float_macro.h +277 -0
  96. data/ext/numo/narray/numo/types/int16.h +12 -0
  97. data/ext/numo/narray/numo/types/int32.h +12 -0
  98. data/ext/numo/narray/numo/types/int64.h +12 -0
  99. data/ext/numo/narray/numo/types/int8.h +12 -0
  100. data/ext/numo/narray/numo/types/int_macro.h +34 -0
  101. data/ext/numo/narray/numo/types/robj_macro.h +218 -0
  102. data/ext/numo/narray/numo/types/robject.h +21 -0
  103. data/ext/numo/narray/numo/types/scomplex.h +11 -0
  104. data/ext/numo/narray/numo/types/sfloat.h +13 -0
  105. data/ext/numo/narray/numo/types/uint16.h +12 -0
  106. data/ext/numo/narray/numo/types/uint32.h +12 -0
  107. data/ext/numo/narray/numo/types/uint64.h +12 -0
  108. data/ext/numo/narray/numo/types/uint8.h +12 -0
  109. data/ext/numo/narray/numo/types/uint_macro.h +31 -0
  110. data/ext/numo/narray/numo/types/xint_macro.h +133 -0
  111. data/ext/numo/narray/rand.c +87 -0
  112. data/ext/numo/narray/step.c +506 -0
  113. data/ext/numo/narray/struct.c +872 -0
  114. data/lib/2.1/numo/narray.so +0 -0
  115. data/lib/2.2/numo/narray.so +0 -0
  116. data/lib/2.3/numo/narray.so +0 -0
  117. data/lib/erbpp.rb +286 -0
  118. data/lib/erbpp/line_number.rb +126 -0
  119. data/lib/erbpp/narray_def.rb +338 -0
  120. data/lib/numo/narray.rb +6 -0
  121. data/numo-narray.gemspec +35 -0
  122. data/spec/bit_spec.rb +93 -0
  123. data/spec/narray_spec.rb +249 -0
  124. metadata +238 -0
@@ -0,0 +1,25 @@
1
+ /*
2
+ <%=type_name%>.c
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 1999-2011,2013 by Masahiro TANAKA
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+
12
+ #include <ruby.h>
13
+ #include <math.h>
14
+ #include "narray.h"
15
+ #include "SFMT.h"
16
+ #include "template.h"
17
+ #include "<%=type_name%>.h"
18
+
19
+ //#define cT <%=type_var%>
20
+ VALUE cT;
21
+ //#define cRT <%=real_type_var%>
22
+
23
+ #ifdef mTM
24
+ VALUE mTM;
25
+ #endif
@@ -0,0 +1,16 @@
1
+ static VALUE
2
+ <%=c_iter%>(char *ptr, size_t pos, VALUE fmt)
3
+ {
4
+ return format_<%=tp%>(fmt, (dtype*)(ptr+pos));
5
+ }
6
+
7
+ /*
8
+ Returns a string containing a human-readable representation of NArray.
9
+ @overload inspect
10
+ @return [String]
11
+ */
12
+ VALUE
13
+ <%=c_func%>(VALUE ary)
14
+ {
15
+ return na_ndloop_inspect(ary, <%=c_iter%>, Qnil);
16
+ }
@@ -0,0 +1,94 @@
1
+ static inline dtype
2
+ yield_map_with_index(dtype x, size_t *c, VALUE *a, int nd, int md)
3
+ {
4
+ int j;
5
+ VALUE y;
6
+
7
+ a[0] = m_data_to_num(x);
8
+ for (j=0; j<=nd; j++) {
9
+ a[j+1] = SIZE2NUM(c[j]);
10
+ }
11
+ y = rb_yield(rb_ary_new4(md,a));
12
+ return m_num_to_data(y);
13
+ }
14
+
15
+ void
16
+ <%=c_iter%>(na_loop_t *const lp)
17
+ {
18
+ size_t i;
19
+ char *p1, *p2;
20
+ ssize_t s1, s2;
21
+ size_t *idx1, *idx2;
22
+ dtype x;
23
+ VALUE *a;
24
+ size_t *c;
25
+ int nd, md;
26
+
27
+ c = (size_t*)(lp->opt_ptr);
28
+ nd = lp->ndim - 1;
29
+ md = lp->ndim + 1;
30
+ a = ALLOCA_N(VALUE,md);
31
+
32
+ INIT_COUNTER(lp, i);
33
+ INIT_PTR_IDX(lp, 0, p1, s1, idx1);
34
+ INIT_PTR_IDX(lp, 1, p2, s2, idx2);
35
+
36
+ c[nd] = 0;
37
+ if (idx1) {
38
+ if (idx2) {
39
+ for (; i--;) {
40
+ GET_DATA_INDEX(p1,idx1,dtype,x);
41
+ x = yield_map_with_index(x,c,a,nd,md);
42
+ SET_DATA_INDEX(p2,idx2,dtype,x);
43
+ c[nd]++;
44
+ }
45
+ } else {
46
+ for (; i--;) {
47
+ GET_DATA_INDEX(p1,idx1,dtype,x);
48
+ x = yield_map_with_index(x,c,a,nd,md);
49
+ SET_DATA_STRIDE(p2,s2,dtype,x);
50
+ c[nd]++;
51
+ }
52
+ }
53
+ } else {
54
+ if (idx2) {
55
+ for (; i--;) {
56
+ GET_DATA_STRIDE(p1,s1,dtype,x);
57
+ x = yield_map_with_index(x,c,a,nd,md);
58
+ SET_DATA_INDEX(p2,idx2,dtype,x);
59
+ c[nd]++;
60
+ }
61
+ } else {
62
+ for (; i--;) {
63
+ GET_DATA_STRIDE(p1,s1,dtype,x);
64
+ x = yield_map_with_index(x,c,a,nd,md);
65
+ SET_DATA_STRIDE(p2,s2,dtype,x);
66
+ c[nd]++;
67
+ }
68
+ }
69
+ }
70
+ }
71
+
72
+ /*
73
+ Invokes the given block once for each element of self,
74
+ passing that element and indices along each axis as parameters.
75
+ Creates a new NArray containing the values returned by the block.
76
+ Inplace option is allowed, i.e., `nary.inplace.map` overwrites `nary`.
77
+
78
+ @overload <%=method%>
79
+
80
+ For a block {|x,i,j,...| ... }
81
+ @yield [x,i,j,...] x is an element, i,j,... are multidimensional indices.
82
+
83
+ @return [Numo::NArray] mapped array
84
+
85
+ */
86
+ static VALUE
87
+ <%=c_func%>(VALUE self)
88
+ {
89
+ ndfunc_arg_in_t ain[1] = {{Qnil,0}};
90
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
91
+ ndfunc_t ndf = {<%=c_iter%>, FULL_LOOP, 1,1, ain,aout};
92
+
93
+ return na_ndloop_with_index(&ndf, 1, self);
94
+ }
@@ -0,0 +1,44 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t n;
5
+ char *p1, *p2;
6
+ dtype *buf;
7
+
8
+ INIT_COUNTER(lp, n);
9
+ p1 = (lp->args[0]).ptr + (lp->args[0].iter[0]).pos;
10
+ p2 = (lp->args[1]).ptr + (lp->args[1].iter[0]).pos;
11
+ buf = (dtype*)p1;
12
+
13
+ <%=tp%>_qsort(buf, n, sizeof(dtype));
14
+
15
+ <% if is_float %>
16
+ for (; n; n--) {
17
+ if (!isnan(buf[n-1])) break;
18
+ }
19
+ <% end %>
20
+
21
+ if (n==0) {
22
+ *(dtype*)p2 = buf[0];
23
+ }
24
+ else if (n%2==0) {
25
+ *(dtype*)p2 = (buf[n/2-1]+buf[n/2])/2;
26
+ }
27
+ else {
28
+ *(dtype*)p2 = buf[(n-1)/2];
29
+ }
30
+ }
31
+
32
+ static VALUE
33
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
34
+ {
35
+ VALUE reduce;
36
+ ndfunc_arg_in_t ain[2] = {{cT,0},{sym_reduce,0}};
37
+ ndfunc_arg_out_t aout[1] = {{INT2FIX(0),0}};
38
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP_NIP|NDF_FLAT_REDUCE, 2,1, ain,aout};
39
+
40
+ self = na_copy(self);
41
+ reduce = na_reduce_dimension(argc, argv, 1, &self); // v[0] = self
42
+
43
+ return na_ndloop(&ndf, 2, self, reduce);
44
+ }
@@ -0,0 +1,47 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1;
6
+ ssize_t s1;
7
+ dtype x,xmin,xmax;
8
+
9
+ INIT_COUNTER(lp, i);
10
+ INIT_PTR(lp, 0, p1, s1);
11
+
12
+ xmin = xmax = *(dtype*)p1;
13
+ p1 += s1;
14
+ i--;
15
+ for (; i--;) {
16
+ x = *(dtype*)p1;
17
+ if (!m_isnan(x)) {
18
+ if (m_isnan(xmax) || m_gt(x,xmax)) {
19
+ xmax = x;
20
+ }
21
+ if (m_isnan(xmin) || m_lt(x,xmin)) {
22
+ xmin = x;
23
+ }
24
+ }
25
+ p1 += s1;
26
+ }
27
+ *(dtype*)(lp->args[1].ptr + lp->args[1].iter[0].pos) = xmin;
28
+ *(dtype*)(lp->args[2].ptr + lp->args[2].iter[0].pos) = xmax;
29
+ }
30
+
31
+ /*
32
+ <%=method.capitalize%> of self.
33
+ @overload <%=method%>(*args)
34
+ @param [Array of Numeric,Range] args Affected dimensions.
35
+ @return [Numo::<%=class_name%>,Numo::<%=class_name%>] min and max of self.
36
+ */
37
+ static VALUE
38
+ <%=c_func%>(int argc, VALUE *argv, VALUE self)
39
+ {
40
+ VALUE reduce;
41
+ ndfunc_arg_in_t ain[2] = {{cT,0},{sym_reduce,0}};
42
+ ndfunc_arg_out_t aout[2] = {{cT,0},{cT,0}};
43
+ ndfunc_t ndf = {<%=c_iter%>, STRIDE_LOOP_NIP|NDF_FLAT_REDUCE|NDF_EXTRACT, 2,2, ain,aout};
44
+
45
+ reduce = na_reduce_dimension(argc, argv, 1, &self);
46
+ return na_ndloop(&ndf, 2, self, reduce);
47
+ }
@@ -0,0 +1,49 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ dtype x, y, a;
6
+
7
+ x = *(dtype*)(lp->args[0].ptr + lp->args[0].iter[0].pos);
8
+ i = lp->narg - 2;
9
+ y = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
10
+ for (; --i;) {
11
+ y = m_mul(x,y);
12
+ a = *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos);
13
+ y = m_add(y,a);
14
+ }
15
+ i = lp->narg - 1;
16
+ *(dtype*)(lp->args[i].ptr + lp->args[i].iter[0].pos) = y;
17
+ }
18
+
19
+ /*
20
+ Polynomial.: a0 + a1*x + a2*x**2 + a3*x**3 + ... + an*x**n
21
+ @overload <%=method%> a0, a1, ...
22
+ @param [Numo::NArray,Numeric] a0
23
+ @param [Numo::NArray,Numeric] a1 , ...
24
+ @return [Numo::<%=class_name%>]
25
+ */
26
+ static VALUE
27
+ <%=c_func%>(VALUE self, VALUE args)
28
+ {
29
+ int argc, i;
30
+ VALUE *argv;
31
+ volatile VALUE v, a;
32
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
33
+ ndfunc_t ndf = { <%=c_iter%>, NO_LOOP, 0, 1, 0, aout };
34
+
35
+ argc = RARRAY_LEN(args);
36
+ ndf.nin = argc+1;
37
+ ndf.ain = ALLOCA_N(ndfunc_arg_in_t,argc+1);
38
+ for (i=0; i<argc+1; i++) {
39
+ ndf.ain[i].type = cT;
40
+ }
41
+ argv = ALLOCA_N(VALUE,argc+1);
42
+ argv[0] = self;
43
+ for (i=0; i<argc; i++) {
44
+ argv[i+1] = RARRAY_PTR(args)[i];
45
+ }
46
+ a = rb_ary_new4(argc+1, argv);
47
+ v = na_ndloop2(&ndf, a);
48
+ return numo_<%=tp%>_extract(v);
49
+ }
@@ -0,0 +1,74 @@
1
+ static void
2
+ <%=c_iter%>(na_loop_t *const lp)
3
+ {
4
+ size_t i;
5
+ char *p1, *p2, *p3;
6
+ ssize_t s1, s2, s3;
7
+ dtype x, y;
8
+ INIT_COUNTER(lp, i);
9
+ INIT_PTR(lp, 0, p1, s1);
10
+ INIT_PTR(lp, 1, p2, s2);
11
+ INIT_PTR(lp, 2, p3, s3);
12
+ for (; i--;) {
13
+ GET_DATA_STRIDE(p1,s1,dtype,x);
14
+ GET_DATA_STRIDE(p2,s2,dtype,y);
15
+ x = m_pow(x,y);
16
+ SET_DATA_STRIDE(p3,s3,dtype,x);
17
+ }
18
+ }
19
+
20
+ static void
21
+ <%=c_iter%>_int32(na_loop_t *const lp)
22
+ {
23
+ size_t i;
24
+ char *p1, *p2, *p3;
25
+ ssize_t s1, s2, s3;
26
+ dtype x;
27
+ int32_t y;
28
+ INIT_COUNTER(lp, i);
29
+ INIT_PTR(lp, 0, p1, s1);
30
+ INIT_PTR(lp, 1, p2, s2);
31
+ INIT_PTR(lp, 2, p3, s3);
32
+ for (; i--;) {
33
+ GET_DATA_STRIDE(p1,s1,dtype,x);
34
+ GET_DATA_STRIDE(p2,s2,int32_t,y);
35
+ x = m_pow_int(x,y);
36
+ SET_DATA_STRIDE(p3,s3,dtype,x);
37
+ }
38
+ }
39
+
40
+ static VALUE
41
+ <%=c_func%>_self(VALUE self, VALUE other)
42
+ {
43
+ ndfunc_arg_in_t ain[2] = {{cT,0},{cT,0}};
44
+ ndfunc_arg_in_t ain_i[2] = {{cT,0},{numo_cInt32,0}};
45
+ ndfunc_arg_out_t aout[1] = {{cT,0}};
46
+ ndfunc_t ndf = { <%=c_iter%>, STRIDE_LOOP, 2, 1, ain, aout };
47
+ ndfunc_t ndf_i = { <%=c_iter%>_int32, STRIDE_LOOP, 2, 1, ain_i, aout };
48
+
49
+ // fixme : use na.integer?
50
+ if (FIXNUM_P(other) || rb_obj_is_kind_of(other,numo_cInt32)) {
51
+ return na_ndloop(&ndf_i, 2, self, other);
52
+ } else {
53
+ return na_ndloop(&ndf, 2, self, other);
54
+ }
55
+ }
56
+
57
+ /*
58
+ Binary power.
59
+ @overload <%=op_map%> other
60
+ @param [Numo::NArray,Numeric] other
61
+ @return [Numo::NArray] self to the other-th power.
62
+ */
63
+ static VALUE
64
+ <%=c_func%>(VALUE self, VALUE other)
65
+ {
66
+ VALUE klass, v;
67
+ klass = na_upcast(CLASS_OF(self),CLASS_OF(other));
68
+ if (klass==cT) {
69
+ return <%=c_func%>_self(self,other);
70
+ } else {
71
+ v = rb_funcall(klass, id_cast, 1, self);
72
+ return rb_funcall(v, id_pow, 1, other);
73
+ }
74
+ }
@@ -0,0 +1,17 @@
1
+ static dtype pow_<%=tp%>(dtype x, int p)
2
+ {
3
+ dtype r = m_one;
4
+ switch(p) {
5
+ case 2: return m_square(x);
6
+ case 3: return m_mul(m_square(x),x);
7
+ case 1: return x;
8
+ case 0: return m_one;
9
+ }
10
+ if (p<0) return m_zero;
11
+ while (p) {
12
+ if ((p%2) == 1) r = m_mul(r,x);
13
+ x = m_square(x);
14
+ p /= 2;
15
+ }
16
+ return r;
17
+ }
@@ -0,0 +1,149 @@
1
+ /*
2
+ qsort.c
3
+ Numerical Array Extension for Ruby
4
+ (C) Copyright 2007-2011 by Masahiro TANAKA
5
+
6
+ This program is free software.
7
+ You can distribute/modify this program
8
+ under the same terms as Ruby itself.
9
+ NO WARRANTY.
10
+ */
11
+
12
+ /*
13
+ * qsort.c: standard quicksort algorithm
14
+ *
15
+ * Modifications from vanilla NetBSD source:
16
+ * Add do ... while() macro fix
17
+ * Remove __inline, _DIAGASSERTs, __P
18
+ * Remove ill-considered "swap_cnt" switch to insertion sort,
19
+ * in favor of a simple check for presorted input.
20
+ *
21
+ * CAUTION: if you change this file, see also qsort_arg.c
22
+ *
23
+ * $PostgreSQL: pgsql/src/port/qsort.c,v 1.12 2006/10/19 20:56:22 tgl Exp $
24
+ */
25
+
26
+ /* $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $ */
27
+
28
+ /*-
29
+ * Copyright (c) 1992, 1993
30
+ * The Regents of the University of California. All rights reserved.
31
+ *
32
+ * Redistribution and use in source and binary forms, with or without
33
+ * modification, are permitted provided that the following conditions
34
+ * are met:
35
+ * 1. Redistributions of source code must retain the above copyright
36
+ * notice, this list of conditions and the following disclaimer.
37
+ * 2. Redistributions in binary form must reproduce the above copyright
38
+ * notice, this list of conditions and the following disclaimer in the
39
+ * documentation and/or other materials provided with the distribution.
40
+ * 3. Neither the name of the University nor the names of its contributors
41
+ * may be used to endorse or promote products derived from this software
42
+ * without specific prior written permission.
43
+ *
44
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54
+ * SUCH DAMAGE.
55
+ */
56
+
57
+ #ifndef QSORT_INCL
58
+ #define QSORT_INCL
59
+ #define Min(x, y) ((x) < (y) ? (x) : (y))
60
+
61
+ #define swap(type,a,b) \
62
+ do {type tmp=*(type*)(a); *(type*)(a)=*(type*)(b); *(type*)(b)=tmp;} while(0)
63
+
64
+ #define vecswap(type, a, b, n) if ((n)>0) swap(type,(a),(b))
65
+
66
+ #define MED3(a,b,c) \
67
+ (cmpgt(b,a) ? \
68
+ (cmpgt(c,b) ? b : (cmpgt(c,a) ? c : a)) \
69
+ : (cmpgt(b,c) ? b : (cmpgt(c,a) ? a : c)))
70
+ #endif
71
+
72
+ #undef qsort_dtype
73
+ #define qsort_dtype <%=dtype%>
74
+ #undef qsort_cast
75
+ #define qsort_cast <%=dcast%>
76
+
77
+ void
78
+ <%=tp%>_qsort(void *a, size_t n, ssize_t es)
79
+ {
80
+ char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
81
+ int d, r, presorted;
82
+
83
+ loop:
84
+ if (n < 7) {
85
+ for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
86
+ for (pl = pm; pl > (char *) a && cmpgt(pl - es, pl);
87
+ pl -= es)
88
+ swap(qsort_dtype, pl, pl - es);
89
+ return;
90
+ }
91
+ presorted = 1;
92
+ for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) {
93
+ if (cmpgt(pm - es, pm)) {
94
+ presorted = 0;
95
+ break;
96
+ }
97
+ }
98
+ if (presorted)
99
+ return;
100
+ pm = (char *) a + (n / 2) * es;
101
+ if (n > 7) {
102
+ pl = (char *) a;
103
+ pn = (char *) a + (n - 1) * es;
104
+ if (n > 40) {
105
+ d = (n / 8) * es;
106
+ pl = MED3(pl, pl + d, pl + 2 * d);
107
+ pm = MED3(pm - d, pm, pm + d);
108
+ pn = MED3(pn - 2 * d, pn - d, pn);
109
+ }
110
+ pm = MED3(pl, pm, pn);
111
+ }
112
+ swap(qsort_dtype, a, pm);
113
+ pa = pb = (char *) a + es;
114
+ pc = pd = (char *) a + (n - 1) * es;
115
+ for (;;) {
116
+ while (pb <= pc && (r = cmp(pb, a)) <= 0) {
117
+ if (r == 0) {
118
+ swap(qsort_dtype, pa, pb);
119
+ pa += es;
120
+ }
121
+ pb += es;
122
+ }
123
+ while (pb <= pc && (r = cmp(pc, a)) >= 0) {
124
+ if (r == 0) {
125
+ swap(qsort_dtype, pc, pd);
126
+ pd -= es;
127
+ }
128
+ pc -= es;
129
+ }
130
+ if (pb > pc)
131
+ break;
132
+ swap(qsort_dtype, pb, pc);
133
+ pb += es;
134
+ pc -= es;
135
+ }
136
+ pn = (char *) a + n * es;
137
+ r = Min(pa - (char *) a, pb - pa);
138
+ vecswap(qsort_dtype, a, pb - r, r);
139
+ r = Min(pd - pc, pn - pd - es);
140
+ vecswap(qsort_dtype, pb, pn - r, r);
141
+ if ((r = pb - pa) > es)
142
+ <%=tp%>_qsort(a, r / es, es);
143
+ if ((r = pd - pc) > es) {
144
+ a = pn - r;
145
+ n = r / es;
146
+ goto loop;
147
+ }
148
+ }
149
+