py_number.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #include "pocketpy/interpreter/vm.h"
  2. #include "pocketpy/common/sstream.h"
  3. #include "pocketpy/pocketpy.h"
  4. #include <math.h>
  5. static bool try_castfloat(py_Ref self, double* out) {
  6. switch(self->type) {
  7. case tp_int: *out = (double)self->_i64; return true;
  8. case tp_float: *out = self->_f64; return true;
  9. default: return false;
  10. }
  11. }
  12. #define DEF_NUM_BINARY_OP(name, op, rint, rfloat) \
  13. static bool int##name(int argc, py_Ref argv) { \
  14. PY_CHECK_ARGC(2); \
  15. if(py_isint(&argv[1])) { \
  16. py_i64 lhs = py_toint(&argv[0]); \
  17. py_i64 rhs = py_toint(&argv[1]); \
  18. rint(py_retval(), lhs op rhs); \
  19. } else if(py_isfloat(&argv[1])) { \
  20. py_i64 lhs = py_toint(&argv[0]); \
  21. py_f64 rhs = py_tofloat(&argv[1]); \
  22. rfloat(py_retval(), lhs op rhs); \
  23. } else { \
  24. py_newnotimplemented(py_retval()); \
  25. } \
  26. return true; \
  27. } \
  28. static bool float##name(int argc, py_Ref argv) { \
  29. PY_CHECK_ARGC(2); \
  30. py_f64 lhs = py_tofloat(&argv[0]); \
  31. py_f64 rhs; \
  32. if(try_castfloat(&argv[1], &rhs)) { \
  33. rfloat(py_retval(), lhs op rhs); \
  34. } else { \
  35. py_newnotimplemented(py_retval()); \
  36. } \
  37. return true; \
  38. }
  39. DEF_NUM_BINARY_OP(__add__, +, py_newint, py_newfloat)
  40. DEF_NUM_BINARY_OP(__sub__, -, py_newint, py_newfloat)
  41. DEF_NUM_BINARY_OP(__mul__, *, py_newint, py_newfloat)
  42. DEF_NUM_BINARY_OP(__eq__, ==, py_newbool, py_newbool)
  43. DEF_NUM_BINARY_OP(__ne__, !=, py_newbool, py_newbool)
  44. DEF_NUM_BINARY_OP(__lt__, <, py_newbool, py_newbool)
  45. DEF_NUM_BINARY_OP(__le__, <=, py_newbool, py_newbool)
  46. DEF_NUM_BINARY_OP(__gt__, >, py_newbool, py_newbool)
  47. DEF_NUM_BINARY_OP(__ge__, >=, py_newbool, py_newbool)
  48. static bool int__neg__(int argc, py_Ref argv) {
  49. PY_CHECK_ARGC(1);
  50. py_i64 val = py_toint(&argv[0]);
  51. py_newint(py_retval(), -val);
  52. return true;
  53. }
  54. static bool float__neg__(int argc, py_Ref argv) {
  55. PY_CHECK_ARGC(1);
  56. py_f64 val = py_tofloat(&argv[0]);
  57. py_newfloat(py_retval(), -val);
  58. return true;
  59. }
  60. static bool int__truediv__(int argc, py_Ref argv) {
  61. PY_CHECK_ARGC(2);
  62. py_i64 lhs = py_toint(&argv[0]);
  63. py_f64 rhs;
  64. if(try_castfloat(&argv[1], &rhs)) {
  65. if(rhs == 0.0) return ZeroDivisionError("float division by zero");
  66. py_newfloat(py_retval(), lhs / rhs);
  67. } else {
  68. py_newnotimplemented(py_retval());
  69. }
  70. return true;
  71. }
  72. static bool float__truediv__(int argc, py_Ref argv) {
  73. PY_CHECK_ARGC(2);
  74. py_f64 lhs = py_tofloat(&argv[0]);
  75. py_f64 rhs;
  76. if(try_castfloat(&argv[1], &rhs)) {
  77. if(rhs == 0.0) return ZeroDivisionError("float division by zero");
  78. py_newfloat(py_retval(), lhs / rhs);
  79. } else {
  80. py_newnotimplemented(py_retval());
  81. }
  82. return true;
  83. }
  84. static bool number__pow__(int argc, py_Ref argv) {
  85. PY_CHECK_ARGC(2);
  86. if(py_isint(&argv[0]) && py_isint(&argv[1])) {
  87. py_i64 lhs = py_toint(&argv[0]);
  88. py_i64 rhs = py_toint(&argv[1]);
  89. if(rhs < 0) {
  90. if(lhs == 0) {
  91. return ZeroDivisionError("0.0 cannot be raised to a negative power");
  92. } else {
  93. py_newfloat(py_retval(), pow(lhs, rhs));
  94. }
  95. } else {
  96. // rhs >= 0
  97. py_i64 ret = 1;
  98. while(true) {
  99. if(rhs & 1) ret *= lhs;
  100. rhs >>= 1;
  101. if(!rhs) break;
  102. lhs *= lhs; // place this here to avoid overflow
  103. }
  104. py_newint(py_retval(), ret);
  105. }
  106. } else {
  107. py_f64 lhs, rhs;
  108. if(!py_castfloat(&argv[0], &lhs)) return false;
  109. if(try_castfloat(&argv[1], &rhs)) {
  110. py_newfloat(py_retval(), pow(lhs, rhs));
  111. } else {
  112. py_newnotimplemented(py_retval());
  113. }
  114. }
  115. return true;
  116. }
  117. static py_i64 i64_abs(py_i64 x) { return x < 0 ? -x : x; }
  118. static py_i64 cpy11__fast_floor_div(py_i64 a, py_i64 b) {
  119. assert(b != 0);
  120. if(a == 0) return 0;
  121. if((a < 0) == (b < 0)) {
  122. return i64_abs(a) / i64_abs(b);
  123. } else {
  124. return -1 - (i64_abs(a) - 1) / i64_abs(b);
  125. }
  126. }
  127. static py_i64 cpy11__fast_mod(py_i64 a, py_i64 b) {
  128. assert(b != 0);
  129. if(a == 0) return 0;
  130. py_i64 res;
  131. if((a < 0) == (b < 0)) {
  132. res = i64_abs(a) % i64_abs(b);
  133. } else {
  134. res = i64_abs(b) - 1 - (i64_abs(a) - 1) % i64_abs(b);
  135. }
  136. return b < 0 ? -res : res;
  137. }
  138. // https://github.com/python/cpython/blob/3.11/Objects/floatobject.c#L677
  139. static void cpy11__float_div_mod(double vx, double wx, double *floordiv, double *mod)
  140. {
  141. double div;
  142. *mod = fmod(vx, wx);
  143. /* fmod is typically exact, so vx-mod is *mathematically* an
  144. exact multiple of wx. But this is fp arithmetic, and fp
  145. vx - mod is an approximation; the result is that div may
  146. not be an exact integral value after the division, although
  147. it will always be very close to one.
  148. */
  149. div = (vx - *mod) / wx;
  150. if (*mod) {
  151. /* ensure the remainder has the same sign as the denominator */
  152. if ((wx < 0) != (*mod < 0)) {
  153. *mod += wx;
  154. div -= 1.0;
  155. }
  156. }
  157. else {
  158. /* the remainder is zero, and in the presence of signed zeroes
  159. fmod returns different results across platforms; ensure
  160. it has the same sign as the denominator. */
  161. *mod = copysign(0.0, wx);
  162. }
  163. /* snap quotient to nearest integral value */
  164. if (div) {
  165. *floordiv = floor(div);
  166. if (div - *floordiv > 0.5) {
  167. *floordiv += 1.0;
  168. }
  169. }
  170. else {
  171. /* div is zero - get the same sign as the true quotient */
  172. *floordiv = copysign(0.0, vx / wx); /* zero w/ sign of vx/wx */
  173. }
  174. }
  175. static bool int__floordiv__(int argc, py_Ref argv) {
  176. PY_CHECK_ARGC(2);
  177. py_i64 lhs = py_toint(&argv[0]);
  178. if(py_isint(&argv[1])) {
  179. py_i64 rhs = py_toint(&argv[1]);
  180. if(rhs == 0) return ZeroDivisionError("integer division by zero");
  181. py_newint(py_retval(), cpy11__fast_floor_div(lhs, rhs));
  182. } else {
  183. py_newnotimplemented(py_retval());
  184. }
  185. return true;
  186. }
  187. static bool int__mod__(int argc, py_Ref argv) {
  188. PY_CHECK_ARGC(2);
  189. py_i64 lhs = py_toint(&argv[0]);
  190. if(py_isint(&argv[1])) {
  191. py_i64 rhs = py_toint(&argv[1]);
  192. if(rhs == 0) return ZeroDivisionError("integer modulo by zero");
  193. py_newint(py_retval(), cpy11__fast_mod(lhs, rhs));
  194. } else {
  195. py_newnotimplemented(py_retval());
  196. }
  197. return true;
  198. }
  199. static bool float__floordiv__(int argc, py_Ref argv) {
  200. PY_CHECK_ARGC(2);
  201. py_f64 lhs = py_tofloat(&argv[0]);
  202. py_f64 rhs;
  203. if(try_castfloat(&argv[1], &rhs)) {
  204. if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
  205. double q, r;
  206. cpy11__float_div_mod(lhs, rhs, &q, &r);
  207. py_newfloat(py_retval(), q);
  208. return true;
  209. }
  210. py_newnotimplemented(py_retval());
  211. return true;
  212. }
  213. static bool float__rfloordiv__(int argc, py_Ref argv) {
  214. PY_CHECK_ARGC(2);
  215. py_f64 rhs = py_tofloat(&argv[0]);
  216. py_f64 lhs;
  217. if(try_castfloat(&argv[1], &lhs)) {
  218. if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
  219. double q, r;
  220. cpy11__float_div_mod(lhs, rhs, &q, &r);
  221. py_newfloat(py_retval(), q);
  222. return true;
  223. }
  224. py_newnotimplemented(py_retval());
  225. return true;
  226. }
  227. static bool float__mod__(int argc, py_Ref argv) {
  228. PY_CHECK_ARGC(2);
  229. py_f64 lhs = py_tofloat(&argv[0]);
  230. py_f64 rhs;
  231. if(try_castfloat(&argv[1], &rhs)) {
  232. if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
  233. double q, r;
  234. cpy11__float_div_mod(lhs, rhs, &q, &r);
  235. py_newfloat(py_retval(), r);
  236. return true;
  237. }
  238. py_newnotimplemented(py_retval());
  239. return true;
  240. }
  241. static bool float__rmod__(int argc, py_Ref argv) {
  242. PY_CHECK_ARGC(2);
  243. py_f64 rhs = py_tofloat(&argv[0]);
  244. py_f64 lhs;
  245. if(try_castfloat(&argv[1], &lhs)) {
  246. if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
  247. double q, r;
  248. cpy11__float_div_mod(lhs, rhs, &q, &r);
  249. py_newfloat(py_retval(), r);
  250. return true;
  251. }
  252. py_newnotimplemented(py_retval());
  253. return true;
  254. }
  255. static bool float__divmod__(int argc, py_Ref argv) {
  256. PY_CHECK_ARGC(2);
  257. py_f64 lhs = py_tofloat(&argv[0]);
  258. py_f64 rhs;
  259. if(try_castfloat(&argv[1], &rhs)) {
  260. if(rhs == 0.0) return ZeroDivisionError("float modulo by zero");
  261. double q, r;
  262. cpy11__float_div_mod(lhs, rhs, &q, &r);
  263. py_Ref p = py_newtuple(py_retval(), 2);
  264. py_newfloat(&p[0], q);
  265. py_newfloat(&p[1], r);
  266. return true;
  267. }
  268. return TypeError("divmod() expects int or float as divisor");
  269. }
  270. static bool int__divmod__(int argc, py_Ref argv) {
  271. PY_CHECK_ARGC(2);
  272. PY_CHECK_ARG_TYPE(1, tp_int);
  273. py_i64 lhs = py_toint(&argv[0]);
  274. py_i64 rhs = py_toint(&argv[1]);
  275. if(rhs == 0) return ZeroDivisionError("integer division or modulo by zero");
  276. py_Ref p = py_newtuple(py_retval(), 2);
  277. py_newint(&p[0], cpy11__fast_floor_div(lhs, rhs));
  278. py_newint(&p[1], cpy11__fast_mod(lhs, rhs));
  279. return true;
  280. }
  281. static bool int__invert__(int argc, py_Ref argv) {
  282. PY_CHECK_ARGC(1);
  283. py_i64 val = py_toint(&argv[0]);
  284. py_newint(py_retval(), ~val);
  285. return true;
  286. }
  287. static bool int_bit_length(int argc, py_Ref argv) {
  288. PY_CHECK_ARGC(1);
  289. py_i64 x = py_toint(py_arg(0));
  290. if(x < 0) x = -x;
  291. int bits = 0;
  292. while(x) {
  293. x >>= 1;
  294. bits++;
  295. }
  296. py_newint(py_retval(), bits);
  297. return true;
  298. }
  299. #define DEF_INT_BITWISE_OP(name, op) \
  300. static bool int##name(int argc, py_Ref argv) { \
  301. PY_CHECK_ARGC(2); \
  302. py_i64 lhs = py_toint(&argv[0]); \
  303. if(py_isint(&argv[1])) { \
  304. py_i64 rhs = py_toint(&argv[1]); \
  305. py_newint(py_retval(), lhs op rhs); \
  306. } else { \
  307. py_newnotimplemented(py_retval()); \
  308. } \
  309. return true; \
  310. }
  311. DEF_INT_BITWISE_OP(__and__, &)
  312. DEF_INT_BITWISE_OP(__or__, |)
  313. DEF_INT_BITWISE_OP(__xor__, ^)
  314. DEF_INT_BITWISE_OP(__lshift__, <<)
  315. DEF_INT_BITWISE_OP(__rshift__, >>)
  316. static bool int__repr__(int argc, py_Ref argv) {
  317. PY_CHECK_ARGC(1);
  318. py_i64 val = py_toint(&argv[0]);
  319. char buf[32];
  320. int size = snprintf(buf, sizeof(buf), "%lld", (long long)val);
  321. py_newstrv(py_retval(), (c11_sv){buf, size});
  322. return true;
  323. }
  324. static bool float__repr__(int argc, py_Ref argv) {
  325. PY_CHECK_ARGC(1);
  326. py_f64 val = py_tofloat(&argv[0]);
  327. c11_sbuf buf;
  328. c11_sbuf__ctor(&buf);
  329. c11_sbuf__write_f64(&buf, val, -1);
  330. c11_sbuf__py_submit(&buf, py_retval());
  331. return true;
  332. }
  333. static bool int__hash__(int argc, py_Ref argv) {
  334. PY_CHECK_ARGC(1);
  335. py_assign(py_retval(), argv);
  336. return true;
  337. }
  338. static bool float__hash__(int argc, py_Ref argv) {
  339. PY_CHECK_ARGC(1);
  340. py_f64 val = py_tofloat(&argv[0]);
  341. py_i64 h_user;
  342. memcpy(&h_user, &val, sizeof(py_f64));
  343. py_newint(py_retval(), h_user);
  344. return true;
  345. }
  346. static bool int__abs__(int argc, py_Ref argv) {
  347. PY_CHECK_ARGC(1);
  348. py_i64 val = py_toint(&argv[0]);
  349. py_newint(py_retval(), val < 0 ? -val : val);
  350. return true;
  351. }
  352. static bool float__abs__(int argc, py_Ref argv) {
  353. PY_CHECK_ARGC(1);
  354. py_f64 val = py_tofloat(&argv[0]);
  355. py_newfloat(py_retval(), val < 0 ? -val : val);
  356. return true;
  357. }
  358. static bool int__new__(int argc, py_Ref argv) {
  359. if(argc == 1 + 0) {
  360. // int() == 0
  361. py_newint(py_retval(), 0);
  362. return true;
  363. }
  364. // 1 arg
  365. if(argc == 1 + 1) {
  366. switch(argv[1].type) {
  367. case tp_float: {
  368. // int(1.1) == 1
  369. py_newint(py_retval(), (py_i64)py_tofloat(&argv[1]));
  370. return true;
  371. }
  372. case tp_int: {
  373. // int(1) == 1
  374. *py_retval() = argv[1];
  375. return true;
  376. }
  377. case tp_bool: {
  378. // int(True) == 1
  379. py_newint(py_retval(), (py_i64)py_tobool(&argv[1]));
  380. return true;
  381. }
  382. case tp_str: break; // leave to the next block
  383. default: return TypeError("int() argument must be a string, number or boolean");
  384. }
  385. }
  386. // 2+ args -> error
  387. if(argc > 1 + 2) return TypeError("int() takes at most 2 arguments");
  388. // 1 or 2 args with str
  389. int base = 10;
  390. if(argc == 1 + 2) {
  391. PY_CHECK_ARG_TYPE(2, tp_int);
  392. base = py_toint(py_arg(2));
  393. }
  394. PY_CHECK_ARG_TYPE(1, tp_str);
  395. c11_sv sv = py_tosv(py_arg(1));
  396. bool negative = false;
  397. if(sv.size && (sv.data[0] == '+' || sv.data[0] == '-')) {
  398. negative = sv.data[0] == '-';
  399. sv.data++;
  400. sv.size--;
  401. }
  402. py_i64 val;
  403. if(c11__parse_uint(sv, &val, base) != IntParsing_SUCCESS) {
  404. return ValueError("invalid literal for int() with base %d: %q", base, sv);
  405. }
  406. py_newint(py_retval(), negative ? -val : val);
  407. return true;
  408. }
  409. static bool float__new__(int argc, py_Ref argv) {
  410. if(argc == 1 + 0) {
  411. // float() == 0.0
  412. py_newfloat(py_retval(), 0.0);
  413. return true;
  414. }
  415. if(argc > 1 + 1) return TypeError("float() takes at most 1 argument");
  416. // 1 arg
  417. switch(argv[1].type) {
  418. case tp_int: {
  419. // float(1) == 1.0
  420. py_newfloat(py_retval(), py_toint(&argv[1]));
  421. return true;
  422. }
  423. case tp_float: {
  424. // float(1.1) == 1.1
  425. *py_retval() = argv[1];
  426. return true;
  427. }
  428. case tp_bool: {
  429. // float(True) == 1.0
  430. py_newfloat(py_retval(), py_tobool(&argv[1]));
  431. return true;
  432. }
  433. case tp_str: {
  434. // str to float
  435. c11_sv sv = py_tosv(py_arg(1));
  436. if(c11__sveq2(sv, "inf")) {
  437. py_newfloat(py_retval(), INFINITY);
  438. return true;
  439. }
  440. if(c11__sveq2(sv, "-inf")) {
  441. py_newfloat(py_retval(), -INFINITY);
  442. return true;
  443. }
  444. char* p_end;
  445. py_f64 float_out = strtod(sv.data, &p_end);
  446. if(p_end != sv.data + sv.size) return ValueError("invalid literal for float(): %q", sv);
  447. py_newfloat(py_retval(), float_out);
  448. return true;
  449. }
  450. default: return TypeError("float() argument must be a string or a real number");
  451. }
  452. }
  453. // tp_bool
  454. static bool bool__new__(int argc, py_Ref argv) {
  455. assert(argc > 0);
  456. if(argc == 1) {
  457. py_newbool(py_retval(), false);
  458. return true;
  459. }
  460. if(argc == 2) {
  461. int res = py_bool(py_arg(1));
  462. if(res == -1) return false;
  463. py_newbool(py_retval(), res);
  464. return true;
  465. }
  466. return TypeError("bool() takes at most 1 argument");
  467. }
  468. static bool bool__hash__(int argc, py_Ref argv) {
  469. PY_CHECK_ARGC(1);
  470. bool res = py_tobool(argv);
  471. py_newint(py_retval(), res);
  472. return true;
  473. }
  474. static bool bool__repr__(int argc, py_Ref argv) {
  475. PY_CHECK_ARGC(1);
  476. bool res = py_tobool(argv);
  477. py_newstr(py_retval(), res ? "True" : "False");
  478. return true;
  479. }
  480. static bool bool__eq__(int argc, py_Ref argv) {
  481. PY_CHECK_ARGC(2);
  482. bool lhs = py_tobool(&argv[0]);
  483. if(argv[1].type == tp_bool) {
  484. bool rhs = py_tobool(&argv[1]);
  485. py_newbool(py_retval(), lhs == rhs);
  486. } else {
  487. py_newnotimplemented(py_retval());
  488. }
  489. return true;
  490. }
  491. static bool bool__ne__(int argc, py_Ref argv) {
  492. PY_CHECK_ARGC(2);
  493. bool lhs = py_tobool(&argv[0]);
  494. if(argv[1].type == tp_bool) {
  495. bool rhs = py_tobool(&argv[1]);
  496. py_newbool(py_retval(), lhs != rhs);
  497. } else {
  498. py_newnotimplemented(py_retval());
  499. }
  500. return true;
  501. }
  502. #define DEF_BOOL_BITWISE(name, op) \
  503. static bool bool##name(int argc, py_Ref argv) { \
  504. PY_CHECK_ARGC(2); \
  505. bool lhs = py_tobool(&argv[0]); \
  506. if(argv[1].type == tp_bool) { \
  507. bool rhs = py_tobool(&argv[1]); \
  508. py_newbool(py_retval(), lhs op rhs); \
  509. } else { \
  510. py_newnotimplemented(py_retval()); \
  511. } \
  512. return true; \
  513. }
  514. DEF_BOOL_BITWISE(__and__, &&)
  515. DEF_BOOL_BITWISE(__or__, ||)
  516. DEF_BOOL_BITWISE(__xor__, !=)
  517. static bool bool__invert__(int argc, py_Ref argv) {
  518. PY_CHECK_ARGC(1);
  519. bool val = py_tobool(&argv[0]);
  520. py_newbool(py_retval(), !val);
  521. return true;
  522. }
  523. void pk_number__register() {
  524. /****** tp_int & tp_float ******/
  525. py_bindmagic(tp_int, __add__, int__add__);
  526. py_bindmagic(tp_float, __add__, float__add__);
  527. py_bindmagic(tp_int, __sub__, int__sub__);
  528. py_bindmagic(tp_float, __sub__, float__sub__);
  529. py_bindmagic(tp_int, __mul__, int__mul__);
  530. py_bindmagic(tp_float, __mul__, float__mul__);
  531. py_bindmagic(tp_int, __eq__, int__eq__);
  532. py_bindmagic(tp_float, __eq__, float__eq__);
  533. py_bindmagic(tp_int, __ne__, int__ne__);
  534. py_bindmagic(tp_float, __ne__, float__ne__);
  535. py_bindmagic(tp_int, __lt__, int__lt__);
  536. py_bindmagic(tp_float, __lt__, float__lt__);
  537. py_bindmagic(tp_int, __le__, int__le__);
  538. py_bindmagic(tp_float, __le__, float__le__);
  539. py_bindmagic(tp_int, __gt__, int__gt__);
  540. py_bindmagic(tp_float, __gt__, float__gt__);
  541. py_bindmagic(tp_int, __ge__, int__ge__);
  542. py_bindmagic(tp_float, __ge__, float__ge__);
  543. // __neg__
  544. py_bindmagic(tp_int, __neg__, int__neg__);
  545. py_bindmagic(tp_float, __neg__, float__neg__);
  546. // __repr__
  547. py_bindmagic(tp_int, __repr__, int__repr__);
  548. py_bindmagic(tp_float, __repr__, float__repr__);
  549. // __hash__
  550. py_bindmagic(tp_int, __hash__, int__hash__);
  551. py_bindmagic(tp_float, __hash__, float__hash__);
  552. // __abs__
  553. py_bindmagic(tp_int, __abs__, int__abs__);
  554. py_bindmagic(tp_float, __abs__, float__abs__);
  555. // __new__
  556. py_bindmagic(tp_int, __new__, int__new__);
  557. py_bindmagic(tp_float, __new__, float__new__);
  558. // __truediv__
  559. py_bindmagic(tp_int, __truediv__, int__truediv__);
  560. py_bindmagic(tp_float, __truediv__, float__truediv__);
  561. // __pow__
  562. py_bindmagic(tp_int, __pow__, number__pow__);
  563. py_bindmagic(tp_float, __pow__, number__pow__);
  564. // __floordiv__ & __mod__ & __divmod__
  565. py_bindmagic(tp_int, __floordiv__, int__floordiv__);
  566. py_bindmagic(tp_int, __mod__, int__mod__);
  567. py_bindmagic(tp_int, __divmod__, int__divmod__);
  568. // fmod
  569. py_bindmagic(tp_float, __floordiv__, float__floordiv__);
  570. py_bindmagic(tp_float, __rfloordiv__, float__rfloordiv__);
  571. py_bindmagic(tp_float, __mod__, float__mod__);
  572. py_bindmagic(tp_float, __rmod__, float__rmod__);
  573. py_bindmagic(tp_float, __divmod__, float__divmod__);
  574. // int.__invert__ & int.<BITWISE OP>
  575. py_bindmagic(tp_int, __invert__, int__invert__);
  576. py_bindmagic(tp_int, __and__, int__and__);
  577. py_bindmagic(tp_int, __or__, int__or__);
  578. py_bindmagic(tp_int, __xor__, int__xor__);
  579. py_bindmagic(tp_int, __lshift__, int__lshift__);
  580. py_bindmagic(tp_int, __rshift__, int__rshift__);
  581. // int.bit_length
  582. py_bindmethod(tp_int, "bit_length", int_bit_length);
  583. /* tp_bool */
  584. py_bindmagic(tp_bool, __new__, bool__new__);
  585. py_bindmagic(tp_bool, __hash__, bool__hash__);
  586. py_bindmagic(tp_bool, __repr__, bool__repr__);
  587. py_bindmagic(tp_bool, __eq__, bool__eq__);
  588. py_bindmagic(tp_bool, __ne__, bool__ne__);
  589. py_bindmagic(tp_bool, __and__, bool__and__);
  590. py_bindmagic(tp_bool, __or__, bool__or__);
  591. py_bindmagic(tp_bool, __xor__, bool__xor__);
  592. py_bindmagic(tp_bool, __invert__, bool__invert__);
  593. }
  594. #undef DEF_NUM_BINARY_OP
  595. #undef DEF_INT_BITWISE_OP
  596. #undef DEF_BOOL_BITWISE