Skip to content

Commit 7975dcf

Browse files
gururaj1512kgryte
andauthored
feat: add constants/float16/sign-mask
PR-URL: #8743 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 358d5c7 commit 7975dcf

File tree

10 files changed

+507
-0
lines changed

10 files changed

+507
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
# FLOAT16_SIGN_MASK
22+
23+
> Mask for the sign bit of a [half-precision floating-point number][ieee754].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var FLOAT16_SIGN_MASK = require( '@stdlib/constants/float16/sign-mask' );
33+
```
34+
35+
#### FLOAT16_SIGN_MASK
36+
37+
Mask for the sign bit of a [half-precision floating-point number][ieee754].
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
// 0x8000 = 32768 => 1 00000 0000000000
43+
var bool = ( FLOAT16_SIGN_MASK === 0x8000 );
44+
// returns true
45+
```
46+
47+
</section>
48+
49+
<!-- /.usage -->
50+
51+
<section class="notes">
52+
53+
</section>
54+
55+
<!-- /.notes -->
56+
57+
<section class="examples">
58+
59+
## Examples
60+
61+
<!-- eslint-disable id-length -->
62+
63+
<!-- eslint no-undef: "error" -->
64+
65+
```javascript
66+
var toWord = require( '@stdlib/number/float16/base/to-word' );
67+
var FLOAT16_SIGN_MASK = require( '@stdlib/constants/float16/sign-mask' );
68+
69+
var x = -11.5;
70+
var w = toWord( x ); // 1 10010 0111000000
71+
// returns 51648
72+
73+
// Mask off all bits except for the sign bit:
74+
var out = (w & FLOAT16_SIGN_MASK)>>>0; // 1 00000 0000000000
75+
// returns 32768
76+
77+
// Turn off the sign bit and leave other bits unchanged:
78+
out = w & (~FLOAT16_SIGN_MASK); // 0 10010 0111000000
79+
// returns 18880
80+
```
81+
82+
</section>
83+
84+
<!-- /.examples -->
85+
86+
<!-- C interface documentation. -->
87+
88+
* * *
89+
90+
<section class="c">
91+
92+
## C APIs
93+
94+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
95+
96+
<section class="intro">
97+
98+
</section>
99+
100+
<!-- /.intro -->
101+
102+
<!-- C usage documentation. -->
103+
104+
<section class="usage">
105+
106+
### Usage
107+
108+
```c
109+
#include "stdlib/constants/float16/sign_mask.h"
110+
```
111+
112+
#### STDLIB_CONSTANT_FLOAT16_SIGN_MASK
113+
114+
Macro for the mask for the sign bit of a [half-precision floating-point number][ieee754].
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
121+
122+
<section class="notes">
123+
124+
</section>
125+
126+
<!-- /.notes -->
127+
128+
<!-- C API usage examples. -->
129+
130+
<section class="examples">
131+
132+
</section>
133+
134+
<!-- /.examples -->
135+
136+
</section>
137+
138+
<!-- /.c -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
</section>
145+
146+
<!-- /.related -->
147+
148+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149+
150+
<section class="links">
151+
152+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
153+
154+
<!-- <related-links> -->
155+
156+
<!-- </related-links> -->
157+
158+
</section>
159+
160+
<!-- /.links -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
{{alias}}
3+
Mask for the sign bit of a half-precision floating-point number.
4+
5+
Examples
6+
--------
7+
> {{alias}}
8+
32768
9+
> {{alias:@stdlib/number/uint16/base/to-binary-string}}( {{alias}} )
10+
'1000000000000000'
11+
12+
See Also
13+
--------
14+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Mask for the sign bit of a half-precision floating-point number.
23+
*
24+
* @example
25+
* var mask = FLOAT16_SIGN_MASK;
26+
* // returns 32768
27+
*/
28+
declare const FLOAT16_SIGN_MASK: number;
29+
30+
31+
// EXPORTS //
32+
33+
export = FLOAT16_SIGN_MASK;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import FLOAT16_SIGN_MASK = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The export is a number...
25+
{
26+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
27+
FLOAT16_SIGN_MASK; // $ExpectType number
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
var toWord = require( '@stdlib/number/float16/base/to-word' );
22+
var toBinaryString = require( '@stdlib/number/uint16/base/to-binary-string' );
23+
var FLOAT16_SIGN_MASK = require( './../lib' );
24+
25+
var x = -11.5;
26+
var w = toWord( x );
27+
console.log( 'Word: %s', toBinaryString( w ) );
28+
29+
var out = (w & FLOAT16_SIGN_MASK)>>>0;
30+
console.log( 'Isolate sign bits: %s', toBinaryString( out ) );
31+
32+
out = w & (~FLOAT16_SIGN_MASK);
33+
console.log( 'Turn off sign bits: %s', toBinaryString( out ) );
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#ifndef STDLIB_CONSTANTS_FLOAT16_SIGN_MASK_H
20+
#define STDLIB_CONSTANTS_FLOAT16_SIGN_MASK_H
21+
22+
/**
23+
* Macro for the mask for the sign bit of a half-precision floating-point number.
24+
*/
25+
#define STDLIB_CONSTANT_FLOAT16_SIGN_MASK 0x8000
26+
27+
#endif // !STDLIB_CONSTANTS_FLOAT16_SIGN_MASK_H
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
/**
22+
* Mask for the sign bit of a half-precision floating-point number.
23+
*
24+
* @module @stdlib/constants/float16/sign-mask
25+
* @type {uinteger16}
26+
*
27+
* @example
28+
* var FLOAT16_SIGN_MASK = require( '@stdlib/constants/float16/sign-mask' );
29+
* // returns 32768
30+
*/
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Mask for the sign bit of a half-precision floating-point number.
37+
*
38+
* ## Notes
39+
*
40+
* The mask for the sign bit of a half-precision floating-point number is an unsigned 16-bit integer with the value \\( 32768 \\), which corresponds to the bit sequence
41+
*
42+
* ```binarystring
43+
* 1 00000 0000000000
44+
* ```
45+
*
46+
* @constant
47+
* @type {uinteger16}
48+
* @default 0x8000
49+
* @see [IEEE 754]{@link https://en.wikipedia.org/wiki/IEEE_754-1985}
50+
*/
51+
var FLOAT16_SIGN_MASK = 0x8000>>>0;
52+
53+
54+
// EXPORTS //
55+
56+
module.exports = FLOAT16_SIGN_MASK;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"options": {},
3+
"fields": [
4+
{
5+
"field": "src",
6+
"resolve": true,
7+
"relative": true
8+
},
9+
{
10+
"field": "include",
11+
"resolve": true,
12+
"relative": true
13+
},
14+
{
15+
"field": "libraries",
16+
"resolve": false,
17+
"relative": false
18+
},
19+
{
20+
"field": "libpath",
21+
"resolve": true,
22+
"relative": false
23+
}
24+
],
25+
"confs": [
26+
{
27+
"src": [],
28+
"include": [
29+
"./include"
30+
],
31+
"libraries": [],
32+
"libpath": [],
33+
"dependencies": []
34+
}
35+
]
36+
}

0 commit comments

Comments
 (0)