Skip to content

Commit e341eb5

Browse files
vivekmaurya001stdlib-botanandkaranubc
authored
refactor!: update math/base/special/cflipsignf to follow latest project conventions
This commit refactors `cflipsignf` to accept the `stdlib_complex64` type. BREAKING CHANGE: use `stdlib_complex64` type To migrate, users should no longer use the built-in C99 complex type, but, instead, use the single-precision complex floating-point type exposed by `@stdlib/complex/float32/ctor`. PR-URL: #4754 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Karan Anand <[email protected]> Reviewed-by: Karan Anand <[email protected]> Reviewed-by: Gunj Joshi <[email protected]>
1 parent 7673be5 commit e341eb5

File tree

15 files changed

+839
-74
lines changed

15 files changed

+839
-74
lines changed

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

+32-11
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,28 @@ for ( i = 0; i < 100; i++ ) {
131131
Returns a single-precision complex floating-point number with the same magnitude as `z` and the sign of `y*z`.
132132

133133
```c
134-
#include <complex.h>
134+
#include "stdlib/complex/float32/ctor.h"
135+
#include "stdlib/complex/float32/real.h"
136+
#include "stdlib/complex/float32/imag.h"
135137

136-
float complex y = stdlib_base_cflipsignf( 2.0-1.0*I, -1.0 );
137-
// returns -2.0+1.0*I
138+
stdlib_complex64_t z = stdlib_complex64( 2.5f, -1.5f );
139+
140+
stdlib_complex64_t out = stdlib_base_cflipsignf( z, -1.0f );
141+
142+
float re = stdlib_complex64_real( out );
143+
// returns -2.5f
144+
145+
float im = stdlib_complex64_imag( out );
146+
// returns 1.5f
138147
```
139148

140149
The function accepts the following arguments:
141150

142-
- **z**: `[in] float complex` input value.
151+
- **z**: `[in] stdlib_complex64_t` input value.
143152
- **y**: `[in] float` number from which to derive the sign.
144153

145154
```c
146-
float complex stdlib_base_cflipsignf( const float complex z, const float y );
155+
stdlib_complex64_t stdlib_base_cflipsignf( const stdlib_complex64_t z, const float y );
147156
```
148157
149158
</section>
@@ -166,19 +175,31 @@ float complex stdlib_base_cflipsignf( const float complex z, const float y );
166175
167176
```c
168177
#include "stdlib/math/base/special/cflipsignf.h"
178+
#include "stdlib/complex/float32/ctor.h"
179+
#include "stdlib/complex/float32/reim.h"
169180
#include <stdio.h>
170-
#include <complex.h>
171181
172182
int main( void ) {
173-
const float complex x[] = { 3.14f+1.5f*I, -3.14f-1.5f*I, 0.0f+0.0f*I, 0.0f/0.0f+0.0f/0.0f*I };
174-
175-
float complex v;
176-
float complex y;
183+
const stdlib_complex64_t x[] = {
184+
stdlib_complex64( 3.14f, 1.5f ),
185+
stdlib_complex64( -3.14f, -1.5f ),
186+
stdlib_complex64( 0.0f, 0.0f ),
187+
stdlib_complex64( 0.0f/0.0f, 0.0f/0.0f )
188+
};
189+
190+
stdlib_complex64_t v;
191+
stdlib_complex64_t y;
192+
float re1;
193+
float im1;
194+
float re2;
195+
float im2;
177196
int i;
178197
for ( i = 0; i < 4; i++ ) {
179198
v = x[ i ];
180199
y = stdlib_base_cflipsignf( v, -1.0f );
181-
printf( "cflipsignf(%f + %fi, %f) = %f + %fi\n", crealf( v ), cimagf( v ), -1.0f, crealf( y ), cimagf( y ) );
200+
stdlib_complex64_reim( v, &re1, &im1 );
201+
stdlib_complex64_reim( y, &re2, &im2 );
202+
printf( "cflipsignf(%f + %fi, %f) = %f + %fi\n", re1, im1, -1.0f, re2, im2 );
182203
}
183204
}
184205
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
28+
var real = require( '@stdlib/complex/float32/real' );
29+
var imag = require( '@stdlib/complex/float32/imag' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var cflipsignf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( cflipsignf instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var values;
46+
var y;
47+
var i;
48+
var v;
49+
50+
values = [
51+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
52+
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
53+
];
54+
v = uniform( 100, -500.0, 500.0, {
55+
'dtype': 'float32'
56+
});
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
y = cflipsignf( values[ i%values.length ], v );
61+
if ( isnanf( real( y ) ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnanf( imag( y ) ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/cflipsignf.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,28 +92,32 @@ static float rand_float( void ) {
9192
* @return elapsed time in seconds
9293
*/
9394
static double benchmark( void ) {
94-
float v[ 100 ];
95-
float complex x;
96-
float complex y;
9795
double elapsed;
96+
float v[ 100 ];
9897
double t;
98+
float re;
99+
float im;
99100
int i;
100101

102+
stdlib_complex64_t x[ 100 ];
103+
stdlib_complex64_t y;
104+
101105
for ( i = 0; i < 100; i++ ) {
102106
v[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
107+
x[ i ] = stdlib_complex64( v[ i ], v[ i ] );
103108
}
104109

105110
t = tic();
106111
for ( i = 0; i < ITERATIONS; i++ ) {
107-
x = v[ i%100 ] + v[ i%100 ]*I;
108-
y = stdlib_base_cflipsignf( x, -v[ i%100 ] );
109-
if ( crealf( y ) != crealf( y ) ) {
112+
y = stdlib_base_cflipsignf( x[ i%100 ], -v[ i%100 ] );
113+
stdlib_complex64_reim( y, &re, &im );
114+
if ( re != re ) {
110115
printf( "unexpected result\n" );
111116
break;
112117
}
113118
}
114119
elapsed = tic() - t;
115-
if ( cimagf( y ) != cimagf( y ) ) {
120+
if ( im != im ) {
116121
printf( "unexpected result\n" );
117122
}
118123
return elapsed;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2025 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+
}

0 commit comments

Comments
 (0)