Skip to content

Commit b56c697

Browse files
kawaho2PranavchikuPlaneshifter
authored
feat: add C implementation for math/base/special/asech
PR-URL: #1750 Closes: #862 --------- Signed-off-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent fa68dfe commit b56c697

File tree

17 files changed

+1476
-0
lines changed

17 files changed

+1476
-0
lines changed

lib/node_modules/@stdlib/math/base/special/asech/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,91 @@ for ( i = 0; i < x.length; i++ ) {
8181

8282
<!-- /.examples -->
8383

84+
<!-- C interface documentation. -->
85+
86+
* * *
87+
88+
<section class="c">
89+
90+
## C APIs
91+
92+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
93+
94+
<section class="intro">
95+
96+
</section>
97+
98+
<!-- /.intro -->
99+
100+
<!-- C usage documentation. -->
101+
102+
<section class="usage">
103+
104+
### Usage
105+
106+
```c
107+
#include "stdlib/math/base/special/asech.h"
108+
```
109+
110+
#### stdlib_base_asech( x )
111+
112+
Computes the [hyperbolic arcsecant][hyperbolic-arcsecant] of `x`.
113+
114+
```c
115+
double out = stdlib_base_asech( 1.0 );
116+
// returns 0.0
117+
```
118+
119+
The function accepts the following arguments:
120+
121+
- **x**: `[in] double` input value.
122+
123+
```c
124+
double stdlib_base_asech( const double x );
125+
```
126+
127+
</section>
128+
129+
<!-- /.usage -->
130+
131+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="notes">
134+
135+
</section>
136+
137+
<!-- /.notes -->
138+
139+
<!-- C API usage examples. -->
140+
141+
<section class="examples">
142+
143+
### Examples
144+
145+
```c
146+
#include "stdlib/math/base/special/asech.h"
147+
#include <stdio.h>
148+
149+
int main( void ) {
150+
const double x[] = { 0.0, 0.5, 1.0, 1.89, 2.33, 3.22, 3.67, 4.11, 4.56, 5.0 };
151+
152+
double v;
153+
int i;
154+
for ( i = 0; i < 10; i++ ) {
155+
v = stdlib_base_asech( x[ i ] );
156+
printf( "asech(%lf) = %lf\n", x[ i ], v );
157+
}
158+
}
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
</section>
166+
167+
<!-- /.c -->
168+
84169
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
85170

86171
<section class="related">
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/base/randu' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var asech = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( asech instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = randu();
49+
y = asech( x );
50+
if ( isnan( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2024 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+
20+
# VARIABLES #
21+
22+
ifndef VERBOSE
23+
QUIET := @
24+
endif
25+
26+
# Determine the OS:
27+
#
28+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
29+
# [2]: http://stackoverflow.com/a/27776822/2225624
30+
OS ?= $(shell uname)
31+
ifneq (, $(findstring MINGW,$(OS)))
32+
OS := WINNT
33+
else
34+
ifneq (, $(findstring MSYS,$(OS)))
35+
OS := WINNT
36+
else
37+
ifneq (, $(findstring CYGWIN,$(OS)))
38+
OS := WINNT
39+
endif
40+
endif
41+
endif
42+
43+
# Define the program used for compiling C source files:
44+
ifdef C_COMPILER
45+
CC := $(C_COMPILER)
46+
else
47+
CC := gcc
48+
endif
49+
50+
# Define the command-line options when compiling C files:
51+
CFLAGS ?= \
52+
-std=c99 \
53+
-O3 \
54+
-Wall \
55+
-pedantic
56+
57+
# Determine whether to generate [position independent code][1]:
58+
#
59+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
60+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
61+
ifeq ($(OS), WINNT)
62+
fPIC ?=
63+
else
64+
fPIC ?= -fPIC
65+
endif
66+
67+
# List of C targets:
68+
c_targets := benchmark.out
69+
70+
71+
# TARGETS #
72+
73+
# Default target.
74+
#
75+
# This target is the default target.
76+
77+
all: $(c_targets)
78+
79+
.PHONY: all
80+
81+
82+
# Compile C source.
83+
#
84+
# This target compiles C source files.
85+
86+
$(c_targets): %.out: %.c
87+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $< -lm
88+
89+
90+
# Run a benchmark.
91+
#
92+
# This target runs a benchmark.
93+
94+
run: $(c_targets)
95+
$(QUIET) ./$<
96+
97+
.PHONY: run
98+
99+
100+
# Perform clean-up.
101+
#
102+
# This target removes generated files.
103+
104+
clean:
105+
$(QUIET) -rm -f *.o *.out
106+
107+
.PHONY: clean
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
20+
* Benchmark `asech`.
21+
*/
22+
#include <stdlib.h>
23+
#include <stdio.h>
24+
#include <math.h>
25+
#include <time.h>
26+
#include <sys/time.h>
27+
28+
#define NAME "asech"
29+
#define ITERATIONS 1000000
30+
#define REPEATS 3
31+
32+
/**
33+
* Prints the TAP version.
34+
*/
35+
void print_version() {
36+
printf( "TAP version 13\n" );
37+
}
38+
39+
/**
40+
* Prints the TAP summary.
41+
*
42+
* @param total total number of tests
43+
* @param passing total number of passing tests
44+
*/
45+
void print_summary( int total, int passing ) {
46+
printf( "#\n" );
47+
printf( "1..%d\n", total ); // TAP plan
48+
printf( "# total %d\n", total );
49+
printf( "# pass %d\n", passing );
50+
printf( "#\n" );
51+
printf( "# ok\n" );
52+
}
53+
54+
/**
55+
* Prints benchmarks results.
56+
*
57+
* @param elapsed elapsed time in seconds
58+
*/
59+
void print_results( double elapsed ) {
60+
double rate = (double)ITERATIONS / elapsed;
61+
printf( " ---\n" );
62+
printf( " iterations: %d\n", ITERATIONS );
63+
printf( " elapsed: %0.9f\n", elapsed );
64+
printf( " rate: %0.9f\n", rate );
65+
printf( " ...\n" );
66+
}
67+
68+
/**
69+
* Returns a clock time.
70+
*
71+
* @return clock time
72+
*/
73+
double tic() {
74+
struct timeval now;
75+
gettimeofday( &now, NULL );
76+
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
77+
}
78+
79+
/**
80+
* Generates a random number on the interval [0,1].
81+
*
82+
* @return random number
83+
*/
84+
double rand_double() {
85+
int r = rand();
86+
return (double)r / ( (double)RAND_MAX + 1.0 );
87+
}
88+
89+
/**
90+
* Runs a benchmark.
91+
*
92+
* @return elapsed time in seconds
93+
*/
94+
double benchmark() {
95+
double elapsed;
96+
double x;
97+
double y;
98+
double t;
99+
int i;
100+
101+
t = tic();
102+
for ( i = 0; i < ITERATIONS; i++ ) {
103+
x = rand_double();
104+
y = acosh( 1.0 / x );
105+
if ( y != y ) {
106+
printf( "should not return NaN\n" );
107+
break;
108+
}
109+
}
110+
elapsed = tic() - t;
111+
if ( y != y ) {
112+
printf( "should not return NaN\n" );
113+
}
114+
return elapsed;
115+
}
116+
117+
/**
118+
* Main execution sequence.
119+
*/
120+
int main( void ) {
121+
double elapsed;
122+
int i;
123+
124+
// Use the current time to seed the random number generator:
125+
srand( time( NULL ) );
126+
127+
print_version();
128+
for ( i = 0; i < REPEATS; i++ ) {
129+
printf( "# c::%s\n", NAME );
130+
elapsed = benchmark();
131+
print_results( elapsed );
132+
printf( "ok %d benchmark finished\n", i+1 );
133+
}
134+
print_summary( REPEATS, REPEATS );
135+
}

0 commit comments

Comments
 (0)