/src/gmpy_xmpz_inplace.c

http://gmpy.googlecode.com/ · C · 436 lines · 328 code · 57 blank · 51 comment · 66 complexity · 37ac1b52d55dd482403990f5d4bf8924 MD5 · raw file

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * gmpy_xmpz_inplace.c *
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision *
  5. * libraries. *
  6. * *
  7. * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
  8. * 2008, 2009 Alex Martelli *
  9. * *
  10. * Copyright 2008, 2009, 2010, 2011, 2012, 2013 Case Van Horsen *
  11. * *
  12. * This file is part of GMPY2. *
  13. * *
  14. * GMPY2 is free software: you can redistribute it and/or modify it under *
  15. * the terms of the GNU Lesser General Public License as published by the *
  16. * Free Software Foundation, either version 3 of the License, or (at your *
  17. * option) any later version. *
  18. * *
  19. * GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
  22. * License for more details. *
  23. * *
  24. * You should have received a copy of the GNU Lesser General Public *
  25. * License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
  26. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  27. /* Provides inplace mutating operations for xmpz. */
  28. #include <math.h>
  29. /* Inplace xmpz addition. */
  30. static PyObject *
  31. Pyxmpz_inplace_add(PyObject *a, PyObject *b)
  32. {
  33. mpz_t tempz;
  34. mpir_si temp_si;
  35. int overflow;
  36. /* Try to make mpz + small_int faster */
  37. if (PyIntOrLong_Check(b)) {
  38. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  39. if (overflow) {
  40. mpz_inoc(tempz);
  41. mpz_set_PyIntOrLong(tempz, b);
  42. mpz_add(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz);
  43. mpz_cloc(tempz);
  44. }
  45. else if(temp_si >= 0) {
  46. mpz_add_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  47. }
  48. else {
  49. mpz_sub_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si);
  50. }
  51. Py_INCREF(a);
  52. return a;
  53. }
  54. if (CHECK_MPZANY(b)) {
  55. mpz_add(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b));
  56. Py_INCREF(a);
  57. return a;
  58. }
  59. Py_RETURN_NOTIMPLEMENTED;
  60. }
  61. /* Inplace mpz subtraction.
  62. */
  63. static PyObject *
  64. Pyxmpz_inplace_sub(PyObject *a, PyObject *b)
  65. {
  66. mpz_t tempz;
  67. mpir_si temp_si;
  68. int overflow;
  69. if (PyIntOrLong_Check(b)) {
  70. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  71. if (overflow) {
  72. mpz_inoc(tempz);
  73. mpz_set_PyIntOrLong(tempz, b);
  74. mpz_sub(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz);
  75. mpz_cloc(tempz);
  76. }
  77. else if(temp_si >= 0) {
  78. mpz_sub_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  79. }
  80. else {
  81. mpz_add_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si);
  82. }
  83. Py_INCREF(a);
  84. return a;
  85. }
  86. if (CHECK_MPZANY(b)) {
  87. mpz_sub(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b));
  88. Py_INCREF(a);
  89. return a;
  90. }
  91. Py_RETURN_NOTIMPLEMENTED;
  92. }
  93. /* Inplace xmpz multiplication.
  94. */
  95. static PyObject *
  96. Pyxmpz_inplace_mul(PyObject *a, PyObject *b)
  97. {
  98. mpz_t tempz;
  99. mpir_si temp_si;
  100. int overflow;
  101. if (PyIntOrLong_Check(b)) {
  102. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  103. if (overflow) {
  104. mpz_inoc(tempz);
  105. mpz_set_PyIntOrLong(tempz, b);
  106. mpz_mul(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz);
  107. mpz_cloc(tempz);
  108. }
  109. else {
  110. mpz_mul_si(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  111. }
  112. Py_INCREF(a);
  113. return a;
  114. }
  115. if (CHECK_MPZANY(b)) {
  116. mpz_mul(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b));
  117. Py_INCREF(a);
  118. return a;
  119. }
  120. Py_RETURN_NOTIMPLEMENTED;
  121. }
  122. /* Pympany_floordiv follows the // semantics from Python 3.x. The result is
  123. * an mpz when the arguments are mpz or mpq, but the result is an mpf when
  124. * the arguments are mpf.
  125. */
  126. static PyObject *
  127. Pyxmpz_inplace_floordiv(PyObject *a, PyObject *b)
  128. {
  129. mpz_t tempz;
  130. mpir_si temp_si;
  131. int overflow;
  132. if (PyIntOrLong_Check(b)) {
  133. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  134. if (overflow) {
  135. mpz_inoc(tempz);
  136. mpz_set_PyIntOrLong(tempz, b);
  137. mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz);
  138. mpz_cloc(tempz);
  139. }
  140. else if(temp_si == 0) {
  141. ZERO_ERROR("xmpz division by zero");
  142. return NULL;
  143. }
  144. else if(temp_si > 0) {
  145. mpz_fdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  146. }
  147. else {
  148. mpz_cdiv_q_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si);
  149. mpz_neg(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a));
  150. }
  151. Py_INCREF(a);
  152. return a;
  153. }
  154. if (CHECK_MPZANY(b)) {
  155. if (mpz_sgn(Pyxmpz_AS_MPZ(b)) == 0) {
  156. ZERO_ERROR("xmpz division by zero");
  157. return NULL;
  158. }
  159. mpz_fdiv_q(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b));
  160. Py_INCREF(a);
  161. return a;
  162. }
  163. Py_RETURN_NOTIMPLEMENTED;
  164. }
  165. /* Inplace xmpz remainder.
  166. */
  167. static PyObject *
  168. Pyxmpz_inplace_rem(PyObject *a, PyObject *b)
  169. {
  170. mpz_t tempz;
  171. mpir_si temp_si;
  172. int overflow;
  173. if (PyIntOrLong_Check(b)) {
  174. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  175. if (overflow) {
  176. mpz_inoc(tempz);
  177. mpz_set_PyIntOrLong(tempz, b);
  178. mpz_fdiv_r(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), tempz);
  179. mpz_cloc(tempz);
  180. }
  181. else if(temp_si > 0) {
  182. mpz_fdiv_r_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  183. }
  184. else if(temp_si == 0) {
  185. ZERO_ERROR("xmpz modulo by zero");
  186. return NULL;
  187. }
  188. else {
  189. mpz_cdiv_r_ui(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), -temp_si);
  190. }
  191. Py_INCREF(a);
  192. return a;
  193. }
  194. if (CHECK_MPZANY(b)) {
  195. if(mpz_sgn(Pyxmpz_AS_MPZ(b)) == 0) {
  196. ZERO_ERROR("xmpz modulo by zero");
  197. return NULL;
  198. }
  199. mpz_fdiv_r(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(b));
  200. Py_INCREF(a);
  201. return a;
  202. }
  203. Py_RETURN_NOTIMPLEMENTED;
  204. }
  205. /* Inplace xmpz rshift.
  206. */
  207. static PyObject *
  208. Pyxmpz_inplace_rshift(PyObject *a, PyObject *b)
  209. {
  210. mpir_si temp_si;
  211. int overflow;
  212. if (PyIntOrLong_Check(b)) {
  213. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  214. if (overflow) {
  215. OVERFLOW_ERROR("outrageous shift count");
  216. return NULL;
  217. }
  218. else if(temp_si >= 0) {
  219. mpz_fdiv_q_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  220. Py_INCREF(a);
  221. return a;
  222. }
  223. else {
  224. VALUE_ERROR("negative shift count");
  225. return NULL;
  226. }
  227. }
  228. if (CHECK_MPZANY(b)) {
  229. if (mpz_sgn(Pyxmpz_AS_MPZ(b)) < 0) {
  230. VALUE_ERROR("negative shift count");
  231. return NULL;
  232. }
  233. if (!mpz_fits_si_p(Pyxmpz_AS_MPZ(b))) {
  234. OVERFLOW_ERROR("outrageous shift count");
  235. return NULL;
  236. }
  237. temp_si = mpz_get_si(Pyxmpz_AS_MPZ(b));
  238. mpz_fdiv_q_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  239. Py_INCREF(a);
  240. return a;
  241. }
  242. Py_RETURN_NOTIMPLEMENTED;
  243. }
  244. /* Inplace xmpz lshift.
  245. */
  246. static PyObject *
  247. Pyxmpz_inplace_lshift(PyObject *a, PyObject *b)
  248. {
  249. mpir_si temp_si;
  250. int overflow;
  251. if (PyIntOrLong_Check(b)) {
  252. temp_si = PyLong_AsSIAndOverflow(b, &overflow);
  253. if (overflow) {
  254. OVERFLOW_ERROR("outrageous shift count");
  255. return NULL;
  256. }
  257. else if(temp_si >= 0) {
  258. mpz_mul_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  259. }
  260. else {
  261. VALUE_ERROR("negative shift count");
  262. return NULL;
  263. }
  264. }
  265. if (CHECK_MPZANY(b)) {
  266. if (mpz_sgn(Pyxmpz_AS_MPZ(b)) < 0) {
  267. VALUE_ERROR("negative shift count");
  268. return NULL;
  269. }
  270. if (!mpz_fits_si_p(Pyxmpz_AS_MPZ(b))) {
  271. OVERFLOW_ERROR("outrageous shift count");
  272. return NULL;
  273. }
  274. temp_si = mpz_get_si(Pyxmpz_AS_MPZ(b));
  275. mpz_mul_2exp(Pyxmpz_AS_MPZ(a), Pyxmpz_AS_MPZ(a), temp_si);
  276. Py_INCREF(a);
  277. return a;
  278. }
  279. Py_RETURN_NOTIMPLEMENTED;
  280. }
  281. /* Inplace xmpz_pow.
  282. */
  283. static PyObject *
  284. Pyxmpz_inplace_pow(PyObject *in_b, PyObject *in_e, PyObject *in_m)
  285. {
  286. PympzObject *e = 0;
  287. mpir_ui el;
  288. if (!Pyxmpz_Check(in_b)) {
  289. PyErr_SetString(PyExc_TypeError, "base must be an Integer");
  290. return NULL;
  291. }
  292. if (in_m != Py_None) {
  293. SYSTEM_ERROR("modulo not expected");
  294. return NULL;
  295. }
  296. e = Pympz_From_Integer(in_e);
  297. if (!e) {
  298. TYPE_ERROR("expected an integer exponent");
  299. return NULL;
  300. }
  301. if (mpz_sgn(e->z) < 0) {
  302. VALUE_ERROR("xmpz.pow with negative power");
  303. Py_DECREF((PyObject*)e);
  304. return NULL;
  305. }
  306. if (!mpz_fits_ui_p(e->z)) {
  307. VALUE_ERROR("xmpz.pow outrageous exponent");
  308. Py_DECREF((PyObject*)e);
  309. return NULL;
  310. }
  311. el = mpz_get_ui(e->z);
  312. mpz_pow_ui(Pyxmpz_AS_MPZ(in_b), Pyxmpz_AS_MPZ(in_b), el);
  313. Py_DECREF((PyObject*)e);
  314. Py_INCREF((PyObject*)in_b);
  315. return (PyObject*)in_b;
  316. }
  317. /* Inplace xmpz and.
  318. */
  319. static PyObject *
  320. Pyxmpz_inplace_and(PyObject *self, PyObject *other)
  321. {
  322. mpz_t tempz;
  323. if (CHECK_MPZANY(other)) {
  324. mpz_and(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other));
  325. Py_INCREF(self);
  326. return self;
  327. }
  328. if (PyIntOrLong_Check(other)) {
  329. mpz_inoc(tempz);
  330. mpz_set_PyIntOrLong(tempz, other);
  331. mpz_and(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz);
  332. mpz_cloc(tempz);
  333. Py_INCREF(self);
  334. return self;
  335. }
  336. Py_RETURN_NOTIMPLEMENTED;
  337. }
  338. /* Inplace xmpz xor.
  339. */
  340. static PyObject *
  341. Pyxmpz_inplace_xor(PyObject *self, PyObject *other)
  342. {
  343. mpz_t tempz;
  344. if(CHECK_MPZANY(other)) {
  345. mpz_xor(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other));
  346. Py_INCREF(self);
  347. return self;
  348. }
  349. if(PyIntOrLong_Check(other)) {
  350. mpz_inoc(tempz);
  351. mpz_set_PyIntOrLong(tempz, other);
  352. mpz_xor(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz);
  353. mpz_cloc(tempz);
  354. Py_INCREF(self);
  355. return self;
  356. }
  357. Py_RETURN_NOTIMPLEMENTED;
  358. }
  359. /* Inplace xmpz or.
  360. */
  361. static PyObject *
  362. Pyxmpz_inplace_ior(PyObject *self, PyObject *other)
  363. {
  364. mpz_t tempz;
  365. if(CHECK_MPZANY(other)) {
  366. mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other));
  367. Py_INCREF(self);
  368. return self;
  369. }
  370. if(PyIntOrLong_Check(other)) {
  371. mpz_inoc(tempz);
  372. mpz_set_PyIntOrLong(tempz, other);
  373. mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz);
  374. mpz_cloc(tempz);
  375. Py_INCREF(self);
  376. return self;
  377. }
  378. Py_RETURN_NOTIMPLEMENTED;
  379. }