Skip to content

Commit 1954bd5

Browse files
feat(add c implementation): add math/base/special/cabs2f
--- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: passed - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 97cc4ae commit 1954bd5

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

lib/node_modules/@stdlib/math/base/special/cabs2f/benchmark/c/benchmark.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/math/base/special/cabs2f.h"
20-
#include "stdlib/complex/float32/ctor.h"
21-
#include "stdlib/complex/float32/reim.h"
2219
#include <stdlib.h>
2320
#include <stdio.h>
2421
#include <math.h>
22+
#include <complex.h>
2523
#include <sys/time.h>
2624

2725
#define NAME "cabs2f"
@@ -85,30 +83,36 @@ static float rand_float( void ) {
8583
return (float)r / ( (float)RAND_MAX + 1.0f );
8684
}
8785

86+
/**
87+
* Computes the squared absolute value of a complex number.
88+
*
89+
* @param z input value
90+
* @return squared absolute value
91+
*/
92+
float cabs2f( float complex z ) {
93+
return ( crealf(z)*crealf(z) ) + ( cimagf(z)*cimagf(z) );
94+
}
95+
8896
/**
8997
* Runs a benchmark.
9098
*
9199
* @return elapsed time in seconds
92100
*/
93101
static double benchmark( void ) {
102+
float complex z;
94103
double elapsed;
95104
double t;
96105
float re;
97106
float im;
98107
float y;
99108
int i;
100109

101-
stdlib_complex64_t z[ 100 ];
102-
103-
for ( i = 0; i < 100; i++ ) {
104-
re = ( 1000.0f * rand_float() ) - 500.0f;
105-
im = ( 1000.0f * rand_float() ) - 500.0f;
106-
z[ i ] = stdlib_complex64( re, im );
107-
}
108-
109110
t = tic();
110111
for ( i = 0; i < ITERATIONS; i++ ) {
111-
y = stdlib_base_cabs2f( z[ i % 100 ] );
112+
re = ( 1000.0f*rand_float() ) - 500.0f;
113+
im = ( 1000.0f*rand_float() ) - 500.0f;
114+
z = re + im*I;
115+
y = cabs2f( z );
112116
if ( y != y ) {
113117
printf( "should not return NaN\n" );
114118
break;

lib/node_modules/@stdlib/math/base/special/cabs2f/benchmark/c/native/benchmark.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cabs2f.h"
20-
#include <complex.h>
20+
#include "stdlib/complex/float32/ctor.h"
21+
#include "stdlib/complex/float32/reim.h"
2122
#include <stdlib.h>
2223
#include <stdio.h>
2324
#include <math.h>
@@ -91,20 +92,24 @@ static float rand_float( void ) {
9192
* @return elapsed time in seconds
9293
*/
9394
static double benchmark( void ) {
94-
float complex z;
9595
double elapsed;
9696
double t;
9797
float re;
9898
float im;
9999
float y;
100100
int i;
101101

102+
stdlib_complex64_t z[ 100 ];
103+
104+
for ( i = 0; i < 100; i++ ) {
105+
re = ( 1000.0f * rand_float() ) - 500.0f;
106+
im = ( 1000.0f * rand_float() ) - 500.0f;
107+
z[ i ] = stdlib_complex64( re, im );
108+
}
109+
102110
t = tic();
103111
for ( i = 0; i < ITERATIONS; i++ ) {
104-
re = ( 1000.0f*rand_float() ) - 500.0f;
105-
im = ( 1000.0f*rand_float() ) - 500.0f;
106-
z = re + im*I;
107-
y = stdlib_base_cabs2f( z );
112+
y = stdlib_base_cabs2f( z[ i % 100 ] );
108113
if ( y != y ) {
109114
printf( "should not return NaN\n" );
110115
break;

0 commit comments

Comments
 (0)