@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.
package/src/main.c ADDED
@@ -0,0 +1,183 @@
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/special/fmod.h"
21
+ #include "stdlib/math/base/assert/is_nan.h"
22
+ #include "stdlib/math/base/assert/is_integer.h"
23
+ #include "stdlib/constants/float64/pinf.h"
24
+ #include "stdlib/constants/float64/ninf.h"
25
+ #include <stdint.h>
26
+
27
+ // 2^63 - 1
28
+ static const int64_t STDLIB_CONSTANT_INT64_MAX = 9223372036854775807;
29
+
30
+ /**
31
+ * Computes the greatest common divisor (gcd) using the binary GCD algorithm.
32
+ *
33
+ * @param a first number
34
+ * @param b second number
35
+ * @return greatest common divisor
36
+ *
37
+ * @example
38
+ * double out = largeIntegers( 1.2676506002282294.0e+30, 9007199254740992.0 );
39
+ * // returns 9007199254740992.0
40
+ */
41
+ static double largeIntegers( const double a, const double b ) {
42
+ double ac;
43
+ double bc;
44
+ double k;
45
+ double t;
46
+
47
+ // Simple cases:
48
+ if ( a == 0.0 ) {
49
+ return b;
50
+ }
51
+ if ( b == 0.0 ) {
52
+ return a;
53
+ }
54
+ ac = a;
55
+ bc = b;
56
+ k = 1.0;
57
+
58
+ // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...
59
+ while ( stdlib_base_fmod( ac, 2.0 ) == 0.0 && stdlib_base_fmod( bc, 2.0 ) == 0.0 ) {
60
+ ac /= 2.0; // right shift
61
+ bc /= 2.0; // right shift
62
+ k *= 2.0; // left shift
63
+ }
64
+ // Reduce `a` to an odd number...
65
+ while ( stdlib_base_fmod( ac, 2.0 ) == 0.0 ) {
66
+ ac /= 2.0; // right shift
67
+ }
68
+ // Henceforth, `a` is always odd...
69
+ while ( bc ) {
70
+ // Remove all factors of 2 in `b`, as they are not common...
71
+ while ( stdlib_base_fmod( bc, 2.0 ) == 0.0 ) {
72
+ bc /= 2.0; // right shift
73
+ }
74
+ // `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)...
75
+ if ( ac > bc ) {
76
+ t = bc;
77
+ bc = ac;
78
+ ac = t;
79
+ }
80
+ bc -= ac; // b=0 iff b=a
81
+ }
82
+ // Restore common factors of 2...
83
+ return k * ac;
84
+ }
85
+
86
+ /**
87
+ * Computes the greatest common divisor (gcd) using the binary GCD algorithm and bitwise operations.
88
+ *
89
+ * @param a first number
90
+ * @param b second number
91
+ * @return greatest common divisor
92
+ *
93
+ * @example
94
+ * double out = bitwise( 48.0, 18.0 );
95
+ * // returns 6.0
96
+ */
97
+ static double bitwise( const double a, const double b ) {
98
+ int64_t ac;
99
+ int64_t bc;
100
+ int64_t k;
101
+ int64_t t;
102
+
103
+ // Simple cases:
104
+ if ( a == 0.0 ) {
105
+ return b;
106
+ }
107
+ if ( b == 0.0 ) {
108
+ return a;
109
+ }
110
+ ac = (int64_t)a;
111
+ bc = (int64_t)b;
112
+ k = 0;
113
+
114
+ // Reduce `a` and/or `b` to odd numbers and keep track of the greatest power of 2 dividing both `a` and `b`...
115
+ while ( (ac & 1) == 0 && (bc & 1) == 0 ) {
116
+ ac >>= 1; // right shift
117
+ bc >>= 1; // right shift
118
+ k += 1;
119
+ }
120
+ // Reduce `a` to an odd number...
121
+ while ( (ac & 1) == 0 ) {
122
+ ac >>= 1; // right shift
123
+ }
124
+ // Henceforth, `a` is always odd...
125
+ while ( bc ) {
126
+ // Remove all factors of 2 in `b`, as they are not common...
127
+ while ( (bc & 1) == 0 ) {
128
+ bc >>= 1; // right shift
129
+ }
130
+ // `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)...
131
+ if ( ac > bc ) {
132
+ t = bc;
133
+ bc = ac;
134
+ ac = t;
135
+ }
136
+ bc -= ac; // b=0 iff b=a
137
+ }
138
+ // Restore common factors of 2...
139
+ return ac << k;
140
+ }
141
+
142
+ /**
143
+ * Computes the greatest common divisor (gcd).
144
+ *
145
+ * @param a first number
146
+ * @param b second number
147
+ * @return greatest common divisor
148
+ *
149
+ * @example
150
+ * double out = stdlib_base_gcd( 48.0, 18.0 );
151
+ * // returns 6.0
152
+ */
153
+ double stdlib_base_gcd( const double a, const double b ) {
154
+ double ac;
155
+ double bc;
156
+
157
+ if ( stdlib_base_is_nan( a ) || stdlib_base_is_nan( b ) ) {
158
+ return 0.0/0.0; // NaN
159
+ }
160
+ if (
161
+ a == STDLIB_CONSTANT_FLOAT64_PINF ||
162
+ b == STDLIB_CONSTANT_FLOAT64_PINF ||
163
+ a == STDLIB_CONSTANT_FLOAT64_NINF ||
164
+ b == STDLIB_CONSTANT_FLOAT64_NINF
165
+ ) {
166
+ return 0.0/0.0; // NaN
167
+ }
168
+ if ( !( stdlib_base_is_integer( a ) && stdlib_base_is_integer( b ) ) ) {
169
+ return 0.0/0.0; // NaN
170
+ }
171
+ ac = a;
172
+ bc = b;
173
+ if ( ac < 0 ) {
174
+ ac = -ac;
175
+ }
176
+ if ( bc < 0 ) {
177
+ bc = -bc;
178
+ }
179
+ if ( ac <= STDLIB_CONSTANT_INT64_MAX && bc <= STDLIB_CONSTANT_INT64_MAX ) {
180
+ return bitwise( ac, bc );
181
+ }
182
+ return largeIntegers( ac, bc );
183
+ }