Skip to content

Commit 8a22e75

Browse files
feat: adding initial files
1 parent e88f80d commit 8a22e75

File tree

24 files changed

+1889
-0
lines changed

24 files changed

+1889
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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 randu = require( '@stdlib/random/base/randu' );
66+
var signbit = require( '@stdlib/number/float16/base/signbit' );
67+
68+
var sign;
69+
var x;
70+
var i;
71+
72+
for ( i = 0; i < 100; i++ ) {
73+
x = (randu()*100.0) - 50.0;
74+
x = toFloat16( x );
75+
sign = signbit( x );
76+
sign = ( sign ) ? 'true' : 'false';
77+
console.log( 'x: %d. signbit: %s.', x, sign );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
#include "stdlib/number/float16/base/signbit.h"
109+
```
110+
111+
#### stdlib_base_float16_signbit( x )
112+
113+
Returns an integer indicating whether the sign bit for a half-precision floating-point number is on (`1`) or off (`0`).
114+
115+
```c
116+
#include "stdlib/number/float16/ctor.h"
117+
#include <stdint.h>
118+
119+
stdlib_float16_t x = stdlib_float16_from_bits( 51648 ); // => -11.5
120+
int8_t out = stdlib_base_float16_signbit( x );
121+
```
122+
123+
The function accepts the following arguments:
124+
125+
- **x**: `[in] stdlib_float16_t` input value.
126+
127+
```c
128+
int8_t stdlib_base_float16_signbit( const stdlib_float16_t x );
129+
```
130+
131+
</section>
132+
133+
<!-- /.usage -->
134+
135+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="notes">
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<!-- C API usage examples. -->
144+
145+
<section class="examples">
146+
147+
### Examples
148+
149+
```c
150+
#include "stdlib/number/float16/base/signbit.h"
151+
#include "stdlib/number/float16/ctor.h"
152+
#include "stdlib/number/float32/base/to_float16.h"
153+
#include <stdint.h>
154+
#include <stdio.h>
155+
#include <inttypes.h>
156+
157+
int main( void ) {
158+
const float x[] = { 3.14f, -3.14f, 0.0f, -0.0f, 4.0f, 1.0f, -1.0f, 1.0e38f, -1.0e38f };
159+
160+
stdlib_float16_t v;
161+
int8_t out;
162+
int i;
163+
for ( i = 0; i < 9; i++ ) {
164+
v = stdlib_base_float32_to_float16( x[ i ] );
165+
out = stdlib_base_float16_signbit( v );
166+
printf( "%f => signbit: %" PRId8 "\n", x[ i ], out );
167+
}
168+
}
169+
```
170+
171+
</section>
172+
173+
<!-- /.examples -->
174+
175+
</section>
176+
177+
<!-- /.c -->
178+
179+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
180+
181+
<section class="related">
182+
183+
</section>
184+
185+
<!-- /.related -->
186+
187+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
188+
189+
<section class="links">
190+
191+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
192+
193+
</section>
194+
195+
<!-- /.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)