@stdlib/math-base-assert-uint32-is-pow2 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -2
- package/include/stdlib/math/base/assert/uint32_is_pow2.h +41 -0
- package/lib/native.js +55 -0
- package/manifest.json +38 -0
- package/package.json +9 -23
- package/src/addon.c +88 -0
- package/src/main.c +38 -0
package/README.md
CHANGED
|
@@ -95,6 +95,98 @@ for ( i = 0; i < 100; i++ ) {
|
|
|
95
95
|
|
|
96
96
|
<!-- /.examples -->
|
|
97
97
|
|
|
98
|
+
<!-- C interface documentation. -->
|
|
99
|
+
|
|
100
|
+
* * *
|
|
101
|
+
|
|
102
|
+
<section class="c">
|
|
103
|
+
|
|
104
|
+
## C APIs
|
|
105
|
+
|
|
106
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
107
|
+
|
|
108
|
+
<section class="intro">
|
|
109
|
+
|
|
110
|
+
</section>
|
|
111
|
+
|
|
112
|
+
<!-- /.intro -->
|
|
113
|
+
|
|
114
|
+
<!-- C usage documentation. -->
|
|
115
|
+
|
|
116
|
+
<section class="usage">
|
|
117
|
+
|
|
118
|
+
### Usage
|
|
119
|
+
|
|
120
|
+
```c
|
|
121
|
+
#include "stdlib/math/base/assert/uint32_is_pow2.h"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### stdlib_base_uint32_is_pow2( x )
|
|
125
|
+
|
|
126
|
+
Tests whether an unsigned integer is a power of 2.
|
|
127
|
+
|
|
128
|
+
```c
|
|
129
|
+
#include <stdbool.h>
|
|
130
|
+
|
|
131
|
+
bool out = stdlib_base_uint32_is_pow2( 5 );
|
|
132
|
+
// returns false
|
|
133
|
+
|
|
134
|
+
out = stdlib_base_uint32_is_pow2( 2 );
|
|
135
|
+
// returns true
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The function accepts the following arguments:
|
|
139
|
+
|
|
140
|
+
- **x**: `[in] uint32_t` input value.
|
|
141
|
+
|
|
142
|
+
```c
|
|
143
|
+
bool stdlib_base_uint32_is_pow2( const uint32_t x );
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
</section>
|
|
147
|
+
|
|
148
|
+
<!-- /.usage -->
|
|
149
|
+
|
|
150
|
+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
151
|
+
|
|
152
|
+
<section class="notes">
|
|
153
|
+
|
|
154
|
+
</section>
|
|
155
|
+
|
|
156
|
+
<!-- /.notes -->
|
|
157
|
+
|
|
158
|
+
<!-- C API usage examples. -->
|
|
159
|
+
|
|
160
|
+
<section class="examples">
|
|
161
|
+
|
|
162
|
+
### Examples
|
|
163
|
+
|
|
164
|
+
```c
|
|
165
|
+
#include "stdlib/math/base/assert/uint32_is_pow2.h"
|
|
166
|
+
#include <stdio.h>
|
|
167
|
+
#include <stdbool.h>
|
|
168
|
+
#include <stdint.h>
|
|
169
|
+
|
|
170
|
+
int main( void ) {
|
|
171
|
+
const uint32_t x[] = { 0, 1, 2, 3, 4, 5, 8, 10, 16, 1024 };
|
|
172
|
+
bool b;
|
|
173
|
+
int i;
|
|
174
|
+
|
|
175
|
+
for ( i = 0; i < 9; i++ ) {
|
|
176
|
+
b = stdlib_base_uint32_is_pow2( x[ i ] );
|
|
177
|
+
printf( "Value: %u. is a power of 2? %s.\n", x[ i ], ( b ) ? "True" : "False" );
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
</section>s
|
|
183
|
+
|
|
184
|
+
<!-- /.examples -->
|
|
185
|
+
|
|
186
|
+
</section>
|
|
187
|
+
|
|
188
|
+
<!-- /.c -->
|
|
189
|
+
|
|
98
190
|
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
99
191
|
|
|
100
192
|
<section class="related">
|
|
@@ -142,8 +234,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
142
234
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/math-base-assert-uint32-is-pow2.svg
|
|
143
235
|
[npm-url]: https://npmjs.org/package/@stdlib/math-base-assert-uint32-is-pow2
|
|
144
236
|
|
|
145
|
-
[test-image]: https://github.com/stdlib-js/math-base-assert-uint32-is-pow2/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
146
|
-
[test-url]: https://github.com/stdlib-js/math-base-assert-uint32-is-pow2/actions/workflows/test.yml?query=branch:v0.
|
|
237
|
+
[test-image]: https://github.com/stdlib-js/math-base-assert-uint32-is-pow2/actions/workflows/test.yml/badge.svg?branch=v0.3.0
|
|
238
|
+
[test-url]: https://github.com/stdlib-js/math-base-assert-uint32-is-pow2/actions/workflows/test.yml?query=branch:v0.3.0
|
|
147
239
|
|
|
148
240
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/math-base-assert-uint32-is-pow2/main.svg
|
|
149
241
|
[coverage-url]: https://codecov.io/github/stdlib-js/math-base-assert-uint32-is-pow2?branch=main
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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_MATH_BASE_ASSERT_UINT32_IS_POW2_H
|
|
20
|
+
#define STDLIB_MATH_BASE_ASSERT_UINT32_IS_POW2_H
|
|
21
|
+
|
|
22
|
+
#include <stdbool.h>
|
|
23
|
+
#include <stdint.h>
|
|
24
|
+
|
|
25
|
+
/*
|
|
26
|
+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
|
|
27
|
+
*/
|
|
28
|
+
#ifdef __cplusplus
|
|
29
|
+
extern "C" {
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Tests whether an unsigned integer is a power of 2.
|
|
34
|
+
*/
|
|
35
|
+
bool stdlib_base_uint32_is_pow2( const uint32_t x );
|
|
36
|
+
|
|
37
|
+
#ifdef __cplusplus
|
|
38
|
+
}
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
#endif // !STDLIB_MATH_BASE_ASSERT_UINT32_IS_POW2_H
|
package/lib/native.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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 Boolean = require( '@stdlib/boolean-ctor' );
|
|
24
|
+
var addon = require( './../src/addon.node' );
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// MAIN //
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Tests whether an unsigned integer is a power of 2.
|
|
31
|
+
*
|
|
32
|
+
* @private
|
|
33
|
+
* @param {number} x - value to test
|
|
34
|
+
* @returns {boolean} boolean indicating whether the number is a power of 2
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* var bool = isPow2Uint32( 5 );
|
|
38
|
+
* // returns false
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var bool = isPow2Uint32( 2 );
|
|
42
|
+
* // returns true
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* var bool = isPow2Uint32( 0 );
|
|
46
|
+
* // returns false
|
|
47
|
+
*/
|
|
48
|
+
function isPow2Uint32( x ) {
|
|
49
|
+
return Boolean( addon( x ) );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
// EXPORTS //
|
|
54
|
+
|
|
55
|
+
module.exports = isPow2Uint32;
|
package/manifest.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
"./src/main.c"
|
|
29
|
+
],
|
|
30
|
+
"include": [
|
|
31
|
+
"./include"
|
|
32
|
+
],
|
|
33
|
+
"libraries": [],
|
|
34
|
+
"libpath": [],
|
|
35
|
+
"dependencies": []
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/math-base-assert-uint32-is-pow2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Test whether an unsigned integer is a power of 2.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -14,20 +14,16 @@
|
|
|
14
14
|
}
|
|
15
15
|
],
|
|
16
16
|
"main": "./lib",
|
|
17
|
+
"gypfile": false,
|
|
17
18
|
"directories": {
|
|
18
|
-
"benchmark": "./benchmark",
|
|
19
19
|
"doc": "./docs",
|
|
20
|
-
"
|
|
20
|
+
"include": "./include",
|
|
21
21
|
"lib": "./lib",
|
|
22
|
-
"
|
|
22
|
+
"src": "./src",
|
|
23
|
+
"dist": "./dist"
|
|
23
24
|
},
|
|
24
25
|
"types": "./docs/types",
|
|
25
|
-
"scripts": {
|
|
26
|
-
"test": "make test",
|
|
27
|
-
"test-cov": "make test-cov",
|
|
28
|
-
"examples": "make examples",
|
|
29
|
-
"benchmark": "make benchmark"
|
|
30
|
-
},
|
|
26
|
+
"scripts": {},
|
|
31
27
|
"homepage": "https://stdlib.io",
|
|
32
28
|
"repository": {
|
|
33
29
|
"type": "git",
|
|
@@ -36,20 +32,10 @@
|
|
|
36
32
|
"bugs": {
|
|
37
33
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
38
34
|
},
|
|
39
|
-
"dependencies": {
|
|
40
|
-
|
|
41
|
-
"@stdlib/assert-is-boolean": "^0.1.1",
|
|
42
|
-
"@stdlib/math-base-assert-is-integer": "^0.1.1",
|
|
43
|
-
"@stdlib/math-base-special-log2": "^0.1.1",
|
|
44
|
-
"@stdlib/math-base-special-round": "^0.1.1",
|
|
45
|
-
"@stdlib/number-float64-base-to-uint32": "^0.1.1",
|
|
46
|
-
"@stdlib/random-base-randu": "^0.1.0",
|
|
47
|
-
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
|
|
48
|
-
"istanbul": "^0.4.1",
|
|
49
|
-
"tap-min": "git+https://github.com/Planeshifter/tap-min.git",
|
|
50
|
-
"@stdlib/bench-harness": "^0.1.2",
|
|
51
|
-
"@stdlib/bench": "^0.3.1"
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@stdlib/utils-library-manifest": "^0.2.1"
|
|
52
37
|
},
|
|
38
|
+
"devDependencies": {},
|
|
53
39
|
"engines": {
|
|
54
40
|
"node": ">=0.10.0",
|
|
55
41
|
"npm": ">2.7.0"
|
package/src/addon.c
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
#include "stdlib/math/base/assert/uint32_is_pow2.h"
|
|
20
|
+
#include <node_api.h>
|
|
21
|
+
#include <stdint.h>
|
|
22
|
+
#include <assert.h>
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Receives JavaScript callback invocation data.
|
|
26
|
+
*
|
|
27
|
+
* @param env environment under which the function is invoked
|
|
28
|
+
* @param info callback data
|
|
29
|
+
* @return Node-API value
|
|
30
|
+
*/
|
|
31
|
+
static napi_value addon( napi_env env, napi_callback_info info ) {
|
|
32
|
+
napi_status status;
|
|
33
|
+
|
|
34
|
+
// Get callback arguments:
|
|
35
|
+
size_t argc = 1;
|
|
36
|
+
napi_value argv[ 1 ];
|
|
37
|
+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
|
|
38
|
+
assert( status == napi_ok );
|
|
39
|
+
|
|
40
|
+
// Check whether we were provided the correct number of arguments:
|
|
41
|
+
if ( argc < 1 ) {
|
|
42
|
+
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
|
|
43
|
+
assert( status == napi_ok );
|
|
44
|
+
return NULL;
|
|
45
|
+
}
|
|
46
|
+
if ( argc > 1 ) {
|
|
47
|
+
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
|
|
48
|
+
assert( status == napi_ok );
|
|
49
|
+
return NULL;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
napi_valuetype vtype0;
|
|
53
|
+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
|
|
54
|
+
assert( status == napi_ok );
|
|
55
|
+
if ( vtype0 != napi_number ) {
|
|
56
|
+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
|
|
57
|
+
assert( status == napi_ok );
|
|
58
|
+
return NULL;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
uint32_t x;
|
|
62
|
+
status = napi_get_value_uint32( env, argv[ 0 ], &x );
|
|
63
|
+
assert( status == napi_ok );
|
|
64
|
+
|
|
65
|
+
bool result = stdlib_base_uint32_is_pow2( x );
|
|
66
|
+
|
|
67
|
+
napi_value v;
|
|
68
|
+
status = napi_create_int32( env, (int32_t)result, &v );
|
|
69
|
+
assert( status == napi_ok );
|
|
70
|
+
|
|
71
|
+
return v;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Initializes a Node-API module.
|
|
76
|
+
*
|
|
77
|
+
* @param env environment under which the function is invoked
|
|
78
|
+
* @param exports exports object
|
|
79
|
+
* @return main export
|
|
80
|
+
*/
|
|
81
|
+
static napi_value init( napi_env env, napi_value exports ) {
|
|
82
|
+
napi_value fcn;
|
|
83
|
+
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
|
|
84
|
+
assert( status == napi_ok );
|
|
85
|
+
return fcn;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2024 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
|
+
#include "stdlib/math/base/assert/uint32_is_pow2.h"
|
|
20
|
+
#include <stdint.h>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Tests whether an unsigned integer is a power of 2.
|
|
24
|
+
*
|
|
25
|
+
* @param x input value
|
|
26
|
+
* @return output value
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* #include <stdbool.h>
|
|
30
|
+
*
|
|
31
|
+
* bool out = stdlib_base_uint32_is_pow2( 3 );
|
|
32
|
+
* // returns false
|
|
33
|
+
*/
|
|
34
|
+
bool stdlib_base_uint32_is_pow2( const uint32_t x ) {
|
|
35
|
+
uint32_t xc;
|
|
36
|
+
xc = x >> 0;
|
|
37
|
+
return ( !!xc && !( xc & ( xc - 1 ) ) );
|
|
38
|
+
}
|