gmp 0.4.3-x86-mingw32 → 0.4.7-x86-mingw32

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.
data/ext/gmpq.c CHANGED
@@ -159,17 +159,18 @@ VALUE r_gmpq_to_s(VALUE self)
159
159
  int sizeinbase;
160
160
  int offset;
161
161
 
162
- Data_Get_Struct(self, MP_RAT, self_val);
162
+ //Data_Get_Struct (self, MP_RAT, self_val);
163
+ mpq_get_struct(self, self_val)
163
164
 
164
- if (mpz_cmp_ui(mpq_denref(self_val), 1) == 0) {
165
- str = mpz_get_str(NULL, 10, mpq_numref (self_val));
166
- res = rb_str_new2(str);
165
+ if (mpz_cmp_ui (mpq_denref (self_val), 1) == 0) {
166
+ str = mpz_get_str (NULL, 10, mpq_numref (self_val));
167
+ res = rb_str_new2 (str);
167
168
  free (str);
168
169
  return res;
169
170
  }
170
171
 
171
- self_val_num = mpq_numref(self_val);
172
- self_val_den = mpq_denref(self_val);
172
+ self_val_num = mpq_numref (self_val);
173
+ self_val_den = mpq_denref (self_val);
173
174
 
174
175
  sizeinbase = mpz_sizeinbase (self_val_num, 10) + mpz_sizeinbase (self_val_den, 10) + 3;
175
176
  str = malloc (sizeinbase);
data/ext/gmpz.c CHANGED
@@ -57,17 +57,30 @@
57
57
  * remove r_gmpz_remove mpz_remove
58
58
  * fac r_gmpz_fac mpz_fac_ui
59
59
  * fib r_gmpz_fib mpz_fib_ui
60
+ * <=>
61
+ * <
62
+ * <=
63
+ * ==
64
+ * >=
65
+ * >
66
+ * cmpabs
67
+ * sgn
68
+ * eql? r_gmpz_eql ------------
69
+ * hash r_gmpz_hash
70
+ * and
71
+ * ior
72
+ * xor
60
73
  * com r_gmpz_com mpz_com
61
74
  * com! r_gmpz_com_self mpz_com
62
- * []= r_gmpz_setbit mpz_setbit
63
- * [] r_gmpz_getbit mpz_tstbit
75
+ * popcount r_gmpz_popcount
64
76
  * scan0 r_gmpz_scan0 mpz_scan0
65
77
  * scan1 r_gmpz_scan1 mpz_scan1
78
+ * []= r_gmpz_setbit mpz_setbit
79
+ * [] r_gmpz_getbit mpz_tstbit
66
80
  * even? r_gmpz_is_even mpz_even
67
81
  * odd? r_gmpz_is_odd mpz_odd
68
82
  * sizeinbase r_gmpz_sizeinbase mpz_sizeinbase
69
83
  * size_in_bin r_gmpz_size_in_bin mpz_sizeinbits
70
- * ...
71
84
  */
72
85
 
73
86
  /**********************************************************************
@@ -233,7 +246,6 @@ VALUE r_gmpzsg_new(int argc, VALUE *argv, VALUE klass)
233
246
  {
234
247
  MP_INT *res_val;
235
248
  VALUE res;
236
-
237
249
  (void)klass;
238
250
 
239
251
  if (argc > 1)
@@ -1467,6 +1479,88 @@ VALUE r_gmpz_eq(VALUE self, VALUE arg)
1467
1479
  }
1468
1480
  }
1469
1481
 
1482
+ int mpz_cmp_value(MP_INT *OP, VALUE arg)
1483
+ {
1484
+ MP_RAT *arg_val_q;
1485
+ MP_INT *arg_val_z;
1486
+ int res;
1487
+
1488
+ if (GMPZ_P(arg)) {
1489
+ mpz_get_struct(arg,arg_val_z);
1490
+ return mpz_cmp(OP,arg_val_z);
1491
+ } else if (FIXNUM_P(arg)) {
1492
+ return mpz_cmp_si(OP, FIX2INT(arg));
1493
+ } else if (GMPQ_P(arg)) {
1494
+ mpq_get_struct(arg,arg_val_q);
1495
+ mpz_temp_alloc(arg_val_z);
1496
+ mpz_init(arg_val_z);
1497
+ mpz_mul(arg_val_z, OP, mpq_denref(arg_val_q));
1498
+ res = mpz_cmp(arg_val_z, mpq_numref(arg_val_q));
1499
+ mpz_temp_free(arg_val_z);
1500
+ return res;
1501
+ } else if (GMPF_P(arg)) {
1502
+ not_yet;
1503
+ } else if (BIGNUM_P(arg)) {
1504
+ mpz_temp_from_bignum(arg_val_z, arg);
1505
+ res = mpz_cmp(OP, arg_val_z);
1506
+ mpz_temp_free(arg_val_z);
1507
+ return res;
1508
+ } else {
1509
+ typeerror_as(ZQFXB, "exponent");
1510
+ }
1511
+ }
1512
+
1513
+ VALUE r_gmpz_cmp(VALUE self, VALUE arg)
1514
+ {
1515
+ MP_INT *self_val;
1516
+ int res;
1517
+ mpz_get_struct(self,self_val);
1518
+ res = mpz_cmp_value(self_val, arg);
1519
+ if (res > 0)
1520
+ return INT2FIX(1);
1521
+ else if (res == 0)
1522
+ return INT2FIX(0);
1523
+ else
1524
+ return INT2FIX(-1);
1525
+ }
1526
+
1527
+ /*
1528
+ * Document-method: <
1529
+ *
1530
+ * call-seq:
1531
+ * int1 < int2
1532
+ *
1533
+ * Returns whether +int1+ is strictly less than +int2+.
1534
+ */
1535
+ DEFUN_INT_CMP(lt,<)
1536
+ /*
1537
+ * Document-method: <=
1538
+ *
1539
+ * call-seq:
1540
+ * int1 <= int2
1541
+ *
1542
+ * Returns whether +int1+ is less than or equal to +int2+.
1543
+ */
1544
+ DEFUN_INT_CMP(le,<=)
1545
+ /*
1546
+ * Document-method: >
1547
+ *
1548
+ * call-seq:
1549
+ * int1 > int2
1550
+ *
1551
+ * Returns whether +int1+ is strictly greater than +int2+.
1552
+ */
1553
+ DEFUN_INT_CMP(gt,>)
1554
+ /*
1555
+ * Document-method: >=
1556
+ *
1557
+ * call-seq:
1558
+ * int1 >= int2
1559
+ *
1560
+ * Returns whether +int1+ is greater than or equal to +int2+.
1561
+ */
1562
+ DEFUN_INT_CMP(ge,>=)
1563
+
1470
1564
  VALUE r_gmpz_cmpabs(VALUE self, VALUE arg)
1471
1565
  {
1472
1566
  MP_INT *arg_val_z, *self_val;
@@ -1503,6 +1597,47 @@ VALUE r_gmpz_cmpabs(VALUE self, VALUE arg)
1503
1597
  }
1504
1598
  }
1505
1599
 
1600
+ /*
1601
+ * call-seq:
1602
+ * integer.sgn
1603
+ *
1604
+ * From the GMP Manual:
1605
+ *
1606
+ * Returns +1 if +integer+ > 0, 0 if +integer+ == 0, and -1 if +integer+ < 0.
1607
+ */
1608
+ VALUE r_gmpz_sgn(VALUE self)
1609
+ {
1610
+ MP_INT *self_val;
1611
+ mpz_get_struct(self, self_val);
1612
+ return INT2FIX(mpz_sgn(self_val));
1613
+ }
1614
+
1615
+
1616
+ /*
1617
+ * eql? and hash are defined so that GMP::Z objects can be used as keys in
1618
+ * hashes.
1619
+ */
1620
+ VALUE r_gmpz_eql(VALUE self, VALUE arg)
1621
+ {
1622
+ MP_INT *self_val, *arg_val;
1623
+ mpz_get_struct(self,self_val);
1624
+
1625
+ if (GMPZ_P(arg)) {
1626
+ mpz_get_struct(arg, arg_val);
1627
+ return (mpz_cmp (self_val, arg_val) == 0) ? Qtrue : Qfalse;
1628
+ }
1629
+ else {
1630
+ return Qfalse;
1631
+ }
1632
+ }
1633
+
1634
+ VALUE r_gmpz_hash(VALUE self)
1635
+ {
1636
+ ID to_s_sym = rb_intern("to_s");
1637
+ ID hash_sym = rb_intern("hash");
1638
+ return rb_funcall(rb_funcall(self, to_s_sym, 0), hash_sym, 0);
1639
+ }
1640
+
1506
1641
 
1507
1642
  /**********************************************************************
1508
1643
  * Integer Logic and Bit Fiddling *
@@ -1542,6 +1677,48 @@ DEFUN_INT_LOGIC(or, mpz_ior)
1542
1677
  */
1543
1678
  DEFUN_INT_LOGIC(xor, mpz_xor)
1544
1679
 
1680
+ /*
1681
+ * Document-method: com
1682
+ *
1683
+ * call-seq:
1684
+ * integer.com
1685
+ *
1686
+ * From the GMP Manual:
1687
+ *
1688
+ * Returns the one's complement of +integer+.
1689
+ */
1690
+ /*
1691
+ * Document-method: com!
1692
+ *
1693
+ * call-seq:
1694
+ * integer.com!
1695
+ *
1696
+ * From the GMP Manual:
1697
+ *
1698
+ * Sets +integer+ to its one's complement.
1699
+ */
1700
+ DEFUN_INT2INT(com, mpz_com)
1701
+
1702
+ /*
1703
+ * Document-method: popcount
1704
+ *
1705
+ * call-seq:
1706
+ * integer.popcount
1707
+ *
1708
+ * From the GMP Manual:
1709
+ *
1710
+ * If <tt>integer>=0</tt>, return the population count of <tt>integer</tt>,
1711
+ * which is the number of 1 bits in the binary representation. If
1712
+ * <tt>op<0</tt>, the number of 1s is infinite, and the return value is
1713
+ * INT2FIX(ULONG_MAX), the largest possible unsigned long.
1714
+ */
1715
+ VALUE r_gmpz_popcount(VALUE self)
1716
+ {
1717
+ MP_INT *self_val;
1718
+ mpz_get_struct(self, self_val);
1719
+ return INT2FIX(mpz_popcount(self_val));
1720
+ }
1721
+
1545
1722
  /*
1546
1723
  * call-seq:
1547
1724
  * integer.scan0(starting_bit)
@@ -1601,26 +1778,6 @@ VALUE r_gmpz_scan1(VALUE self, VALUE bitnr)
1601
1778
  return INT2FIX(mpz_scan1(self_val, bitnr_val));
1602
1779
  }
1603
1780
 
1604
- /*
1605
- * Document-method: popcount
1606
- *
1607
- * call-seq:
1608
- * integer.popcount
1609
- *
1610
- * From the GMP Manual:
1611
- *
1612
- * If <tt>integer>=0</tt>, return the population count of <tt>integer</tt>,
1613
- * which is the number of 1 bits in the binary representation. If
1614
- * <tt>op<0</tt>, the number of 1s is infinite, and the return value is
1615
- * INT2FIX(ULONG_MAX), the largest possible unsigned long.
1616
- */
1617
- VALUE r_gmpz_popcount(VALUE self)
1618
- {
1619
- MP_INT *self_val;
1620
- mpz_get_struct(self, self_val);
1621
- return INT2FIX(mpz_popcount(self_val));
1622
- }
1623
-
1624
1781
  /*
1625
1782
  * call-seq:
1626
1783
  * integer[index] = x &rarr; nil
@@ -1671,54 +1828,6 @@ VALUE r_gmpz_getbit(VALUE self, VALUE bitnr)
1671
1828
  * Miscellaneous Integer Functions *
1672
1829
  **********************************************************************/
1673
1830
 
1674
- VALUE r_gmpz_sizeinbase(VALUE self, VALUE base)
1675
- {
1676
- MP_INT *self_val;
1677
- int base_val;
1678
- mpz_get_struct (self, self_val);
1679
- base_val = FIX2INT (base);
1680
- return INT2FIX (mpz_sizeinbase (self_val, base_val));
1681
- }
1682
-
1683
- VALUE r_gmpz_size_in_bin(VALUE self)
1684
- {
1685
- MP_INT *self_val;
1686
- mpz_get_struct (self, self_val);
1687
- return INT2FIX (mpz_sizeinbase (self_val, 2));
1688
- }
1689
-
1690
-
1691
- /**********************************************************************
1692
- * Integer Special Functions *
1693
- **********************************************************************/
1694
-
1695
-
1696
- /**********************************************************************
1697
- * _unsorted_ *
1698
- **********************************************************************/
1699
-
1700
- /*
1701
- * Document-method: com
1702
- *
1703
- * call-seq:
1704
- * integer.com
1705
- *
1706
- * From the GMP Manual:
1707
- *
1708
- * Returns the one's complement of +integer+.
1709
- */
1710
- /*
1711
- * Document-method: com!
1712
- *
1713
- * call-seq:
1714
- * integer.com!
1715
- *
1716
- * From the GMP Manual:
1717
- *
1718
- * Sets +integer+ to its one's complement.
1719
- */
1720
- DEFUN_INT2INT(com, mpz_com)
1721
-
1722
1831
  /*
1723
1832
  * Document-method: even?
1724
1833
  *
@@ -1742,107 +1851,36 @@ DEFUN_INT_COND_P(is_even,mpz_even_p)
1742
1851
  */
1743
1852
  DEFUN_INT_COND_P(is_odd,mpz_odd_p)
1744
1853
 
1745
- /*
1746
- * call-seq:
1747
- * integer.sgn
1748
- *
1749
- * From the GMP Manual:
1750
- *
1751
- * Returns +1 if +integer+ > 0, 0 if +integer+ == 0, and -1 if +integer+ < 0.
1752
- */
1753
- VALUE r_gmpz_sgn(VALUE self)
1854
+ VALUE r_gmpz_sizeinbase(VALUE self, VALUE base)
1754
1855
  {
1755
1856
  MP_INT *self_val;
1756
- mpz_get_struct(self, self_val);
1757
- return INT2FIX(mpz_sgn(self_val));
1758
- }
1759
-
1760
- DEFUN_INT_F_UL(fshr,mpz_fdiv_q_2exp,"shift size")
1761
- DEFUN_INT_F_UL(tshr,mpz_tdiv_q_2exp,"shift size")
1762
- DEFUN_INT_F_UL(fshrm,mpz_fdiv_r_2exp,"mark size")
1763
- DEFUN_INT_F_UL(tshrm,mpz_tdiv_r_2exp,"mark size")
1764
-
1765
- int mpz_cmp_value(MP_INT *OP, VALUE arg)
1766
- {
1767
- MP_RAT *arg_val_q;
1768
- MP_INT *arg_val_z;
1769
- int res;
1770
-
1771
- if (GMPZ_P(arg)) {
1772
- mpz_get_struct(arg,arg_val_z);
1773
- return mpz_cmp(OP,arg_val_z);
1774
- } else if (FIXNUM_P(arg)) {
1775
- return mpz_cmp_si(OP, FIX2INT(arg));
1776
- } else if (GMPQ_P(arg)) {
1777
- mpq_get_struct(arg,arg_val_q);
1778
- mpz_temp_alloc(arg_val_z);
1779
- mpz_init(arg_val_z);
1780
- mpz_mul(arg_val_z, OP, mpq_denref(arg_val_q));
1781
- res = mpz_cmp(arg_val_z, mpq_numref(arg_val_q));
1782
- mpz_temp_free(arg_val_z);
1783
- return res;
1784
- } else if (GMPF_P(arg)) {
1785
- not_yet;
1786
- } else if (BIGNUM_P(arg)) {
1787
- mpz_temp_from_bignum(arg_val_z, arg);
1788
- res = mpz_cmp(OP, arg_val_z);
1789
- mpz_temp_free(arg_val_z);
1790
- return res;
1791
- } else {
1792
- typeerror_as(ZQFXB, "exponent");
1793
- }
1857
+ int base_val;
1858
+ mpz_get_struct (self, self_val);
1859
+ base_val = FIX2INT (base);
1860
+ return INT2FIX (mpz_sizeinbase (self_val, base_val));
1794
1861
  }
1795
1862
 
1796
- VALUE r_gmpz_cmp(VALUE self, VALUE arg)
1863
+ VALUE r_gmpz_size_in_bin(VALUE self)
1797
1864
  {
1798
1865
  MP_INT *self_val;
1799
- int res;
1800
- mpz_get_struct(self,self_val);
1801
- res = mpz_cmp_value(self_val, arg);
1802
- if (res > 0)
1803
- return INT2FIX(1);
1804
- else if (res == 0)
1805
- return INT2FIX(0);
1806
- else
1807
- return INT2FIX(-1);
1866
+ mpz_get_struct (self, self_val);
1867
+ return INT2FIX (mpz_sizeinbase (self_val, 2));
1808
1868
  }
1809
1869
 
1810
- /*
1811
- * Document-method: <
1812
- *
1813
- * call-seq:
1814
- * int1 < int2
1815
- *
1816
- * Returns whether +int1+ is strictly less than +int2+.
1817
- */
1818
- DEFUN_INT_CMP(lt,<)
1819
- /*
1820
- * Document-method: <=
1821
- *
1822
- * call-seq:
1823
- * int1 <= int2
1824
- *
1825
- * Returns whether +int1+ is less than or equal to +int2+.
1826
- */
1827
- DEFUN_INT_CMP(le,<=)
1828
- /*
1829
- * Document-method: >
1830
- *
1831
- * call-seq:
1832
- * int1 > int2
1833
- *
1834
- * Returns whether +int1+ is strictly greater than +int2+.
1835
- */
1836
- DEFUN_INT_CMP(gt,>)
1837
- /*
1838
- * Document-method: >=
1839
- *
1840
- * call-seq:
1841
- * int1 >= int2
1842
- *
1843
- * Returns whether +int1+ is greater than or equal to +int2+.
1844
- */
1845
- DEFUN_INT_CMP(ge,>=)
1870
+
1871
+ /**********************************************************************
1872
+ * Integer Special Functions *
1873
+ **********************************************************************/
1874
+
1875
+
1876
+ /**********************************************************************
1877
+ * _unsorted_ *
1878
+ **********************************************************************/
1879
+
1880
+ DEFUN_INT_F_UL(fshr,mpz_fdiv_q_2exp,"shift size")
1881
+ DEFUN_INT_F_UL(tshr,mpz_tdiv_q_2exp,"shift size")
1882
+ DEFUN_INT_F_UL(fshrm,mpz_fdiv_r_2exp,"mark size")
1883
+ DEFUN_INT_F_UL(tshrm,mpz_tdiv_r_2exp,"mark size")
1846
1884
 
1847
1885
  VALUE r_gmpzsg_pow(VALUE klass, VALUE base, VALUE exp)
1848
1886
  {
@@ -1931,38 +1969,41 @@ void init_gmpz()
1931
1969
  rb_define_singleton_method(cGMP_Z, "fib", r_gmpzsg_fib, 1);
1932
1970
 
1933
1971
  // Integer Comparisons
1934
- rb_define_method(cGMP_Z, "<=>", r_gmpz_cmp, 1);
1935
- rb_define_method(cGMP_Z, ">", r_gmpz_cmp_gt, 1);
1936
- rb_define_method(cGMP_Z, ">=", r_gmpz_cmp_ge, 1);
1937
- rb_define_method(cGMP_Z, "<", r_gmpz_cmp_lt, 1);
1938
- rb_define_method(cGMP_Z, "<=", r_gmpz_cmp_le, 1);
1972
+ rb_define_method(cGMP_Z, "<=>", r_gmpz_cmp, 1);
1973
+ rb_define_method(cGMP_Z, ">", r_gmpz_cmp_gt, 1);
1974
+ rb_define_method(cGMP_Z, ">=", r_gmpz_cmp_ge, 1);
1975
+ rb_define_method(cGMP_Z, "==", r_gmpz_eq, 1);
1976
+ rb_define_method(cGMP_Z, "<", r_gmpz_cmp_lt, 1);
1977
+ rb_define_method(cGMP_Z, "<=", r_gmpz_cmp_le, 1);
1939
1978
  rb_define_method(cGMP_Z, "cmpabs", r_gmpz_cmpabs, 1);
1979
+ rb_define_method(cGMP_Z, "sgn", r_gmpz_sgn, 0);
1980
+
1981
+ rb_define_method(cGMP_Z, "eql?", r_gmpz_eql, 1);
1982
+ rb_define_method(cGMP_Z, "hash", r_gmpz_hash, 0);
1940
1983
 
1941
1984
  // Integer Logic and Bit Fiddling
1942
- rb_define_method(cGMP_Z, "com", r_gmpz_com, 0);
1943
- rb_define_method(cGMP_Z, "com!", r_gmpz_com_self, 0);
1944
1985
  rb_define_method(cGMP_Z, "&", r_gmpz_and, 1);
1945
1986
  rb_define_method(cGMP_Z, "|", r_gmpz_or, 1);
1946
1987
  rb_define_method(cGMP_Z, "^", r_gmpz_xor, 1);
1947
- rb_define_method(cGMP_Z, "[]=", r_gmpz_setbit, 2);
1948
- rb_define_method(cGMP_Z, "[]", r_gmpz_getbit, 1);
1988
+ rb_define_method(cGMP_Z, "com", r_gmpz_com, 0);
1989
+ rb_define_method(cGMP_Z, "com!", r_gmpz_com_self, 0);
1990
+ rb_define_method(cGMP_Z, "popcount", r_gmpz_popcount, 0);
1949
1991
  rb_define_method(cGMP_Z, "scan0", r_gmpz_scan0, 1);
1950
1992
  rb_define_method(cGMP_Z, "scan1", r_gmpz_scan1, 1);
1993
+ rb_define_method(cGMP_Z, "[]=", r_gmpz_setbit, 2);
1994
+ rb_define_method(cGMP_Z, "[]", r_gmpz_getbit, 1);
1951
1995
 
1952
1996
  //Miscellaneous Integer Functions
1997
+ rb_define_method(cGMP_Z, "even?", r_gmpz_is_even, 0);
1998
+ rb_define_method(cGMP_Z, "odd?", r_gmpz_is_odd, 0);
1953
1999
  rb_define_method(cGMP_Z, "sizeinbase", r_gmpz_sizeinbase, 1);
1954
2000
  rb_define_alias( cGMP_Z, "size_in_base", "sizeinbase");
1955
2001
  rb_define_method(cGMP_Z, "size_in_bin", r_gmpz_size_in_bin, 0);
1956
2002
 
1957
2003
  // _unsorted_
1958
- rb_define_method(cGMP_Z, "even?", r_gmpz_is_even, 0);
1959
- rb_define_method(cGMP_Z, "odd?", r_gmpz_is_odd, 0);
1960
- rb_define_method(cGMP_Z, "sgn", r_gmpz_sgn, 0);
1961
- rb_define_method(cGMP_Z, "==", r_gmpz_eq, 1);
1962
2004
  rb_define_method(cGMP_Z, ">>", r_gmpz_fshr, 1);
1963
2005
  rb_define_method(cGMP_Z, "tshr", r_gmpz_tshr, 1);
1964
2006
  rb_define_method(cGMP_Z, "lastbits_sgn", r_gmpz_tshrm, 1);
1965
2007
  rb_define_method(cGMP_Z, "lastbits_pos", r_gmpz_fshrm, 1);
1966
- rb_define_method(cGMP_Z, "popcount", r_gmpz_popcount, 0);
1967
2008
 
1968
2009
  }