Skip to content

Commit

Permalink
bench: refactor benchmarks in math/base/special/rsqrtf
Browse files Browse the repository at this point in the history
PR-URL: #5223
Co-authored-by: Athan Reines <[email protected]>
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
gururaj1512 and kgryte authored Feb 16, 2025
1 parent 8449e0b commit f6cca6b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var pkg = require( './../package.json' ).name;
var rsqrtf = require( './../lib' );
Expand All @@ -34,10 +34,13 @@ bench( pkg, function benchmark( b ) {
var y;
var i;

x = randu( 100, 0.0, 100000.0, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100000.0 ) - 0.0;
y = rsqrtf( x );
y = rsqrtf( x[ i % x.length ] );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -55,10 +58,13 @@ bench( pkg+'::built-in', function benchmark( b ) {
var y;
var i;

x = randu( 100, 0.0, 100000.0, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100000.0 ) - 0.0;
y = 1.0 / Math.sqrt( x ); // eslint-disable-line stdlib/no-builtin-math
y = 1.0 / Math.sqrt( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var randu = require( '@stdlib/random/array/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand All @@ -43,10 +43,13 @@ bench( pkg+'::native', opts, function benchmark( b ) {
var y;
var i;

x = randu( 100, 0.0, 100000.0, {
'dtype': 'float32'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100000.0 ) - 0.0;
y = rsqrtf( x );
y = rsqrtf( x[ i % x.length ] );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2020 The Stdlib Authors.
# Copyright (c) 2025 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@
# limitations under the License.
#/


# VARIABLES #

ifndef VERBOSE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,15 +90,18 @@ static float rand_float( void ) {
*/
static double benchmark( void ) {
double elapsed;
float x;
float y;
float x[ 100 ];
double t;
float y;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = 100000.0f * rand_float();
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = rand_float() * 100000.0f;
y = 1.0f / sqrtf( x );
y = 1.0f / sqrtf( x[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#/
# @license Apache-2.0
#
# Copyright (c) 2020 The Stdlib Authors.
# Copyright (c) 2025 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,15 +88,15 @@ c_targets := benchmark.out
# RULES #

#/
# Compiles source files.
# Compiles C source files.
#
# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
# @param {string} [CFLAGS] - C compiler options
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
# @param {string} [SOURCE_FILES] - list of source files
# @param {string} SOURCE_FILES - list of C source files
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`)
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
# @param {string} [C_COMPILER] - C compiler
# @param {string} [CFLAGS] - C compiler flags
# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code
#
# @example
# make
Expand All @@ -112,13 +112,13 @@ all: $(c_targets)
# Compiles C source files.
#
# @private
# @param {string} CC - C compiler (e.g., `gcc`)
# @param {string} CFLAGS - C compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
# @param {string} SOURCE_FILES - list of C source files
# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`)
# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`)
# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} CC - C compiler
# @param {string} CFLAGS - C compiler flags
# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code
#/
$(c_targets): %.out: %.c
$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 The Stdlib Authors.
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,15 +91,18 @@ static float rand_float( void ) {
*/
static double benchmark( void ) {
double elapsed;
float x;
float y;
float x[ 100 ];
double t;
float y;
int i;

for ( i = 0; i < 100; i++ ) {
x[ i ] = 100000.0f * rand_float();
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x = rand_float() * 100000.0f;
y = stdlib_base_rsqrtf( x );
y = stdlib_base_rsqrtf( x[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
Expand Down

1 comment on commit f6cca6b

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/base/special/rsqrtf $\color{green}176/176$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}176/176$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.