convolver 3.0.1 → 4.0.0

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.
@@ -1,167 +1,57 @@
1
- // ext/convolver/correlate_raw.c
2
-
3
1
  #include "correlate_raw.h"
2
+ #include "raw_helpers.h"
4
3
 
5
- static inline size_t checked_add( size_t left, size_t right, const char *description ) {
6
- if ( right > SIZE_MAX - left ) {
7
- rb_raise( rb_eRangeError, "%s exceeds native implementation limit", description );
8
- }
9
- return left + right;
10
- }
11
-
12
- static inline size_t checked_multiply( size_t left, size_t right, const char *description ) {
13
- if ( left != 0 && right > SIZE_MAX / left ) {
14
- rb_raise( rb_eRangeError, "%s exceeds native implementation limit", description );
15
- }
16
- return left * right;
17
- }
18
-
19
- static inline size_t size_from_shape( int rank, const size_t *shape, const char *description ) {
20
- size_t size = 1;
21
- int i;
22
- for ( i = 0; i < rank; i++ ) {
23
- size = checked_multiply( size, shape[i], description );
24
- }
25
- return size;
26
- }
27
-
28
- // Sets reverse indices
29
- static inline void corner_reset( int rank, const size_t *shape, size_t *rev_indices ) {
30
- int i;
31
- for ( i = 0; i < rank; i++ ) { rev_indices[i] = shape[i] - 1; }
32
- }
33
-
34
- // Counts indices down, returns number of ranks that reset
35
- static inline int corner_dec( int rank, const size_t *shape, size_t *rev_indices ) {
36
- int i = 0;
37
- (void) rank;
38
- while ( ! rev_indices[i]-- ) {
39
- rev_indices[i] = shape[i] - 1;
40
- i++;
41
- }
42
- return i;
43
- }
44
-
45
- // Generates co-increment steps by rank boundaries crossed, for the outer position as inner position is incremented by 1
46
- static inline void calc_co_increment(
47
- int rank, const size_t *outer_shape, const size_t *inner_shape, size_t *co_increment ) {
48
- size_t factor = 1;
49
- int i;
50
- co_increment[0] = 1; // co-increment is always 1 in lowest rank
51
- for ( i = 0; i < rank; i++ ) {
52
- size_t skipped = checked_multiply( factor, outer_shape[i] - inner_shape[i], "array offset" );
53
- co_increment[i+1] = checked_add( co_increment[i], skipped, "array offset" );
54
- factor = checked_multiply( factor, outer_shape[i], "array size" );
55
- }
56
- }
57
-
58
- static inline size_t maximum_offset(
59
- int rank, const size_t *outer_shape, const size_t *inner_shape, const char *description ) {
60
- size_t factor = 1;
61
- size_t offset = 0;
62
- int i;
63
- for ( i = 0; i < rank; i++ ) {
64
- size_t dimension_offset = checked_multiply( factor, inner_shape[i] - 1, description );
65
- offset = checked_add( offset, dimension_offset, description );
66
- factor = checked_multiply( factor, outer_shape[i], "array size" );
67
- }
68
- return offset;
69
- }
70
-
71
- ////////////////////////////////////////////////////////////////////////////////////////////////////
72
- //
73
- // Correlate
74
- //
75
-
76
- void correlate_raw(
77
- int in_rank, const size_t *in_shape, const float *in_ptr,
78
- int kernel_rank, const size_t *kernel_shape, const float *kernel_ptr,
79
- int out_rank, const size_t *out_shape, float *out_ptr ) {
80
- size_t i, j, input_size, kernel_size, kernel_aligned, out_size, offset;
81
- size_t maximum_input_offset;
82
- size_t out_co_incr[LARGEST_RANK + 1], kernel_co_incr[LARGEST_RANK + 1];
83
- size_t ker_q[LARGEST_RANK], out_q[LARGEST_RANK];
84
- size_t *kernel_co_incr_cache;
85
- VALUE cache_storage = 0;
86
-
87
- kernel_size = size_from_shape( kernel_rank, kernel_shape, "kernel size" );
88
- kernel_aligned = kernel_size - kernel_size % 4;
89
- out_size = size_from_shape( out_rank, out_shape, "output size" );
90
- input_size = size_from_shape( in_rank, in_shape, "input size" );
91
-
92
- calc_co_increment( in_rank, in_shape, out_shape, out_co_incr );
93
- calc_co_increment( in_rank, in_shape, kernel_shape, kernel_co_incr );
94
- maximum_input_offset = checked_add(
95
- maximum_offset( in_rank, in_shape, out_shape, "input offset" ),
96
- maximum_offset( in_rank, in_shape, kernel_shape, "input offset" ),
97
- "input offset"
98
- );
99
- if ( maximum_input_offset >= input_size ) {
100
- rb_raise( rb_eRangeError, "input offset exceeds native implementation limit" );
101
- }
102
-
103
- kernel_co_incr_cache = RB_ALLOCV_N( size_t, cache_storage, kernel_size );
104
- kernel_co_incr_cache[0] = 0;
105
-
106
- corner_reset( kernel_rank, kernel_shape, ker_q );
107
- for ( i = 1; i < kernel_size; i++ ) {
108
- kernel_co_incr_cache[i] = checked_add(
109
- kernel_co_incr_cache[i-1],
110
- kernel_co_incr[ corner_dec( kernel_rank, kernel_shape, ker_q ) ],
111
- "kernel offset"
112
- );
113
- }
114
-
115
- offset = 0;
116
- corner_reset( out_rank, out_shape, out_q );
117
-
118
- // Main correlation loop
119
- for ( i = 0; i < out_size; i++ ) {
120
- #if CONVOLVER_USE_SSE
121
- __m128 simd_x, simd_y, simd_t;
122
- float v[4];
123
- simd_t = _mm_setzero_ps();
124
- #endif
125
- float t = 0.0;
126
-
127
- #if CONVOLVER_USE_SSE
128
- // Contiguous kernel views need not start on a 16-byte boundary.
129
- for ( j = 0; j < kernel_aligned; j +=4 ) {
130
- simd_x = _mm_loadu_ps( kernel_ptr + j );
131
- // Yes the backwards alignment is correct
132
- simd_y = _mm_set_ps( in_ptr[ offset + kernel_co_incr_cache[j+3] ], in_ptr[ offset + kernel_co_incr_cache[j+2] ],
133
- in_ptr[ offset + kernel_co_incr_cache[j+1] ], in_ptr[ offset + kernel_co_incr_cache[j] ] );
134
- simd_x = _mm_mul_ps( simd_x, simd_y );
135
- simd_t = _mm_add_ps( simd_x, simd_t );
136
- }
137
- _mm_store_ps( v, simd_t );
4
+ #define RAW_INDEX(j) (j)
5
+ #if defined(__SSE__)
6
+ #define RAW_SIMD 1
138
7
  #else
139
- // SSE is unavailable on architectures such as Apple Silicon.
140
- // Use the same calculation without x86-specific intrinsics.
141
- for ( j = 0; j < kernel_aligned; j++ ) {
142
- t += in_ptr[ offset + kernel_co_incr_cache[j] ] * kernel_ptr[j];
143
- }
8
+ #define RAW_SIMD 0
144
9
  #endif
145
-
146
- // Complete any remaining 1,2 or 3 items one at a time
147
- for ( j = kernel_aligned; j < kernel_size; j++ ) {
148
- t += in_ptr[ offset + kernel_co_incr_cache[j] ] * kernel_ptr[ j ];
149
- }
150
-
151
- #if CONVOLVER_USE_SSE
152
- out_ptr[i] = v[0] + v[1] + v[2] + v[3] + t;
10
+ #define RAW_NAME correlate_raw
11
+ #define RAW_TYPE float
12
+ #define RAW_LANES 4
13
+ #define RAW_VECTOR __m128
14
+ #define RAW_ZERO _mm_setzero_ps
15
+ #define RAW_LOAD _mm_loadu_ps
16
+ #define RAW_STORE _mm_storeu_ps
17
+ #define RAW_ADD _mm_add_ps
18
+ #define RAW_MUL _mm_mul_ps
19
+ #include "raw_loop.h"
20
+ #undef RAW_NAME
21
+ #undef RAW_TYPE
22
+ #undef RAW_LANES
23
+ #undef RAW_VECTOR
24
+ #undef RAW_ZERO
25
+ #undef RAW_LOAD
26
+ #undef RAW_STORE
27
+ #undef RAW_ADD
28
+ #undef RAW_MUL
29
+ #undef RAW_SIMD
30
+
31
+ #if defined(__SSE2__)
32
+ #define RAW_SIMD 1
153
33
  #else
154
- out_ptr[i] = t;
34
+ #define RAW_SIMD 0
155
35
  #endif
156
-
157
- if ( i + 1 < out_size ) {
158
- offset = checked_add(
159
- offset,
160
- out_co_incr[ corner_dec( out_rank, out_shape, out_q ) ],
161
- "input offset"
162
- );
163
- }
164
- }
165
-
166
- RB_ALLOCV_END( cache_storage );
167
- }
36
+ #define RAW_NAME correlate_raw_double
37
+ #define RAW_TYPE double
38
+ #define RAW_LANES 2
39
+ #define RAW_VECTOR __m128d
40
+ #define RAW_ZERO _mm_setzero_pd
41
+ #define RAW_LOAD _mm_loadu_pd
42
+ #define RAW_STORE _mm_storeu_pd
43
+ #define RAW_ADD _mm_add_pd
44
+ #define RAW_MUL _mm_mul_pd
45
+ #include "raw_loop.h"
46
+ #undef RAW_NAME
47
+ #undef RAW_TYPE
48
+ #undef RAW_LANES
49
+ #undef RAW_VECTOR
50
+ #undef RAW_ZERO
51
+ #undef RAW_LOAD
52
+ #undef RAW_STORE
53
+ #undef RAW_ADD
54
+ #undef RAW_MUL
55
+ #undef RAW_SIMD
56
+
57
+ #undef RAW_INDEX
@@ -1,28 +1,14 @@
1
- // ext/convolver/correlate_raw.h
2
-
3
- ////////////////////////////////////////////////////////////////////////////////////////////////
4
- //
5
- // Declarations of narray helper functions
6
- //
7
-
8
1
  #ifndef CORRELATE_RAW_H
9
2
  #define CORRELATE_RAW_H
10
-
11
- #ifndef CONVOLVER_RAW_CONFIG
12
- #define CONVOLVER_RAW_CONFIG
13
- #include <ruby.h>
14
- #if defined(__SSE__)
15
- #include <xmmintrin.h>
16
- #define CONVOLVER_USE_SSE 1
17
- #else
18
- #define CONVOLVER_USE_SSE 0
19
- #endif
20
- #define LARGEST_RANK 16
21
- #endif
3
+ #include "raw_config.h"
22
4
 
23
5
  void correlate_raw(
24
6
  int in_rank, const size_t *in_shape, const float *in_ptr,
25
7
  int kernel_rank, const size_t *kernel_shape, const float *kernel_ptr,
26
8
  int out_rank, const size_t *out_shape, float *out_ptr );
27
9
 
10
+ void correlate_raw_double(
11
+ int in_rank, const size_t *in_shape, const double *in_ptr,
12
+ int kernel_rank, const size_t *kernel_shape, const double *kernel_ptr,
13
+ int out_rank, const size_t *out_shape, double *out_ptr );
28
14
  #endif
@@ -11,6 +11,7 @@ $LOAD_PATH.each do |load_path|
11
11
  end
12
12
 
13
13
  abort 'numo/narray.h not found' unless have_header('numo/narray.h')
14
+ abort 'log2 not found' unless have_func('log2', 'math.h') || have_library('m', 'log2', 'math.h')
14
15
 
15
16
  if RUBY_PLATFORM.match?(/mswin|cygwin|mingw/)
16
17
  $LOAD_PATH.each do |load_path|
@@ -0,0 +1,116 @@
1
+ #include "fft_shape.h"
2
+ #include "raw_config.h"
3
+ #include <limits.h>
4
+ #include <math.h>
5
+
6
+ static size_t positive_size(VALUE value) {
7
+ if (!RB_INTEGER_TYPE_P(value) || RTEST(rb_funcall(value, rb_intern("<="), 1, INT2FIX(0)))) {
8
+ rb_raise(rb_eArgError, "FFT dimensions and size limit must be positive integers");
9
+ }
10
+ return NUM2SIZET(value);
11
+ }
12
+
13
+ static long read_shape(VALUE shape, size_t *dimensions) {
14
+ Check_Type(shape, T_ARRAY);
15
+ long rank = RARRAY_LEN(shape);
16
+ if (rank < 1 || rank > LARGEST_RANK) {
17
+ rb_raise(rb_eArgError, "FFT shape rank must be between 1 and %d", LARGEST_RANK);
18
+ }
19
+ for (long axis = 0; axis < rank; axis++) {
20
+ dimensions[axis] = positive_size(rb_ary_entry(shape, axis));
21
+ }
22
+ return rank;
23
+ }
24
+
25
+ static size_t doubled_candidate(size_t initial, size_t target, int even, size_t maximum) {
26
+ size_t candidate = initial;
27
+ while (candidate < target || (even && candidate % 2 != 0)) {
28
+ if (candidate > maximum / 2) return 0;
29
+ candidate *= 2;
30
+ }
31
+ return candidate;
32
+ }
33
+
34
+ /* Enumerate 3/5 powers only while they can improve the best 2/3/5 candidate. */
35
+ static size_t next_fast_size(size_t target, int even, size_t maximum) {
36
+ size_t best = 0, five = 1;
37
+ for (;;) {
38
+ size_t three = five;
39
+ for (;;) {
40
+ size_t candidate = doubled_candidate(three, target, even, maximum);
41
+ if (candidate != 0 && (best == 0 || candidate < best)) best = candidate;
42
+ size_t bound = best != 0 ? best : maximum;
43
+ if (three > bound / 3) break;
44
+ three *= 3;
45
+ }
46
+ size_t bound = best != 0 ? best : maximum;
47
+ if (five > bound / 5) break;
48
+ five *= 5;
49
+ }
50
+ return best;
51
+ }
52
+
53
+ /*
54
+ * Generates optional smooth dimensions without allocating numeric buffers.
55
+ * @private
56
+ */
57
+ static VALUE convolver_fft_fast_shape(VALUE self, VALUE shape, VALUE maximum_value) {
58
+ (void)self;
59
+ size_t dimensions[LARGEST_RANK];
60
+ long rank = read_shape(shape, dimensions);
61
+ size_t maximum = positive_size(maximum_value);
62
+ for (long axis = 0; axis < rank; axis++) {
63
+ if (dimensions[axis] > maximum) return Qnil;
64
+ dimensions[axis] = next_fast_size(dimensions[axis], axis == rank - 1, maximum);
65
+ if (dimensions[axis] == 0) return Qnil;
66
+ }
67
+ VALUE result = rb_ary_new_capa(rank);
68
+ for (long axis = 0; axis < rank; axis++) {
69
+ rb_ary_push(result, SIZET2NUM(dimensions[axis]));
70
+ }
71
+ return result;
72
+ }
73
+
74
+ static double large_factor_penalty(size_t value) {
75
+ double penalty = 0.0;
76
+ size_t divisor = 2;
77
+ while (divisor <= value / divisor) {
78
+ while (value % divisor == 0) {
79
+ if (divisor > 7) penalty += 1.5 * (log2((double)divisor) - 3.0);
80
+ value /= divisor;
81
+ }
82
+ divisor += divisor == 2 ? 1 : 2;
83
+ }
84
+ if (value > 7) penalty += 1.5 * (log2((double)value) - 3.0);
85
+ return penalty;
86
+ }
87
+
88
+ /*
89
+ * Scores an admissible candidate; Ruby owns the complete FFT buffer policy.
90
+ * @private
91
+ */
92
+ static VALUE convolver_fft_shape_cost(VALUE self, VALUE shape) {
93
+ (void)self;
94
+ size_t dimensions[LARGEST_RANK], product = 1;
95
+ long rank = read_shape(shape, dimensions);
96
+ for (long axis = 0; axis < rank; axis++) {
97
+ /* Bound trial factorization even if this private primitive is called directly. */
98
+ if (dimensions[axis] > (size_t)INT_MAX / 16) {
99
+ rb_raise(rb_eRangeError, "FFT scoring axis exceeds native integer limit");
100
+ }
101
+ if (dimensions[axis] > SIZE_MAX / product) {
102
+ rb_raise(rb_eRangeError, "FFT scoring size exceeds native element limit");
103
+ }
104
+ product *= dimensions[axis];
105
+ }
106
+ double cost = 0.0;
107
+ for (long axis = 0; axis < rank; axis++) {
108
+ cost += log2((double)dimensions[axis]) + large_factor_penalty(dimensions[axis]);
109
+ }
110
+ return DBL2NUM((double)product * cost);
111
+ }
112
+
113
+ void convolver_init_fft_shape(VALUE convolver) {
114
+ rb_define_singleton_method(convolver, "fft_fast_shape", convolver_fft_fast_shape, 2);
115
+ rb_define_singleton_method(convolver, "fft_shape_cost", convolver_fft_shape_cost, 1);
116
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef CONVOLVER_FFT_SHAPE_H
2
+ #define CONVOLVER_FFT_SHAPE_H
3
+
4
+ #include <ruby.h>
5
+
6
+ void convolver_init_fft_shape(VALUE convolver);
7
+
8
+ #endif
@@ -0,0 +1,11 @@
1
+ #ifndef CONVOLVER_RAW_CONFIG_H
2
+ #define CONVOLVER_RAW_CONFIG_H
3
+ #include <ruby.h>
4
+ #if defined(__SSE__)
5
+ #include <xmmintrin.h>
6
+ #endif
7
+ #if defined(__SSE2__)
8
+ #include <emmintrin.h>
9
+ #endif
10
+ #define LARGEST_RANK 16
11
+ #endif
@@ -0,0 +1,71 @@
1
+ #ifndef CONVOLVER_RAW_HELPERS_H
2
+ #define CONVOLVER_RAW_HELPERS_H
3
+
4
+ static inline size_t checked_add( size_t left, size_t right, const char *description ) {
5
+ if ( right > SIZE_MAX - left ) {
6
+ rb_raise( rb_eRangeError, "%s exceeds native implementation limit", description );
7
+ }
8
+ return left + right;
9
+ }
10
+
11
+ static inline size_t checked_multiply( size_t left, size_t right, const char *description ) {
12
+ if ( left != 0 && right > SIZE_MAX / left ) {
13
+ rb_raise( rb_eRangeError, "%s exceeds native implementation limit", description );
14
+ }
15
+ return left * right;
16
+ }
17
+
18
+ static inline size_t size_from_shape( int rank, const size_t *shape, const char *description ) {
19
+ size_t size = 1;
20
+ int i;
21
+ for ( i = 0; i < rank; i++ ) {
22
+ size = checked_multiply( size, shape[i], description );
23
+ }
24
+ return size;
25
+ }
26
+
27
+ // Sets reverse indices
28
+ static inline void corner_reset( int rank, const size_t *shape, size_t *rev_indices ) {
29
+ int i;
30
+ for ( i = 0; i < rank; i++ ) { rev_indices[i] = shape[i] - 1; }
31
+ }
32
+
33
+ // Counts indices down, returns number of ranks that reset
34
+ static inline int corner_dec( int rank, const size_t *shape, size_t *rev_indices ) {
35
+ int i = 0;
36
+ (void) rank;
37
+ while ( ! rev_indices[i]-- ) {
38
+ rev_indices[i] = shape[i] - 1;
39
+ i++;
40
+ }
41
+ return i;
42
+ }
43
+
44
+ // Generates co-increment steps by rank boundaries crossed, for the outer position as inner position is incremented by 1
45
+ static inline void calc_co_increment(
46
+ int rank, const size_t *outer_shape, const size_t *inner_shape, size_t *co_increment ) {
47
+ size_t factor = 1;
48
+ int i;
49
+ co_increment[0] = 1; // co-increment is always 1 in lowest rank
50
+ for ( i = 0; i < rank; i++ ) {
51
+ size_t skipped = checked_multiply( factor, outer_shape[i] - inner_shape[i], "array offset" );
52
+ co_increment[i+1] = checked_add( co_increment[i], skipped, "array offset" );
53
+ factor = checked_multiply( factor, outer_shape[i], "array size" );
54
+ }
55
+ }
56
+
57
+ static inline size_t maximum_offset(
58
+ int rank, const size_t *outer_shape, const size_t *inner_shape, const char *description ) {
59
+ size_t factor = 1;
60
+ size_t offset = 0;
61
+ int i;
62
+ for ( i = 0; i < rank; i++ ) {
63
+ size_t dimension_offset = checked_multiply( factor, inner_shape[i] - 1, description );
64
+ offset = checked_add( offset, dimension_offset, description );
65
+ factor = checked_multiply( factor, outer_shape[i], "array size" );
66
+ }
67
+ return offset;
68
+ }
69
+
70
+
71
+ #endif
@@ -0,0 +1,93 @@
1
+ /* Intentionally included once per dtype; operation/dtype dispatch is outside hot loops. */
2
+ void RAW_NAME(
3
+ int in_rank, const size_t *in_shape, const RAW_TYPE *in_ptr,
4
+ int kernel_rank, const size_t *kernel_shape, const RAW_TYPE *kernel_ptr,
5
+ int out_rank, const size_t *out_shape, RAW_TYPE *out_ptr ) {
6
+ size_t i, j, input_size, kernel_size, kernel_aligned, out_size, offset;
7
+ size_t maximum_input_offset;
8
+ size_t out_co_incr[LARGEST_RANK + 1], kernel_co_incr[LARGEST_RANK + 1];
9
+ size_t ker_q[LARGEST_RANK], out_q[LARGEST_RANK];
10
+ size_t *kernel_co_incr_cache;
11
+ VALUE cache_storage = 0;
12
+
13
+ kernel_size = size_from_shape( kernel_rank, kernel_shape, "kernel size" );
14
+ kernel_aligned = kernel_size - kernel_size % RAW_LANES;
15
+ out_size = size_from_shape( out_rank, out_shape, "output size" );
16
+ input_size = size_from_shape( in_rank, in_shape, "input size" );
17
+
18
+ calc_co_increment( in_rank, in_shape, out_shape, out_co_incr );
19
+ calc_co_increment( in_rank, in_shape, kernel_shape, kernel_co_incr );
20
+ maximum_input_offset = checked_add(
21
+ maximum_offset( in_rank, in_shape, out_shape, "input offset" ),
22
+ maximum_offset( in_rank, in_shape, kernel_shape, "input offset" ),
23
+ "input offset"
24
+ );
25
+ if ( maximum_input_offset >= input_size ) {
26
+ rb_raise( rb_eRangeError, "input offset exceeds native implementation limit" );
27
+ }
28
+
29
+ kernel_co_incr_cache = RB_ALLOCV_N( size_t, cache_storage, kernel_size );
30
+ kernel_co_incr_cache[0] = 0;
31
+
32
+ corner_reset( kernel_rank, kernel_shape, ker_q );
33
+ for ( i = 1; i < kernel_size; i++ ) {
34
+ kernel_co_incr_cache[i] = checked_add(
35
+ kernel_co_incr_cache[i-1],
36
+ kernel_co_incr[ corner_dec( kernel_rank, kernel_shape, ker_q ) ],
37
+ "kernel offset"
38
+ );
39
+ }
40
+
41
+ offset = 0;
42
+ corner_reset( out_rank, out_shape, out_q );
43
+
44
+ for ( i = 0; i < out_size; i++ ) {
45
+ RAW_TYPE t = 0;
46
+ #if RAW_SIMD
47
+ RAW_VECTOR simd_t = RAW_ZERO();
48
+ RAW_TYPE v[RAW_LANES];
49
+ for ( j = 0; j < kernel_aligned; j += RAW_LANES ) {
50
+ RAW_VECTOR simd_x = RAW_LOAD(kernel_ptr + j);
51
+ #if RAW_LANES == 4
52
+ RAW_VECTOR simd_y = _mm_set_ps(
53
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j+3)]],
54
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j+2)]],
55
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j+1)]],
56
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j)]]);
57
+ #else
58
+ RAW_VECTOR simd_y = _mm_set_pd(
59
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j+1)]],
60
+ in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j)]]);
61
+ #endif
62
+ simd_t = RAW_ADD(RAW_MUL(simd_x, simd_y), simd_t);
63
+ }
64
+ RAW_STORE(v, simd_t);
65
+ #else
66
+ for ( j = 0; j < kernel_aligned; j++ ) {
67
+ t += in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j)]] * kernel_ptr[j];
68
+ }
69
+ #endif
70
+ for ( j = kernel_aligned; j < kernel_size; j++ ) {
71
+ t += in_ptr[offset + kernel_co_incr_cache[RAW_INDEX(j)]] * kernel_ptr[j];
72
+ }
73
+ #if RAW_SIMD
74
+ #if RAW_LANES == 4
75
+ out_ptr[i] = v[0] + v[1] + v[2] + v[3] + t;
76
+ #else
77
+ out_ptr[i] = v[0] + v[1] + t;
78
+ #endif
79
+ #else
80
+ out_ptr[i] = t;
81
+ #endif
82
+
83
+ if ( i + 1 < out_size ) {
84
+ offset = checked_add(
85
+ offset,
86
+ out_co_incr[ corner_dec( out_rank, out_shape, out_q ) ],
87
+ "input offset"
88
+ );
89
+ }
90
+ }
91
+
92
+ RB_ALLOCV_END( cache_storage );
93
+ }
@@ -35,15 +35,15 @@ module Convolver
35
35
  attr_reader :plan, :signal_size, :kernel_size, :limits
36
36
 
37
37
  def common!
38
- limits.buffer!(plan.result_size, 4, 'result buffer')
39
- limits.buffer!(signal_size, 4, 'signal conversion buffer')
40
- limits.buffer!(kernel_size, 4, 'kernel conversion buffer')
38
+ limits.buffer!(plan.result_size, plan.dtype::ELEMENT_BYTE_SIZE, 'result buffer')
39
+ limits.buffer!(signal_size, plan.dtype::ELEMENT_BYTE_SIZE, 'signal conversion buffer')
40
+ limits.buffer!(kernel_size, plan.dtype::ELEMENT_BYTE_SIZE, 'kernel conversion buffer')
41
41
  end
42
42
 
43
43
  def extension!
44
44
  return if plan.valid?
45
45
 
46
- limits.buffer!(plan.extended_size, 4, 'extended signal buffer')
46
+ limits.buffer!(plan.extended_size, plan.dtype::ELEMENT_BYTE_SIZE, 'extended signal buffer')
47
47
  return if plan.boundary == :constant
48
48
 
49
49
  plan.extended_shape.each do |length|
@@ -5,6 +5,7 @@ module Convolver
5
5
  # @private
6
6
  class CircularFftOperation
7
7
  def initialize(operation, signal, kernel, plan)
8
+ @dtype = plan.dtype
8
9
  @real_axis = plan.fft_buffers.real_axis
9
10
  @operation = operation
10
11
  @signal = Numo::DFloat.cast(signal)
@@ -28,7 +29,7 @@ module Convolver
28
29
 
29
30
  private
30
31
 
31
- attr_reader :operation, :signal, :kernel, :real_axis
32
+ attr_reader :operation, :signal, :kernel, :real_axis, :dtype
32
33
 
33
34
  def real_product(real_axis)
34
35
  final_axis = signal.ndim - 1
@@ -36,7 +37,7 @@ module Convolver
36
37
  prepared_kernel = prepare_real_operand(kernel, real_axis, final_axis)
37
38
  result = real_spectrum_product(prepared_signal, prepared_kernel)
38
39
  result = result.swapaxes(real_axis, final_axis) unless real_axis == final_axis
39
- Numo::SFloat.cast(result)
40
+ dtype.cast(result)
40
41
  end
41
42
 
42
43
  def prepare_real_operand(value, real_axis, final_axis)
@@ -55,7 +56,7 @@ module Convolver
55
56
  kernel_spectrum = Numo::Pocketfft.fftn(kernel)
56
57
  kernel_spectrum.inplace.conj if operation == :correlation
57
58
  result = Numo::Pocketfft.ifftn(signal_spectrum.inplace * kernel_spectrum).real
58
- Numo::SFloat.cast(result)
59
+ dtype.cast(result)
59
60
  end
60
61
 
61
62
  def fold_kernel(source, signal_shape, anchors)
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Convolver
4
+ # Immutable measured-environment coefficients in seconds; ranks one through three.
5
+ # @private
6
+ module CostProfiles
7
+ MAC = {
8
+ direct: [[6.55e-6, 3.24e-10, 7.08e-10].freeze, [6.55e-6, 3.24e-10, 7.08e-10].freeze].freeze,
9
+ fixed: [1.64e-5, 5.12e-5, 1.13e-4].freeze,
10
+ transform: [8.02e-10, 7.17e-10, 1.54e-9].freeze,
11
+ indices: { reflect: 1.33e-7, nearest: 1.46e-7, mirror: 1.57e-7, wrap: 3.23e-8 }.freeze,
12
+ folding: [3.78e-7, 3.99e-7, 5.09e-7].freeze
13
+ }.freeze
14
+ X86 = {
15
+ direct: [[1.59e-5, 3.08e-10, 2.52e-10].freeze, [1.63e-5, 3.22e-10, 3.96e-10].freeze].freeze,
16
+ fixed: [3.77e-5, 1.20e-4, 1.98e-4].freeze,
17
+ transform: [2.00e-9, 2.13e-9, 3.13e-9].freeze,
18
+ indices: { reflect: 2.32e-7, nearest: 2.88e-7, mirror: 2.62e-7, wrap: 1.11e-7 }.freeze,
19
+ folding: [8.20e-7, 7.51e-7, 9.79e-7].freeze
20
+ }.freeze
21
+ ARM = {
22
+ direct: [[1.88e-5, 5.12e-10, 5.96e-10].freeze, [1.88e-5, 5.12e-10, 5.96e-10].freeze].freeze,
23
+ fixed: [4.20e-5, 1.20e-4, 2.70e-4].freeze,
24
+ transform: [3.29e-9, 2.89e-9, 3.74e-9].freeze,
25
+ indices: { reflect: 2.62e-7, nearest: 3.67e-7, mirror: 2.87e-7, wrap: 1.50e-7 }.freeze,
26
+ folding: [9.97e-7, 8.80e-7, 1.24e-6].freeze
27
+ }.freeze
28
+ def self.for_platform(platform)
29
+ case platform
30
+ when /\Aarm64-darwin/ then MAC
31
+ when /\Ax86_64-linux/ then X86
32
+ when /\Aaarch64-linux/ then ARM
33
+ end
34
+ end
35
+
36
+ CURRENT = for_platform(RUBY_PLATFORM)
37
+
38
+ def self.for_rank(rank)
39
+ CURRENT if (1..3).cover?(rank)
40
+ end
41
+ end
42
+
43
+ private_constant :CostProfiles
44
+ end