sswap.c (1888B)
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/base/sswap.h" 20 21 /** 22 * Interchanges two single-precision floating-point vectors. 23 * 24 * @param N number of elements to swap 25 * @param X first input array 26 * @param strideX X stride length 27 * @param Y second input array 28 * @param strideY Y stride length 29 */ 30 void c_sswap( const int N, float *X, const int strideX, float *Y, const int strideY ) { 31 float tmp; 32 int ix; 33 int iy; 34 int i; 35 int m; 36 37 if ( N <= 0 ) { 38 return; 39 } 40 // If both strides are equal to `1`, use unrolled loops... 41 if ( strideX == 1 && strideY == 1 ) { 42 m = N % 3; 43 44 // If we have a remainder, do a clean-up loop... 45 if ( m > 0 ) { 46 for ( i = 0; i < m; i++ ) { 47 tmp = X[ i ]; 48 X[ i ] = Y[ i ]; 49 Y[ i ] = tmp; 50 } 51 if ( N < 3 ) { 52 return; 53 } 54 } 55 for ( i = m; i < N; i += 3 ) { 56 tmp = X[ i ]; 57 X[ i ] = Y[ i ]; 58 Y[ i ] = tmp; 59 60 tmp = X[ i+1 ]; 61 X[ i+1 ] = Y[ i+1 ]; 62 Y[ i+1 ] = tmp; 63 64 tmp = X[ i+2 ]; 65 X[ i+2 ] = Y[ i+2 ]; 66 Y[ i+2 ] = tmp; 67 } 68 return; 69 } 70 if ( strideX < 0 ) { 71 ix = (1-N) * strideX; 72 } else { 73 ix = 0; 74 } 75 if ( strideY < 0 ) { 76 iy = (1-N) * strideY; 77 } else { 78 iy = 0; 79 } 80 for ( i = 0; i < N; i++ ) { 81 tmp = X[ ix ]; 82 X[ ix ] = Y[ iy ]; 83 Y[ iy ] = tmp; 84 ix += strideX; 85 iy += strideY; 86 } 87 return; 88 }