@ohos-ports/stdlib-math-base-special-gcd 0.0.0-stage → 0.3.2-beta.1

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.
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ * Computes the greatest common divisor (gcd) using the binary GCD algorithm.
23
+ *
24
+ * ## References
25
+ *
26
+ * - Stein, Josef. 1967. "Computational problems associated with Racah algebra." _Journal of Computational Physics_ 1 (3): 397–405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].
27
+ *
28
+ * [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2
29
+ *
30
+ * @private
31
+ * @param {integer} a - first number
32
+ * @param {integer} b - second number
33
+ * @returns {integer} greatest common divisor
34
+ *
35
+ * @example
36
+ * var v = gcd( 1.2676506002282294e+30, 9007199254740992 );
37
+ * // returns 9007199254740992
38
+ */
39
+ function gcd( a, b ) {
40
+ var k = 1;
41
+ var t;
42
+
43
+ // Simple cases:
44
+ if ( a === 0 ) {
45
+ return b;
46
+ }
47
+ if ( b === 0 ) {
48
+ return a;
49
+ }
50
+ // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...
51
+ while ( a%2 === 0 && b%2 === 0 ) {
52
+ a /= 2; // right shift
53
+ b /= 2; // right shift
54
+ k *= 2; // left shift
55
+ }
56
+ // Reduce `a` to an odd number...
57
+ while ( a%2 === 0 ) {
58
+ a /= 2; // right shift
59
+ }
60
+ // Henceforth, `a` is always odd...
61
+ while ( b ) {
62
+ // Remove all factors of 2 in `b`, as they are not common...
63
+ while ( b%2 === 0 ) {
64
+ b /= 2; // right shift
65
+ }
66
+ // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...
67
+ if ( a > b ) {
68
+ t = b;
69
+ b = a;
70
+ a = t;
71
+ }
72
+ b -= a; // b=0 iff b=a
73
+ }
74
+ // Restore common factors of 2...
75
+ return k * a;
76
+ }
77
+
78
+
79
+ // EXPORTS //
80
+
81
+ module.exports = gcd;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ * Computes the greatest common divisor (gcd) using the binary GCD algorithm and bitwise operations.
23
+ *
24
+ * ## References
25
+ *
26
+ * - Stein, Josef. 1967. "Computational problems associated with Racah algebra." _Journal of Computational Physics_ 1 (3): 397–405. doi:[10.1016/0021-9991(67)90047-2][@stein:1967].
27
+ *
28
+ * [@stein:1967]: https://doi.org/10.1016/0021-9991(67)90047-2
29
+ *
30
+ * @private
31
+ * @param {integer32} a - first number
32
+ * @param {integer32} b - second number
33
+ * @returns {integer32} greatest common divisor
34
+ *
35
+ * @example
36
+ * var v = gcd( 48, 18 );
37
+ * // returns 6
38
+ */
39
+ function gcd( a, b ) {
40
+ var k = 0;
41
+ var t;
42
+
43
+ // Simple cases:
44
+ if ( a === 0 ) {
45
+ return b;
46
+ }
47
+ if ( b === 0 ) {
48
+ return a;
49
+ }
50
+ // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...
51
+ while ( (a & 1) === 0 && (b & 1) === 0 ) {
52
+ a >>>= 1; // right shift
53
+ b >>>= 1; // right shift
54
+ k += 1;
55
+ }
56
+ // Reduce `a` to an odd number...
57
+ while ( (a & 1) === 0 ) {
58
+ a >>>= 1; // right shift
59
+ }
60
+ // Henceforth, `a` is always odd...
61
+ while ( b ) {
62
+ // Remove all factors of 2 in `b`, as they are not common...
63
+ while ( (b & 1) === 0 ) {
64
+ b >>>= 1; // right shift
65
+ }
66
+ // `a` and `b` are both odd. Swap values such that `b` is the larger of the two values, and then set `b` to the difference (which is even)...
67
+ if ( a > b ) {
68
+ t = b;
69
+ b = a;
70
+ a = t;
71
+ }
72
+ b -= a; // b=0 iff b=a
73
+ }
74
+ // Restore common factors of 2...
75
+ return a << k;
76
+ }
77
+
78
+
79
+ // EXPORTS //
80
+
81
+ module.exports = gcd;
package/lib/index.js ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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
+ * Compute the greatest common divisor (gcd).
23
+ *
24
+ * @module @stdlib/math-base-special-gcd
25
+ *
26
+ * @example
27
+ * var gcd = require( '@stdlib/math-base-special-gcd' );
28
+ *
29
+ * var v = gcd( 48, 18 );
30
+ * // returns 6
31
+ */
32
+
33
+ // MODULES //
34
+
35
+ var main = require( './main.js' );
36
+
37
+
38
+ // EXPORTS //
39
+
40
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 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 isnan = require( '@stdlib/math-base-assert-is-nan' );
24
+ var isInteger = require( '@stdlib/math-base-assert-is-integer' );
25
+ var PINF = require( '@stdlib/constants-float64-pinf' );
26
+ var NINF = require( '@stdlib/constants-float64-ninf' );
27
+ var INT32_MAX = require( '@stdlib/constants-int32-max' );
28
+ var bitwise = require( './bitwise_binary_gcd.js' );
29
+ var largeIntegers = require( './binary_gcd.js' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Computes the greatest common divisor (gcd).
36
+ *
37
+ * @param {integer} a - first number
38
+ * @param {integer} b - second number
39
+ * @returns {integer} greatest common divisor
40
+ *
41
+ * @example
42
+ * var v = gcd( 48, 18 );
43
+ * // returns 6
44
+ *
45
+ * @example
46
+ * var v = gcd( 3.14, 18 );
47
+ * // returns NaN
48
+ *
49
+ * @example
50
+ * var v = gcd( NaN, 18 );
51
+ * // returns NaN
52
+ */
53
+ function gcd( a, b ) {
54
+ if ( isnan( a ) || isnan( b ) ) {
55
+ return NaN;
56
+ }
57
+ if (
58
+ a === PINF ||
59
+ b === PINF ||
60
+ a === NINF ||
61
+ b === NINF
62
+ ) {
63
+ return NaN;
64
+ }
65
+ if ( !( isInteger( a ) && isInteger( b ) ) ) {
66
+ return NaN;
67
+ }
68
+ if ( a < 0 ) {
69
+ a = -a;
70
+ }
71
+ if ( b < 0 ) {
72
+ b = -b;
73
+ }
74
+ if ( a <= INT32_MAX && b <= INT32_MAX ) {
75
+ return bitwise( a, b );
76
+ }
77
+ return largeIntegers( a, b );
78
+ }
79
+
80
+
81
+ // EXPORTS //
82
+
83
+ module.exports = gcd;
package/lib/native.js ADDED
@@ -0,0 +1,51 @@
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 addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Computes the greatest common divisor (gcd).
30
+ *
31
+ * @private
32
+ * @param {number} a - first number
33
+ * @param {number} b - second number
34
+ * @returns {number} greatest common divisor
35
+ *
36
+ * @example
37
+ * var v = gcd( 0.0, 0.0 );
38
+ * // returns 0.0
39
+ *
40
+ * @example
41
+ * var v = gcd( 48.0, 18.0 );
42
+ * // returns 6.0
43
+ */
44
+ function gcd( a, b ) {
45
+ return addon( a, b );
46
+ }
47
+
48
+
49
+ // EXPORTS //
50
+
51
+ module.exports = gcd;
package/manifest.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "options": {
3
+ "task": "build"
4
+ },
5
+ "fields": [
6
+ {
7
+ "field": "src",
8
+ "resolve": true,
9
+ "relative": true
10
+ },
11
+ {
12
+ "field": "include",
13
+ "resolve": true,
14
+ "relative": true
15
+ },
16
+ {
17
+ "field": "libraries",
18
+ "resolve": false,
19
+ "relative": false
20
+ },
21
+ {
22
+ "field": "libpath",
23
+ "resolve": true,
24
+ "relative": false
25
+ }
26
+ ],
27
+ "confs": [
28
+ {
29
+ "task": "build",
30
+ "src": [
31
+ "./src/main.c"
32
+ ],
33
+ "include": [
34
+ "./include"
35
+ ],
36
+ "libraries": [],
37
+ "libpath": [],
38
+ "dependencies": [
39
+ "@stdlib/math-base-napi-binary",
40
+ "@stdlib/math-base-assert-is-nan",
41
+ "@stdlib/math-base-special-fmod",
42
+ "@stdlib/math-base-assert-is-integer",
43
+ "@stdlib/constants-float64-pinf",
44
+ "@stdlib/constants-float64-ninf"
45
+ ]
46
+ },
47
+ {
48
+ "task": "benchmark",
49
+ "src": [
50
+ "./src/main.c"
51
+ ],
52
+ "include": [
53
+ "./include"
54
+ ],
55
+ "libraries": [],
56
+ "libpath": [],
57
+ "dependencies": [
58
+ "@stdlib/math-base-assert-is-nan",
59
+ "@stdlib/math-base-special-fmod",
60
+ "@stdlib/math-base-assert-is-integer",
61
+ "@stdlib/constants-float64-pinf",
62
+ "@stdlib/constants-float64-ninf"
63
+ ]
64
+ },
65
+ {
66
+ "task": "examples",
67
+ "src": [
68
+ "./src/main.c"
69
+ ],
70
+ "include": [
71
+ "./include"
72
+ ],
73
+ "libraries": [],
74
+ "libpath": [],
75
+ "dependencies": [
76
+ "@stdlib/math-base-assert-is-nan",
77
+ "@stdlib/math-base-special-fmod",
78
+ "@stdlib/math-base-assert-is-integer",
79
+ "@stdlib/constants-float64-pinf",
80
+ "@stdlib/constants-float64-ninf"
81
+ ]
82
+ }
83
+ ]
84
+ }
package/package.json CHANGED
@@ -1,6 +1,88 @@
1
1
  {
2
2
  "name": "@ohos-ports/stdlib-math-base-special-gcd",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.2-beta.1",
4
+ "description": "Compute the greatest common divisor (gcd).",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "gypfile": false,
18
+ "directories": {
19
+ "doc": "./docs",
20
+ "include": "./include",
21
+ "lib": "./lib",
22
+ "src": "./src",
23
+ "dist": "./dist"
24
+ },
25
+ "types": "./docs/types",
26
+ "scripts": {},
27
+ "homepage": "https://stdlib.io",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
31
+ "directory": "ports/stdlib-math-base-special-gcd/0.3.2"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
35
+ },
36
+ "dependencies": {
37
+ "@stdlib/constants-float64-ninf": "^0.2.3",
38
+ "@stdlib/constants-float64-pinf": "^0.2.3",
39
+ "@stdlib/constants-int32-max": "^0.3.1",
40
+ "@stdlib/math-base-assert-is-integer": "^0.2.7",
41
+ "@stdlib/math-base-assert-is-nan": "^0.2.3",
42
+ "@stdlib/math-base-napi-binary": "^0.3.3",
43
+ "@stdlib/math-base-special-fmod": "^0.1.1",
44
+ "@stdlib/utils-library-manifest": "^0.2.3"
45
+ },
46
+ "devDependencies": {},
47
+ "engines": {
48
+ "node": ">=0.10.0",
49
+ "npm": ">2.7.0"
50
+ },
51
+ "os": [
52
+ "aix",
53
+ "darwin",
54
+ "freebsd",
55
+ "linux",
56
+ "macos",
57
+ "openbsd",
58
+ "sunos",
59
+ "win32",
60
+ "windows",
61
+ "openharmony"
62
+ ],
63
+ "keywords": [
64
+ "stdlib",
65
+ "stdmath",
66
+ "math",
67
+ "mathematics",
68
+ "euclid",
69
+ "euclidean",
70
+ "stein",
71
+ "binary gcd",
72
+ "greatest common divisor",
73
+ "greatest common factor",
74
+ "highest common factor",
75
+ "greatest common measure",
76
+ "highest common divisor",
77
+ "gcd",
78
+ "gcf",
79
+ "hcf",
80
+ "gcm",
81
+ "arithmetic",
82
+ "integer"
83
+ ],
84
+ "funding": {
85
+ "type": "opencollective",
86
+ "url": "https://opencollective.com/stdlib"
87
+ }
88
+ }
package/src/addon.c ADDED
@@ -0,0 +1,22 @@
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/special/gcd.h"
20
+ #include "stdlib/math/base/napi/binary.h"
21
+
22
+ STDLIB_MATH_BASE_NAPI_MODULE_DD_D( stdlib_base_gcd )