srev.c (1851B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include "stdlib/blas/ext/base/srev.h" 20 #include <stdint.h> 21 22 /** 23 * Reverses a single-precision floating-point strided array in-place. 24 * 25 * @param N number of indexed elements 26 * @param X input array 27 * @param stride index increment 28 */ 29 void c_srev( const int64_t N, float *X, const int64_t stride ) { 30 int64_t ix; 31 int64_t iy; 32 int64_t m; 33 int64_t n; 34 int64_t i; 35 float tmp; 36 37 if ( N <= 0 ) { 38 return; 39 } 40 n = N / 2; 41 42 // Use loop unrolling if the stride is equal to `1`... 43 if ( stride == 1 ) { 44 m = n % 3; 45 iy = N - 1; 46 47 // If we have a remainder, run a clean-up loop... 48 if ( m > 0 ) { 49 for ( ix = 0; ix < m; ix++ ) { 50 tmp = X[ ix ]; 51 X[ ix ] = X[ iy ]; 52 X[ iy ] = tmp; 53 iy -= 1; 54 } 55 } 56 if ( n < 3 ) { 57 return; 58 } 59 for ( ix = m; ix < n; ix += 3 ) { 60 tmp = X[ ix ]; 61 X[ ix ] = X[ iy ]; 62 X[ iy ] = tmp; 63 64 tmp = X[ ix+1 ]; 65 X[ ix+1 ] = X[ iy-1 ]; 66 X[ iy-1 ] = tmp; 67 68 tmp = X[ ix+2 ]; 69 X[ ix+2 ] = X[ iy-2 ]; 70 X[ iy-2 ] = tmp; 71 72 iy -= 3; 73 } 74 return; 75 } 76 if ( stride < 0 ) { 77 ix = (1-N) * stride; 78 } else { 79 ix = 0; 80 } 81 iy = ix + ((N-1)*stride); 82 for ( i = 0; i < n; i++ ) { 83 tmp = X[ ix ]; 84 X[ ix ] = X[ iy ]; 85 X[ iy ] = tmp; 86 ix += stride; 87 iy -= stride; 88 } 89 return; 90 }