[gcalctool] Use stdbool and stdint in MP functions, use 64 bit integers
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcalctool] Use stdbool and stdint in MP functions, use 64 bit integers
- Date: Mon, 1 Mar 2010 00:01:14 +0000 (UTC)
commit 0ebbcf1509582cf4560ed7853146569296850629
Author: Robert Ancell <robert ancell gmail com>
Date: Mon Mar 1 09:48:44 2010 +1100
Use stdbool and stdint in MP functions, use 64 bit integers
src/mp-binary.c | 2 +-
src/mp-convert.c | 89 ++++++++++++++++++++++++++++++++++++++---------------
src/mp-internal.h | 2 +-
src/mp.c | 59 ++++++++++++++++++-----------------
src/mp.h | 55 ++++++++++++++++++---------------
5 files changed, 126 insertions(+), 81 deletions(-)
---
diff --git a/src/mp-binary.c b/src/mp-binary.c
index b57ab46..ed9dfa2 100644
--- a/src/mp-binary.c
+++ b/src/mp-binary.c
@@ -84,7 +84,7 @@ static int mp_bitwise_xor(int v1, int v2) { return v1 ^ v2; }
static int mp_bitwise_not(int v1, int dummy) { return v1 ^ 0xF; }
-int
+bool
mp_is_overflow (const MPNumber *x, int wordlen)
{
MPNumber tmp1, tmp2;
diff --git a/src/mp-convert.c b/src/mp-convert.c
index 8584999..9a5b8c7 100644
--- a/src/mp-convert.c
+++ b/src/mp-convert.c
@@ -175,7 +175,7 @@ mp_set_from_double(double dx, MPNumber *z)
void
-mp_set_from_integer(int x, MPNumber *z)
+mp_set_from_integer(int64_t x, MPNumber *z)
{
memset(z, 0, sizeof(MPNumber));
@@ -202,24 +202,24 @@ mp_set_from_integer(int x, MPNumber *z)
void
-mp_set_from_fraction(int i, int j, MPNumber *z)
+mp_set_from_fraction(int64_t numerator, int64_t denominator, MPNumber *z)
{
- mp_gcd(&i, &j);
+ mp_gcd(&numerator, &denominator);
- if (j == 0) {
+ if (denominator == 0) {
mperr("*** J == 0 IN CALL TO MP_SET_FROM_FRACTION ***\n");
mp_set_from_integer(0, z);
return;
}
- if (j < 0) {
- i = -i;
- j = -j;
+ if (denominator < 0) {
+ numerator = -numerator;
+ denominator = -denominator;
}
- mp_set_from_integer(i, z);
- if (j != 1)
- mp_divide_integer(z, j, z);
+ mp_set_from_integer(numerator, z);
+ if (denominator != 1)
+ mp_divide_integer(z, denominator, z);
}
@@ -254,18 +254,17 @@ mp_set_from_random(MPNumber *z)
}
-int
+int64_t
mp_cast_to_int(const MPNumber *x)
{
- int i, j, x2, xs, ret_val = 0;
+ int i, j;
+ int64_t ret_val = 0;
/* RETURN 0 IF X = 0 OR IF NUMBER FRACTION */
- xs = x->sign;
- if (xs == 0 || x->exponent <= 0)
+ if (x->sign == 0 || x->exponent <= 0)
return 0;
- x2 = x->exponent;
- for (i = 0; i < x2; i++) {
+ for (i = 0; i < x->exponent; i++) {
int izs;
izs = ret_val;
ret_val = MP_BASE * ret_val;
@@ -277,11 +276,9 @@ mp_cast_to_int(const MPNumber *x)
return 0;
}
- /* CHECK THAT RESULT IS CORRECT (AN UNDETECTED OVERFLOW MAY
- * HAVE OCCURRED).
- */
+ /* CHECK THAT RESULT IS CORRECT (AN UNDETECTED OVERFLOW MAY HAVE OCCURRED). */
j = ret_val;
- for (i = x2 - 1; i >= 0; i--) {
+ for (i = x->exponent - 1; i >= 0; i--) {
int j1, kx;
j1 = j / MP_BASE;
@@ -296,7 +293,7 @@ mp_cast_to_int(const MPNumber *x)
return 0;
/* RESULT CORRECT SO RESTORE SIGN AND RETURN */
- return xs * ret_val;
+ return x->sign * ret_val;
/* Old comment about returning zero: */
/* HERE OVERFLOW OCCURRED (OR X WAS UNNORMALIZED), SO
@@ -305,6 +302,48 @@ mp_cast_to_int(const MPNumber *x)
}
+uint64_t
+mp_cast_to_unsigned_int(const MPNumber *x)
+{
+ int i, j;
+ uint64_t ret_val = 0;
+
+ /* RETURN 0 IF X <= 0 OR IF NUMBER FRACTION */
+ if (x->sign <= 0 || x->exponent <= 0)
+ return 0;
+
+ for (i = 0; i < x->exponent; i++) {
+ int izs;
+ izs = ret_val;
+ ret_val = MP_BASE * ret_val;
+ if (i < MP_T)
+ ret_val += x->fraction[i];
+
+ /* CHECK FOR SIGNS OF INTEGER OVERFLOW */
+ if (ret_val <= 0 || ret_val <= izs)
+ return 0;
+ }
+
+ /* CHECK THAT RESULT IS CORRECT (AN UNDETECTED OVERFLOW MAY HAVE OCCURRED). */
+ j = ret_val;
+ for (i = x->exponent - 1; i >= 0; i--) {
+ int j1, kx;
+
+ j1 = j / MP_BASE;
+ kx = 0;
+ if (i < MP_T)
+ kx = x->fraction[i];
+ if (kx != j - MP_BASE * j1)
+ return 0;
+ j = j1;
+ }
+ if (j != 0)
+ return 0;
+
+ return ret_val;
+}
+
+
static double
mppow_ri(float ap, int bp)
{
@@ -655,7 +694,7 @@ ends_with(const char *start, const char *end, const char *word)
}
-int
+bool
mp_set_from_string(const char *str, MPNumber *z)
{
int i, base, negate = 0, multiplier = 0, base_multiplier = 1;
@@ -703,7 +742,7 @@ mp_set_from_string(const char *str, MPNumber *z)
mp_set_from_integer(0, z);
while ((i = char_val((char **)&c, base)) >= 0) {
if (i > base)
- return 1;
+ return true;
mp_multiply_integer(z, base, z);
mp_add_integer(z, i, z);
}
@@ -742,7 +781,7 @@ mp_set_from_string(const char *str, MPNumber *z)
}
if (c != end) {
- return 1;
+ return true;
}
if (multiplier != 0) {
@@ -755,5 +794,5 @@ mp_set_from_string(const char *str, MPNumber *z)
if (negate == 1)
mp_invert_sign(z, z);
- return 0;
+ return false;
}
diff --git a/src/mp-internal.h b/src/mp-internal.h
index 9552630..a441a8b 100644
--- a/src/mp-internal.h
+++ b/src/mp-internal.h
@@ -39,7 +39,7 @@
#define MP_T 55
void mperr(const char *format, ...) __attribute__((format(printf, 1, 2)));
-void mp_gcd(int *, int *);
+void mp_gcd(int64_t *, int64_t *);
void mp_normalize(MPNumber *);
#endif /* MP_INTERNAL_H */
diff --git a/src/mp.c b/src/mp.c
index 67ecf1f..edb6591 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -382,7 +382,7 @@ mp_add(const MPNumber *x, const MPNumber *y, MPNumber *z)
void
-mp_add_integer(const MPNumber *x, int y, MPNumber *z)
+mp_add_integer(const MPNumber *x, int64_t y, MPNumber *z)
{
MPNumber t;
mp_set_from_integer(y, &t);
@@ -391,7 +391,7 @@ mp_add_integer(const MPNumber *x, int y, MPNumber *z)
void
-mp_add_fraction(const MPNumber *x, int i, int j, MPNumber *y)
+mp_add_fraction(const MPNumber *x, int64_t i, int64_t j, MPNumber *y)
{
MPNumber t;
mp_set_from_fraction(i, j, &t);
@@ -550,7 +550,7 @@ mp_divide(const MPNumber *x, const MPNumber *y, MPNumber *z)
void
-mp_divide_integer(const MPNumber *x, int y, MPNumber *z)
+mp_divide_integer(const MPNumber *x, int64_t y, MPNumber *z)
{
int i__1;
int c, i, k, b2, c2, i2, j1, j2, r1;
@@ -722,13 +722,13 @@ L210:
}
-int
+bool
mp_is_integer(const MPNumber *x)
{
MPNumber t1, t2, t3;
if (mp_is_complex(x))
- return 0;
+ return false;
/* This fix is required for 1/3 repiprocal not being detected as an integer */
/* Multiplication and division by 10000 is used to get around a
@@ -746,49 +746,50 @@ mp_is_integer(const MPNumber *x)
// Zero is an integer
if (mp_is_zero(x))
- return 1;
+ return true;
// Fractional
if (x->exponent <= 0)
- return 0;
+ return false;
// Look for fractional components
for (i = x->exponent; i < MP_SIZE; i++) {
if (x->fraction[i] != 0)
- return 0;
+ return false;
}
- return 1;*/
+ return true;*/
}
-int
+bool
mp_is_positive_integer(const MPNumber *x)
{
if (mp_is_complex(x))
- return 0;
+ return false;
else
return x->sign >= 0 && mp_is_integer(x);
}
-int
+bool
mp_is_natural(const MPNumber *x)
{
if (mp_is_complex(x))
- return 0;
+ return false;
else
return x->sign > 0 && mp_is_integer(x);
}
-int mp_is_complex(const MPNumber *x)
+bool
+mp_is_complex(const MPNumber *x)
{
- return 0;//x->im_sign != 0;
+ return false;//x->im_sign != 0;
}
-int
+bool
mp_is_equal(const MPNumber *x, const MPNumber *y)
{
return mp_compare_mp_to_mp(x, y) == 0;
@@ -986,9 +987,9 @@ mp_epowy(const MPNumber *x, MPNumber *z)
* SAVE INPUT PARAMETERS IN LOCAL VARIABLES
*/
void
-mp_gcd(int *k, int *l)
+mp_gcd(int64_t *k, int64_t *l)
{
- int i, j;
+ int64_t i, j;
i = abs(*k);
j = abs(*l);
@@ -1018,35 +1019,35 @@ mp_gcd(int *k, int *l)
}
-int
+bool
mp_is_zero(const MPNumber *x)
{
return x->sign == 0; // && x->im_sign == 0;
}
-int
+bool
mp_is_negative(const MPNumber *x)
{
return x->sign < 0;
}
-int
+bool
mp_is_greater_equal(const MPNumber *x, const MPNumber *y)
{
return mp_compare_mp_to_mp(x, y) >= 0;
}
-int
+bool
mp_is_greater_than(const MPNumber *x, const MPNumber *y)
{
return mp_compare_mp_to_mp(x, y) > 0;
}
-int
+bool
mp_is_less_equal(const MPNumber *x, const MPNumber *y)
{
return mp_compare_mp_to_mp(x, y) <= 0;
@@ -1195,7 +1196,7 @@ mp_ln(const MPNumber *x, MPNumber *z)
void
-mp_logarithm(int n, const MPNumber *x, MPNumber *z)
+mp_logarithm(int64_t n, const MPNumber *x, MPNumber *z)
{
MPNumber t1, t2;
@@ -1215,7 +1216,7 @@ mp_logarithm(int n, const MPNumber *x, MPNumber *z)
}
-int
+bool
mp_is_less_than(const MPNumber *x, const MPNumber *y)
{
return mp_compare_mp_to_mp(x, y) < 0;
@@ -1351,7 +1352,7 @@ mp_multiply(const MPNumber *x, const MPNumber *y, MPNumber *z)
void
-mp_multiply_integer(const MPNumber *x, int y, MPNumber *z)
+mp_multiply_integer(const MPNumber *x, int64_t y, MPNumber *z)
{
int c, i, c1, c2, j1, j2;
int t1, t3, t4, ij, ri = 0, is, ix;
@@ -1480,7 +1481,7 @@ mp_multiply_integer(const MPNumber *x, int y, MPNumber *z)
void
-mp_multiply_fraction(const MPNumber *x, int numerator, int denominator, MPNumber *z)
+mp_multiply_fraction(const MPNumber *x, int64_t numerator, int64_t denominator, MPNumber *z)
{
if (denominator == 0) {
mperr(_("Division by zero is undefined"));
@@ -1651,7 +1652,7 @@ mp_reciprocal(const MPNumber *x, MPNumber *z)
void
-mp_root(const MPNumber *x, int n, MPNumber *z)
+mp_root(const MPNumber *x, int64_t n, MPNumber *z)
{
float approximation;
int ex, np, it0, t;
@@ -1840,7 +1841,7 @@ mp_xpowy(const MPNumber *x, const MPNumber *y, MPNumber *z)
void
-mp_xpowy_integer(const MPNumber *x, int n, MPNumber *z)
+mp_xpowy_integer(const MPNumber *x, int64_t n, MPNumber *z)
{
int i;
MPNumber t;
diff --git a/src/mp.h b/src/mp.h
index 3ebcc80..52592f3 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -39,6 +39,8 @@
#ifndef MP_H
#define MP_H
+#include <stdbool.h>
+#include <stdint.h>
#include <glib.h>
/* Size of the multiple precision values */
@@ -85,37 +87,37 @@ void mp_clear_error();
int mp_compare_mp_to_mp(const MPNumber *x, const MPNumber *y);
/* Return true if the value is x == 0 */
-int mp_is_zero(const MPNumber *x);
+bool mp_is_zero(const MPNumber *x);
/* Return true if x < 0 */
-int mp_is_negative(const MPNumber *x);
+bool mp_is_negative(const MPNumber *x);
/* Return true if x is integer */
-int mp_is_integer(const MPNumber *x);
+bool mp_is_integer(const MPNumber *x);
/* Return true if x is a positive integer */
-int mp_is_positive_integer(const MPNumber *x);
+bool mp_is_positive_integer(const MPNumber *x);
/* Return true if x is a natural number (an integer â?¥ 0) */
-int mp_is_natural(const MPNumber *x);
+bool mp_is_natural(const MPNumber *x);
/* Return true if x has an imaginary component */
-int mp_is_complex(const MPNumber *x);
+bool mp_is_complex(const MPNumber *x);
/* Return true if x == y */
-int mp_is_equal(const MPNumber *x, const MPNumber *y);
+bool mp_is_equal(const MPNumber *x, const MPNumber *y);
/* Return true if x â?¥ y */
-int mp_is_greater_equal(const MPNumber *x, const MPNumber *y);
+bool mp_is_greater_equal(const MPNumber *x, const MPNumber *y);
/* Return true if x > y */
-int mp_is_greater_than(const MPNumber *x, const MPNumber *y);
+bool mp_is_greater_than(const MPNumber *x, const MPNumber *y);
/* Return true if x â?¤ y */
-int mp_is_less_equal(const MPNumber *x, const MPNumber *y);
+bool mp_is_less_equal(const MPNumber *x, const MPNumber *y);
/* Return true if x < y */
-int mp_is_less_than(const MPNumber *x, const MPNumber *y);
+bool mp_is_less_than(const MPNumber *x, const MPNumber *y);
/* Sets z = |x| */
void mp_abs(const MPNumber *x, MPNumber *z);
@@ -139,10 +141,10 @@ void mp_invert_sign(const MPNumber *x, MPNumber *z);
void mp_add(const MPNumber *x, const MPNumber *y, MPNumber *z);
/* Sets z = x + y */
-void mp_add_integer(const MPNumber *x, int y, MPNumber *z);
+void mp_add_integer(const MPNumber *x, int64_t y, MPNumber *z);
/* Sets z = x + numerator ÷ denominator */
-void mp_add_fraction(const MPNumber *x, int numerator, int denominator, MPNumber *z);
+void mp_add_fraction(const MPNumber *x, int64_t numerator, int64_t denominator, MPNumber *z);
/* Sets z = x â?? y */
void mp_subtract(const MPNumber *x, const MPNumber *y, MPNumber *z);
@@ -151,16 +153,16 @@ void mp_subtract(const MPNumber *x, const MPNumber *y, MPNumber *z);
void mp_multiply(const MPNumber *x, const MPNumber *y, MPNumber *z);
/* Sets z = x Ã? y */
-void mp_multiply_integer(const MPNumber *x, int y, MPNumber *z);
+void mp_multiply_integer(const MPNumber *x, int64_t y, MPNumber *z);
/* Sets z = x � numerator ÷ denominator */
-void mp_multiply_fraction(const MPNumber *x, int numerator, int denominator, MPNumber *z);
+void mp_multiply_fraction(const MPNumber *x, int64_t numerator, int64_t denominator, MPNumber *z);
/* Sets z = x ÷ y */
void mp_divide(const MPNumber *x, const MPNumber *y, MPNumber *z);
/* Sets z = x ÷ y */
-void mp_divide_integer(const MPNumber *x, int y, MPNumber *z);
+void mp_divide_integer(const MPNumber *x, int64_t y, MPNumber *z);
/* Sets z = 1 ÷ x */
void mp_reciprocal(const MPNumber *, MPNumber *);
@@ -175,7 +177,7 @@ void mp_integer_component(const MPNumber *x, MPNumber *z);
void mp_ln(const MPNumber *x, MPNumber *z);
/* Sets z = log_n x */
-void mp_logarithm(int n, const MPNumber *x, MPNumber *z);
+void mp_logarithm(int64_t n, const MPNumber *x, MPNumber *z);
/* Sets z = Ï? */
void mp_get_pi(MPNumber *z);
@@ -187,7 +189,7 @@ void mp_get_eulers(MPNumber *z);
void mp_get_i(MPNumber *z);
/* Sets z = nâ??x */
-void mp_root(const MPNumber *x, int n, MPNumber *z);
+void mp_root(const MPNumber *x, int64_t n, MPNumber *z);
/* Sets z = â??x */
void mp_sqrt(const MPNumber *x, MPNumber *z);
@@ -202,7 +204,7 @@ void mp_modulus_divide(const MPNumber *x, const MPNumber *y, MPNumber *z);
void mp_xpowy(const MPNumber *x, const MPNumber *y, MPNumber *z);
/* Sets z = x^y */
-void mp_xpowy_integer(const MPNumber *x, int y, MPNumber *z);
+void mp_xpowy_integer(const MPNumber *x, int64_t y, MPNumber *z);
/* Sets z = e^x */
void mp_epowy(const MPNumber *x, MPNumber *z);
@@ -220,10 +222,10 @@ void mp_set_from_float(float x, MPNumber *z);
void mp_set_from_double(double x, MPNumber *z);
/* Sets z = x */
-void mp_set_from_integer(int x, MPNumber *z);
+void mp_set_from_integer(int64_t x, MPNumber *z);
/* Sets z = numerator ÷ denominator */
-void mp_set_from_fraction(int numerator, int denominator, MPNumber *z);
+void mp_set_from_fraction(int64_t numerator, int64_t denominator, MPNumber *z);
/* Sets z = r(cos theta + i sin theta) */
void mp_set_from_polar(const MPNumber *r, MPAngleUnit unit, const MPNumber *theta, MPNumber *z);
@@ -235,9 +237,9 @@ void mp_set_from_complex(const MPNumber *x, const MPNumber *y, MPNumber *z);
void mp_set_from_random(MPNumber *z);
/* Sets z from a string representation in 'text'.
- * Returns 0 on success.
+ * Returns true on success.
*/
-int mp_set_from_string(const char *text, MPNumber *z);
+bool mp_set_from_string(const char *text, MPNumber *z);
/* Returns x as a native single-precision floating point number */
float mp_cast_to_float(const MPNumber *x);
@@ -246,7 +248,10 @@ float mp_cast_to_float(const MPNumber *x);
double mp_cast_to_double(const MPNumber *x);
/* Returns x as a native integer */
-int mp_cast_to_int(const MPNumber *x);
+int64_t mp_cast_to_int(const MPNumber *x);
+
+/* Returns x as a native unsigned integer */
+uint64_t mp_cast_to_unsigned_int(const MPNumber *x);
/* Converts x to a string representation.
* The string is written into 'buffer' which is guaranteed to be at least 'buffer_length' octets in size.
@@ -294,7 +299,7 @@ void mp_acosh(const MPNumber *x, MPNumber *z);
void mp_atanh(const MPNumber *x, MPNumber *z);
/* Returns true if x is cannot be represented in a binary word of length 'wordlen' */
-int mp_is_overflow(const MPNumber *x, int wordlen);
+bool mp_is_overflow(const MPNumber *x, int wordlen);
/* Sets z = boolean AND for each bit in x and z */
void mp_and(const MPNumber *x, const MPNumber *y, MPNumber *z);
[Date Prev][
Date Next] [Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]