Skip to content

Commit cf0cdff

Browse files
Neerajpathak07kgrytestdlib-botgururaj1512
authored
feat: add number/float16/base/signbit
PR-URL: #9390 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io> Co-authored-by: Gururaj Gurram <gururajgurram1512@gmail.com> Reviewed-by: Gururaj Gurram <gururajgurram1512@gmail.com>
1 parent 6d021ff commit cf0cdff

24 files changed

Lines changed: 1884 additions & 0 deletions

File tree

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# signbit
22+
23+
> Return a boolean indicating if the sign bit for a [half-precision floating-point number][ieee754] is on (true) or off (false).
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var signbit = require( '@stdlib/number/float16/base/signbit' );
31+
```
32+
33+
#### signbit( x )
34+
35+
Returns a boolean indicating if the sign bit for a [half-precision floating-point number][ieee754] is on (`true`) or off (`false`).
36+
37+
```javascript
38+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
39+
40+
var bool = signbit( toFloat16( 4.0 ) );
41+
// returns false
42+
43+
bool = signbit( toFloat16( -5.960464477539063e-8 ) );
44+
// returns true
45+
46+
bool = signbit( 0.0 );
47+
// returns false
48+
49+
bool = signbit( -0.0 );
50+
// returns true
51+
```
52+
53+
</section>
54+
55+
<!-- /.usage -->
56+
57+
<section class="examples">
58+
59+
## Examples
60+
61+
<!-- eslint no-undef: "error" -->
62+
63+
```javascript
64+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
65+
var uniform = require( '@stdlib/random/array/uniform' );
66+
var map = require( '@stdlib/array/base/map' );
67+
var logEachMap = require( '@stdlib/console/log-each-map' );
68+
var signbit = require( '@stdlib/number/float16/base/signbit' );
69+
70+
// Create an array of random half-precision floating-point numbers:
71+
var x = map( uniform( 100, -50.0, 50.0 ), toFloat16 );
72+
73+
// Determine whether the sign bit is on or off for each number:
74+
logEachMap( 'x: %0.4f => %s', x, signbit );
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- C interface documentation. -->
82+
83+
* * *
84+
85+
<section class="c">
86+
87+
## C APIs
88+
89+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
90+
91+
<section class="intro">
92+
93+
</section>
94+
95+
<!-- /.intro -->
96+
97+
<!-- C usage documentation. -->
98+
99+
<section class="usage">
100+
101+
### Usage
102+
103+
```c
104+
#include "stdlib/number/float16/base/signbit.h"
105+
```
106+
107+
#### stdlib_base_float16_signbit( x )
108+
109+
Returns an integer indicating whether the sign bit for a half-precision floating-point number is on (`1`) or off (`0`).
110+
111+
```c
112+
#include "stdlib/number/float16/ctor.h"
113+
#include <stdint.h>
114+
115+
stdlib_float16_t x = stdlib_float16_from_bits( 51648 ); // => -11.5
116+
int8_t out = stdlib_base_float16_signbit( x );
117+
```
118+
119+
The function accepts the following arguments:
120+
121+
- **x**: `[in] stdlib_float16_t` input value.
122+
123+
```c
124+
int8_t stdlib_base_float16_signbit( const stdlib_float16_t 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/number/float16/base/signbit.h"
147+
#include "stdlib/number/float16/ctor.h"
148+
#include "stdlib/number/float32/base/to_float16.h"
149+
#include <stdint.h>
150+
#include <stdio.h>
151+
#include <inttypes.h>
152+
153+
int main( void ) {
154+
const float x[] = { 3.14f, -3.14f, 0.0f, -0.0f, 4.0f, 1.0f, -1.0f, 1.0e38f, -1.0e38f };
155+
156+
stdlib_float16_t v;
157+
int8_t out;
158+
int i;
159+
for ( i = 0; i < 9; i++ ) {
160+
v = stdlib_base_float32_to_float16( x[ i ] );
161+
out = stdlib_base_float16_signbit( v );
162+
printf( "%f => signbit: %" PRId8 "\n", x[ i ], out );
163+
}
164+
}
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
</section>
172+
173+
<!-- /.c -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
</section>
180+
181+
<!-- /.related -->
182+
183+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="links">
186+
187+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
188+
189+
</section>
190+
191+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
27+
var map = require( '@stdlib/array/base/map' );
28+
var naryFunction = require( '@stdlib/utils/nary-function' );
29+
var pkg = require( './../package.json' ).name;
30+
var signbit = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var x;
37+
var y;
38+
var i;
39+
40+
x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
y = signbit( x[ i%x.length ] );
45+
if ( typeof y !== 'boolean' ) {
46+
b.fail( 'should return a boolean' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isBoolean( y ) ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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/array/uniform' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
28+
var map = require( '@stdlib/array/base/map' );
29+
var naryFunction = require( '@stdlib/utils/nary-function' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var signbit = tryRequire( resolve( __dirname, './../lib/native.js' ) );
37+
var opts = {
38+
'skip': ( signbit instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::native', opts, function benchmark( b ) {
45+
var x;
46+
var y;
47+
var i;
48+
49+
x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) );
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
y = signbit( x[ i%x.length ] );
54+
if ( typeof y !== 'boolean' ) {
55+
b.fail( 'should return a boolean' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isBoolean( y ) ) {
60+
b.fail( 'should return a boolean' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});

0 commit comments

Comments
 (0)