Skip to content

Commit 119e99d

Browse files
steff456kgryte
andauthored
feat!: migrate to using stdlib complex number types
In addition to migrating to use stdlib complex number types, this commit adds an addon interface to allow testing the C implementation from JavaScript. BREAKING CHANGE: migrate to using stdlib complex number types To migrate, users need to change `double complex` to `stdlib_complex128_t`. PR-URL: #912 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Private-ref: stdlib-js/todo#1454
1 parent 059038f commit 119e99d

File tree

14 files changed

+643
-57
lines changed

14 files changed

+643
-57
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,20 @@ for ( i = 0; i < 100; i++ ) {
135135
Computes the squared [absolute value][absolute-value] of a double-precision complex floating-point number.
136136

137137
```c
138-
#include <complex.h>
138+
#include "stdlib/complex/float64.h"
139139

140-
double y = stdlib_base_cabs2( 5.0+3.0*I );
140+
stdlib_complex128_t z = stdlib_complex128( 5.0, 3.0 );
141+
142+
double y = stdlib_base_cabs2( z );
141143
// returns 34.0
142144
```
143145

144146
The function accepts the following arguments:
145147

146-
- **z**: `[in] double complex` input value.
148+
- **z**: `[in] stdlib_complex128_t` input value.
147149

148150
```c
149-
double stdlib_base_cabs2( const double complex z );
151+
double stdlib_base_cabs2( const stdlib_complex128_t z );
150152
```
151153
152154
</section>
@@ -169,19 +171,28 @@ double stdlib_base_cabs2( const double complex z );
169171
170172
```c
171173
#include "stdlib/math/base/special/cabs2.h"
174+
#include "stdlib/complex/float64.h"
175+
#include "stdlib/complex/reim.h"
172176
#include <stdio.h>
173-
#include <complex.h>
174177
175178
int main() {
176-
double complex x[] = { 3.14+1.0*I, -3.14-1.0*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I };
177-
178-
double complex v;
179+
const stdlib_complex128_t x[] = {
180+
stdlib_complex128( 3.14, 1.0 ),
181+
stdlib_complex128( -3.14, -1.0 ),
182+
stdlib_complex128( 0.0, 0.0 ),
183+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
184+
};
185+
186+
stdlib_complex128_t v;
187+
double re;
188+
double im;
179189
double y;
180190
int i;
181191
for ( i = 0; i < 4; i++ ) {
182192
v = x[ i ];
183193
y = stdlib_base_cabs2( v );
184-
printf( "f(%lf + %lf) = %lf\n", creal( v ), cimag( v ), y );
194+
stdlib_reim( v, &re, &im );
195+
printf( "f(%lf + %lf) = %lf\n", re, im, y );
185196
}
186197
}
187198
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 uniform = require( '@stdlib/random/base/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var Complex128 = require( '@stdlib/complex/float64' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var cabs2 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( cabs2 instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::native', opts, function benchmark( b ) {
43+
var values;
44+
var y;
45+
var i;
46+
47+
values = [
48+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
49+
new Complex128( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
50+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
y = cabs2( values[ i%values.length ] );
55+
if ( isnan( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
}
59+
b.toc();
60+
if ( isnan( y ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* Benchmark `cabs2`.
2121
*/
2222
#include "stdlib/math/base/special/cabs2.h"
23-
#include <complex.h>
23+
#include "stdlib/complex/float64.h"
24+
#include "stdlib/complex/reim.h"
2425
#include <stdlib.h>
2526
#include <stdio.h>
2627
#include <math.h>
@@ -94,19 +95,20 @@ double rand_double() {
9495
* @return elapsed time in seconds
9596
*/
9697
double benchmark() {
97-
double complex z;
9898
double elapsed;
9999
double re;
100100
double im;
101101
double y;
102102
double t;
103103
int i;
104104

105+
stdlib_complex128_t z;
106+
105107
t = tic();
106108
for ( i = 0; i < ITERATIONS; i++ ) {
107109
re = ( 1000.0*rand_double() ) - 500.0;
108110
im = ( 1000.0*rand_double() ) - 500.0;
109-
z = re + im*I;
111+
z = stdlib_complex128( re, im );
110112
y = stdlib_base_cabs2( z );
111113
if ( y != y ) {
112114
printf( "should not return NaN\n" );
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2023 The Stdlib Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# A `.gyp` file for building a Node.js native add-on.
18+
#
19+
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
20+
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
21+
{
22+
# List of files to include in this file:
23+
'includes': [
24+
'./include.gypi',
25+
],
26+
27+
# Define variables to be used throughout the configuration for all targets:
28+
'variables': {
29+
# Target name should match the add-on export name:
30+
'addon_target_name%': 'addon',
31+
32+
# Set variables based on the host OS:
33+
'conditions': [
34+
[
35+
'OS=="win"',
36+
{
37+
# Define the object file suffix:
38+
'obj': 'obj',
39+
},
40+
{
41+
# Define the object file suffix:
42+
'obj': 'o',
43+
}
44+
], # end condition (OS=="win")
45+
], # end conditions
46+
}, # end variables
47+
48+
# Define compile targets:
49+
'targets': [
50+
51+
# Target to generate an add-on:
52+
{
53+
# The target name should match the add-on export name:
54+
'target_name': '<(addon_target_name)',
55+
56+
# Define dependencies:
57+
'dependencies': [],
58+
59+
# Define directories which contain relevant include headers:
60+
'include_dirs': [
61+
# Local include directory:
62+
'<@(include_dirs)',
63+
],
64+
65+
# List of source files:
66+
'sources': [
67+
'<@(src_files)',
68+
],
69+
70+
# Settings which should be applied when a target's object files are used as linker input:
71+
'link_settings': {
72+
# Define libraries:
73+
'libraries': [
74+
'<@(libraries)',
75+
],
76+
77+
# Define library directories:
78+
'library_dirs': [
79+
'<@(library_dirs)',
80+
],
81+
},
82+
83+
# C/C++ compiler flags:
84+
'cflags': [
85+
# Enable commonly used warning options:
86+
'-Wall',
87+
88+
# Aggressive optimization:
89+
'-O3',
90+
],
91+
92+
# C specific compiler flags:
93+
'cflags_c': [
94+
# Specify the C standard to which a program is expected to conform:
95+
'-std=c99',
96+
],
97+
98+
# C++ specific compiler flags:
99+
'cflags_cpp': [
100+
# Specify the C++ standard to which a program is expected to conform:
101+
'-std=c++11',
102+
],
103+
104+
# Linker flags:
105+
'ldflags': [],
106+
107+
# Apply conditions based on the host OS:
108+
'conditions': [
109+
[
110+
'OS=="mac"',
111+
{
112+
# Linker flags:
113+
'ldflags': [
114+
'-undefined dynamic_lookup',
115+
'-Wl,-no-pie',
116+
'-Wl,-search_paths_first',
117+
],
118+
},
119+
], # end condition (OS=="mac")
120+
[
121+
'OS!="win"',
122+
{
123+
# C/C++ flags:
124+
'cflags': [
125+
# Generate platform-independent code:
126+
'-fPIC',
127+
],
128+
},
129+
], # end condition (OS!="win")
130+
], # end conditions
131+
}, # end target <(addon_target_name)
132+
133+
# Target to copy a generated add-on to a standard location:
134+
{
135+
'target_name': 'copy_addon',
136+
137+
# Declare that the output of this target is not linked:
138+
'type': 'none',
139+
140+
# Define dependencies:
141+
'dependencies': [
142+
# Require that the add-on be generated before building this target:
143+
'<(addon_target_name)',
144+
],
145+
146+
# Define a list of actions:
147+
'actions': [
148+
{
149+
'action_name': 'copy_addon',
150+
'message': 'Copying addon...',
151+
152+
# Explicitly list the inputs in the command-line invocation below:
153+
'inputs': [],
154+
155+
# Declare the expected outputs:
156+
'outputs': [
157+
'<(addon_output_dir)/<(addon_target_name).node',
158+
],
159+
160+
# Define the command-line invocation:
161+
'action': [
162+
'cp',
163+
'<(PRODUCT_DIR)/<(addon_target_name).node',
164+
'<(addon_output_dir)/<(addon_target_name).node',
165+
],
166+
},
167+
], # end actions
168+
}, # end target copy_addon
169+
], # end targets
170+
}

lib/node_modules/@stdlib/math/base/special/cabs2/examples/c/example.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cabs2.h"
20+
#include "stdlib/complex/float64.h"
21+
#include "stdlib/complex/reim.h"
2022
#include <stdio.h>
21-
#include <complex.h>
2223

2324
int main() {
24-
// cppcheck-suppress nanInArithmeticExpression
25-
double complex x[] = { 3.14+1.0*I, -3.14-1.0*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I };
25+
const stdlib_complex128_t x[] = {
26+
stdlib_complex128( 3.14, 1.0 ),
27+
stdlib_complex128( -3.14, -1.0 ),
28+
stdlib_complex128( 0.0, 0.0 ),
29+
stdlib_complex128( 0.0/0.0, 0.0/0.0 )
30+
};
2631

27-
double complex v;
32+
stdlib_complex128_t v;
33+
double re;
34+
double im;
2835
double y;
2936
int i;
3037
for ( i = 0; i < 4; i++ ) {
3138
v = x[ i ];
3239
y = stdlib_base_cabs2( v );
33-
printf( "f(%lf + %lf) = %lf\n", creal( v ), cimag( v ), y );
40+
stdlib_reim( v, &re, &im );
41+
printf( "f(%lf + %lf) = %lf\n", re, im, y );
3442
}
3543
}

0 commit comments

Comments
 (0)