sscal.c (1512B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2019 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/sscal.h" 20 21 /** 22 * Multiplies a single-precision floating-point vector `X` by a constant. 23 * 24 * @param N number of indexed elements 25 * @param alpha scalar 26 * @param X input array 27 * @param stride index increment 28 */ 29 void c_sscal( const int N, const float alpha, float *X, const int stride ) { 30 int i; 31 int m; 32 33 if ( N <= 0 || stride <= 0 || alpha == 1.0f ) { 34 return; 35 } 36 // Use loop unrolling if the stride is equal to `1`... 37 if ( stride == 1 ) { 38 m = N % 5; 39 40 // If we have a remainder, run a clean-up loop... 41 if ( m > 0 ) { 42 for ( i = 0; i < m; i++ ) { 43 X[ i ] *= alpha; 44 } 45 } 46 if ( N < 5 ) { 47 return; 48 } 49 for ( i = m; i < N; i += 5 ) { 50 X[ i ] *= alpha; 51 X[ i+1 ] *= alpha; 52 X[ i+2 ] *= alpha; 53 X[ i+3 ] *= alpha; 54 X[ i+4 ] *= alpha; 55 } 56 return; 57 } 58 for ( i = 0; i < N*stride; i += stride ) { 59 X[ i ] *= alpha; 60 } 61 return; 62 }