Optimize.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * Optional optimisations of built-in functions and methods.
  3. *
  4. * Required replacements of builtins are in Builtins.c.
  5. *
  6. * General object operations and protocols are in ObjectHandling.c.
  7. */
  8. /////////////// append.proto ///////////////
  9. static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x); /*proto*/
  10. /////////////// append ///////////////
  11. //@requires: ListAppend
  12. //@requires: ObjectHandling.c::PyObjectCallMethod1
  13. static CYTHON_INLINE int __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
  14. if (likely(PyList_CheckExact(L))) {
  15. if (unlikely(__Pyx_PyList_Append(L, x) < 0)) return -1;
  16. } else {
  17. PyObject* retval = __Pyx_PyObject_CallMethod1(L, PYIDENT("append"), x);
  18. if (unlikely(!retval))
  19. return -1;
  20. Py_DECREF(retval);
  21. }
  22. return 0;
  23. }
  24. /////////////// ListAppend.proto ///////////////
  25. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  26. static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {
  27. PyListObject* L = (PyListObject*) list;
  28. Py_ssize_t len = Py_SIZE(list);
  29. if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {
  30. Py_INCREF(x);
  31. PyList_SET_ITEM(list, len, x);
  32. Py_SIZE(list) = len+1;
  33. return 0;
  34. }
  35. return PyList_Append(list, x);
  36. }
  37. #else
  38. #define __Pyx_PyList_Append(L,x) PyList_Append(L,x)
  39. #endif
  40. /////////////// ListCompAppend.proto ///////////////
  41. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  42. static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) {
  43. PyListObject* L = (PyListObject*) list;
  44. Py_ssize_t len = Py_SIZE(list);
  45. if (likely(L->allocated > len)) {
  46. Py_INCREF(x);
  47. PyList_SET_ITEM(list, len, x);
  48. Py_SIZE(list) = len+1;
  49. return 0;
  50. }
  51. return PyList_Append(list, x);
  52. }
  53. #else
  54. #define __Pyx_ListComp_Append(L,x) PyList_Append(L,x)
  55. #endif
  56. //////////////////// ListExtend.proto ////////////////////
  57. static CYTHON_INLINE int __Pyx_PyList_Extend(PyObject* L, PyObject* v) {
  58. #if CYTHON_COMPILING_IN_CPYTHON
  59. PyObject* none = _PyList_Extend((PyListObject*)L, v);
  60. if (unlikely(!none))
  61. return -1;
  62. Py_DECREF(none);
  63. return 0;
  64. #else
  65. return PyList_SetSlice(L, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, v);
  66. #endif
  67. }
  68. /////////////// pop.proto ///////////////
  69. static CYTHON_INLINE PyObject* __Pyx__PyObject_Pop(PyObject* L); /*proto*/
  70. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  71. static CYTHON_INLINE PyObject* __Pyx_PyList_Pop(PyObject* L); /*proto*/
  72. #define __Pyx_PyObject_Pop(L) (likely(PyList_CheckExact(L)) ? \
  73. __Pyx_PyList_Pop(L) : __Pyx__PyObject_Pop(L))
  74. #else
  75. #define __Pyx_PyList_Pop(L) __Pyx__PyObject_Pop(L)
  76. #define __Pyx_PyObject_Pop(L) __Pyx__PyObject_Pop(L)
  77. #endif
  78. /////////////// pop ///////////////
  79. //@requires: ObjectHandling.c::PyObjectCallMethod0
  80. static CYTHON_INLINE PyObject* __Pyx__PyObject_Pop(PyObject* L) {
  81. if (Py_TYPE(L) == &PySet_Type) {
  82. return PySet_Pop(L);
  83. }
  84. return __Pyx_PyObject_CallMethod0(L, PYIDENT("pop"));
  85. }
  86. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  87. static CYTHON_INLINE PyObject* __Pyx_PyList_Pop(PyObject* L) {
  88. /* Check that both the size is positive and no reallocation shrinking needs to be done. */
  89. if (likely(PyList_GET_SIZE(L) > (((PyListObject*)L)->allocated >> 1))) {
  90. Py_SIZE(L) -= 1;
  91. return PyList_GET_ITEM(L, PyList_GET_SIZE(L));
  92. }
  93. return CALL_UNBOUND_METHOD(PyList_Type, "pop", L);
  94. }
  95. #endif
  96. /////////////// pop_index.proto ///////////////
  97. static PyObject* __Pyx__PyObject_PopNewIndex(PyObject* L, PyObject* py_ix); /*proto*/
  98. static PyObject* __Pyx__PyObject_PopIndex(PyObject* L, PyObject* py_ix); /*proto*/
  99. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  100. static PyObject* __Pyx__PyList_PopIndex(PyObject* L, PyObject* py_ix, Py_ssize_t ix); /*proto*/
  101. #define __Pyx_PyObject_PopIndex(L, py_ix, ix, is_signed, type, to_py_func) ( \
  102. (likely(PyList_CheckExact(L) && __Pyx_fits_Py_ssize_t(ix, type, is_signed))) ? \
  103. __Pyx__PyList_PopIndex(L, py_ix, ix) : ( \
  104. (unlikely(py_ix == Py_None)) ? __Pyx__PyObject_PopNewIndex(L, to_py_func(ix)) : \
  105. __Pyx__PyObject_PopIndex(L, py_ix)))
  106. #define __Pyx_PyList_PopIndex(L, py_ix, ix, is_signed, type, to_py_func) ( \
  107. __Pyx_fits_Py_ssize_t(ix, type, is_signed) ? \
  108. __Pyx__PyList_PopIndex(L, py_ix, ix) : ( \
  109. (unlikely(py_ix == Py_None)) ? __Pyx__PyObject_PopNewIndex(L, to_py_func(ix)) : \
  110. __Pyx__PyObject_PopIndex(L, py_ix)))
  111. #else
  112. #define __Pyx_PyList_PopIndex(L, py_ix, ix, is_signed, type, to_py_func) \
  113. __Pyx_PyObject_PopIndex(L, py_ix, ix, is_signed, type, to_py_func)
  114. #define __Pyx_PyObject_PopIndex(L, py_ix, ix, is_signed, type, to_py_func) ( \
  115. (unlikely(py_ix == Py_None)) ? __Pyx__PyObject_PopNewIndex(L, to_py_func(ix)) : \
  116. __Pyx__PyObject_PopIndex(L, py_ix))
  117. #endif
  118. /////////////// pop_index ///////////////
  119. //@requires: ObjectHandling.c::PyObjectCallMethod1
  120. static PyObject* __Pyx__PyObject_PopNewIndex(PyObject* L, PyObject* py_ix) {
  121. PyObject *r;
  122. if (unlikely(!py_ix)) return NULL;
  123. r = __Pyx__PyObject_PopIndex(L, py_ix);
  124. Py_DECREF(py_ix);
  125. return r;
  126. }
  127. static PyObject* __Pyx__PyObject_PopIndex(PyObject* L, PyObject* py_ix) {
  128. return __Pyx_PyObject_CallMethod1(L, PYIDENT("pop"), py_ix);
  129. }
  130. #if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS
  131. static PyObject* __Pyx__PyList_PopIndex(PyObject* L, PyObject* py_ix, Py_ssize_t ix) {
  132. Py_ssize_t size = PyList_GET_SIZE(L);
  133. if (likely(size > (((PyListObject*)L)->allocated >> 1))) {
  134. Py_ssize_t cix = ix;
  135. if (cix < 0) {
  136. cix += size;
  137. }
  138. if (likely(0 <= cix && cix < size)) {
  139. PyObject* v = PyList_GET_ITEM(L, cix);
  140. Py_SIZE(L) -= 1;
  141. size -= 1;
  142. memmove(&PyList_GET_ITEM(L, cix), &PyList_GET_ITEM(L, cix+1), (size_t)(size-cix)*sizeof(PyObject*));
  143. return v;
  144. }
  145. }
  146. if (py_ix == Py_None) {
  147. return __Pyx__PyObject_PopNewIndex(L, PyInt_FromSsize_t(ix));
  148. } else {
  149. return __Pyx__PyObject_PopIndex(L, py_ix);
  150. }
  151. }
  152. #endif
  153. /////////////// dict_getitem_default.proto ///////////////
  154. static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value); /*proto*/
  155. /////////////// dict_getitem_default ///////////////
  156. static PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject* key, PyObject* default_value) {
  157. PyObject* value;
  158. #if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
  159. value = PyDict_GetItemWithError(d, key);
  160. if (unlikely(!value)) {
  161. if (unlikely(PyErr_Occurred()))
  162. return NULL;
  163. value = default_value;
  164. }
  165. Py_INCREF(value);
  166. // avoid C compiler warning about unused utility functions
  167. if ((1));
  168. #else
  169. if (PyString_CheckExact(key) || PyUnicode_CheckExact(key) || PyInt_CheckExact(key)) {
  170. /* these presumably have safe hash functions */
  171. value = PyDict_GetItem(d, key);
  172. if (unlikely(!value)) {
  173. value = default_value;
  174. }
  175. Py_INCREF(value);
  176. }
  177. #endif
  178. else {
  179. if (default_value == Py_None)
  180. value = CALL_UNBOUND_METHOD(PyDict_Type, "get", d, key);
  181. else
  182. value = CALL_UNBOUND_METHOD(PyDict_Type, "get", d, key, default_value);
  183. }
  184. return value;
  185. }
  186. /////////////// dict_setdefault.proto ///////////////
  187. static CYTHON_INLINE PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *default_value, int is_safe_type); /*proto*/
  188. /////////////// dict_setdefault ///////////////
  189. static CYTHON_INLINE PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *default_value,
  190. CYTHON_UNUSED int is_safe_type) {
  191. PyObject* value;
  192. #if PY_VERSION_HEX >= 0x030400A0
  193. // we keep the method call at the end to avoid "unused" C compiler warnings
  194. if ((1)) {
  195. value = PyDict_SetDefault(d, key, default_value);
  196. if (unlikely(!value)) return NULL;
  197. Py_INCREF(value);
  198. #else
  199. if (is_safe_type == 1 || (is_safe_type == -1 &&
  200. /* the following builtins presumably have repeatably safe and fast hash functions */
  201. #if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
  202. (PyUnicode_CheckExact(key) || PyString_CheckExact(key) || PyLong_CheckExact(key)))) {
  203. value = PyDict_GetItemWithError(d, key);
  204. if (unlikely(!value)) {
  205. if (unlikely(PyErr_Occurred()))
  206. return NULL;
  207. if (unlikely(PyDict_SetItem(d, key, default_value) == -1))
  208. return NULL;
  209. value = default_value;
  210. }
  211. Py_INCREF(value);
  212. #else
  213. (PyString_CheckExact(key) || PyUnicode_CheckExact(key) || PyInt_CheckExact(key) || PyLong_CheckExact(key)))) {
  214. value = PyDict_GetItem(d, key);
  215. if (unlikely(!value)) {
  216. if (unlikely(PyDict_SetItem(d, key, default_value) == -1))
  217. return NULL;
  218. value = default_value;
  219. }
  220. Py_INCREF(value);
  221. #endif
  222. #endif
  223. } else {
  224. value = CALL_UNBOUND_METHOD(PyDict_Type, "setdefault", d, key, default_value);
  225. }
  226. return value;
  227. }
  228. /////////////// py_dict_clear.proto ///////////////
  229. #define __Pyx_PyDict_Clear(d) (PyDict_Clear(d), 0)
  230. /////////////// py_dict_pop.proto ///////////////
  231. static CYTHON_INLINE PyObject *__Pyx_PyDict_Pop(PyObject *d, PyObject *key, PyObject *default_value); /*proto*/
  232. /////////////// py_dict_pop ///////////////
  233. static CYTHON_INLINE PyObject *__Pyx_PyDict_Pop(PyObject *d, PyObject *key, PyObject *default_value) {
  234. #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX > 0x030600B3
  235. if ((1)) {
  236. return _PyDict_Pop(d, key, default_value);
  237. } else
  238. // avoid "function unused" warnings
  239. #endif
  240. if (default_value) {
  241. return CALL_UNBOUND_METHOD(PyDict_Type, "pop", d, key, default_value);
  242. } else {
  243. return CALL_UNBOUND_METHOD(PyDict_Type, "pop", d, key);
  244. }
  245. }
  246. /////////////// dict_iter.proto ///////////////
  247. static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* dict, int is_dict, PyObject* method_name,
  248. Py_ssize_t* p_orig_length, int* p_is_dict);
  249. static CYTHON_INLINE int __Pyx_dict_iter_next(PyObject* dict_or_iter, Py_ssize_t orig_length, Py_ssize_t* ppos,
  250. PyObject** pkey, PyObject** pvalue, PyObject** pitem, int is_dict);
  251. /////////////// dict_iter ///////////////
  252. //@requires: ObjectHandling.c::UnpackTuple2
  253. //@requires: ObjectHandling.c::IterFinish
  254. //@requires: ObjectHandling.c::PyObjectCallMethod0
  255. static CYTHON_INLINE PyObject* __Pyx_dict_iterator(PyObject* iterable, int is_dict, PyObject* method_name,
  256. Py_ssize_t* p_orig_length, int* p_source_is_dict) {
  257. is_dict = is_dict || likely(PyDict_CheckExact(iterable));
  258. *p_source_is_dict = is_dict;
  259. if (is_dict) {
  260. #if !CYTHON_COMPILING_IN_PYPY
  261. *p_orig_length = PyDict_Size(iterable);
  262. Py_INCREF(iterable);
  263. return iterable;
  264. #elif PY_MAJOR_VERSION >= 3
  265. // On PyPy3, we need to translate manually a few method names.
  266. // This logic is not needed on CPython thanks to the fast case above.
  267. static PyObject *py_items = NULL, *py_keys = NULL, *py_values = NULL;
  268. PyObject **pp = NULL;
  269. if (method_name) {
  270. const char *name = PyUnicode_AsUTF8(method_name);
  271. if (strcmp(name, "iteritems") == 0) pp = &py_items;
  272. else if (strcmp(name, "iterkeys") == 0) pp = &py_keys;
  273. else if (strcmp(name, "itervalues") == 0) pp = &py_values;
  274. if (pp) {
  275. if (!*pp) {
  276. *pp = PyUnicode_FromString(name + 4);
  277. if (!*pp)
  278. return NULL;
  279. }
  280. method_name = *pp;
  281. }
  282. }
  283. #endif
  284. }
  285. *p_orig_length = 0;
  286. if (method_name) {
  287. PyObject* iter;
  288. iterable = __Pyx_PyObject_CallMethod0(iterable, method_name);
  289. if (!iterable)
  290. return NULL;
  291. #if !CYTHON_COMPILING_IN_PYPY
  292. if (PyTuple_CheckExact(iterable) || PyList_CheckExact(iterable))
  293. return iterable;
  294. #endif
  295. iter = PyObject_GetIter(iterable);
  296. Py_DECREF(iterable);
  297. return iter;
  298. }
  299. return PyObject_GetIter(iterable);
  300. }
  301. static CYTHON_INLINE int __Pyx_dict_iter_next(
  302. PyObject* iter_obj, CYTHON_NCP_UNUSED Py_ssize_t orig_length, CYTHON_NCP_UNUSED Py_ssize_t* ppos,
  303. PyObject** pkey, PyObject** pvalue, PyObject** pitem, int source_is_dict) {
  304. PyObject* next_item;
  305. #if !CYTHON_COMPILING_IN_PYPY
  306. if (source_is_dict) {
  307. PyObject *key, *value;
  308. if (unlikely(orig_length != PyDict_Size(iter_obj))) {
  309. PyErr_SetString(PyExc_RuntimeError, "dictionary changed size during iteration");
  310. return -1;
  311. }
  312. if (unlikely(!PyDict_Next(iter_obj, ppos, &key, &value))) {
  313. return 0;
  314. }
  315. if (pitem) {
  316. PyObject* tuple = PyTuple_New(2);
  317. if (unlikely(!tuple)) {
  318. return -1;
  319. }
  320. Py_INCREF(key);
  321. Py_INCREF(value);
  322. PyTuple_SET_ITEM(tuple, 0, key);
  323. PyTuple_SET_ITEM(tuple, 1, value);
  324. *pitem = tuple;
  325. } else {
  326. if (pkey) {
  327. Py_INCREF(key);
  328. *pkey = key;
  329. }
  330. if (pvalue) {
  331. Py_INCREF(value);
  332. *pvalue = value;
  333. }
  334. }
  335. return 1;
  336. } else if (PyTuple_CheckExact(iter_obj)) {
  337. Py_ssize_t pos = *ppos;
  338. if (unlikely(pos >= PyTuple_GET_SIZE(iter_obj))) return 0;
  339. *ppos = pos + 1;
  340. next_item = PyTuple_GET_ITEM(iter_obj, pos);
  341. Py_INCREF(next_item);
  342. } else if (PyList_CheckExact(iter_obj)) {
  343. Py_ssize_t pos = *ppos;
  344. if (unlikely(pos >= PyList_GET_SIZE(iter_obj))) return 0;
  345. *ppos = pos + 1;
  346. next_item = PyList_GET_ITEM(iter_obj, pos);
  347. Py_INCREF(next_item);
  348. } else
  349. #endif
  350. {
  351. next_item = PyIter_Next(iter_obj);
  352. if (unlikely(!next_item)) {
  353. return __Pyx_IterFinish();
  354. }
  355. }
  356. if (pitem) {
  357. *pitem = next_item;
  358. } else if (pkey && pvalue) {
  359. if (__Pyx_unpack_tuple2(next_item, pkey, pvalue, source_is_dict, source_is_dict, 1))
  360. return -1;
  361. } else if (pkey) {
  362. *pkey = next_item;
  363. } else {
  364. *pvalue = next_item;
  365. }
  366. return 1;
  367. }
  368. /////////////// set_iter.proto ///////////////
  369. static CYTHON_INLINE PyObject* __Pyx_set_iterator(PyObject* iterable, int is_set,
  370. Py_ssize_t* p_orig_length, int* p_source_is_set); /*proto*/
  371. static CYTHON_INLINE int __Pyx_set_iter_next(
  372. PyObject* iter_obj, Py_ssize_t orig_length,
  373. Py_ssize_t* ppos, PyObject **value,
  374. int source_is_set); /*proto*/
  375. /////////////// set_iter ///////////////
  376. //@requires: ObjectHandling.c::IterFinish
  377. static CYTHON_INLINE PyObject* __Pyx_set_iterator(PyObject* iterable, int is_set,
  378. Py_ssize_t* p_orig_length, int* p_source_is_set) {
  379. #if CYTHON_COMPILING_IN_CPYTHON
  380. is_set = is_set || likely(PySet_CheckExact(iterable) || PyFrozenSet_CheckExact(iterable));
  381. *p_source_is_set = is_set;
  382. if (unlikely(!is_set))
  383. return PyObject_GetIter(iterable);
  384. *p_orig_length = PySet_Size(iterable);
  385. Py_INCREF(iterable);
  386. return iterable;
  387. #else
  388. (void)is_set;
  389. *p_source_is_set = 0;
  390. *p_orig_length = 0;
  391. return PyObject_GetIter(iterable);
  392. #endif
  393. }
  394. static CYTHON_INLINE int __Pyx_set_iter_next(
  395. PyObject* iter_obj, Py_ssize_t orig_length,
  396. Py_ssize_t* ppos, PyObject **value,
  397. int source_is_set) {
  398. if (!CYTHON_COMPILING_IN_CPYTHON || unlikely(!source_is_set)) {
  399. *value = PyIter_Next(iter_obj);
  400. if (unlikely(!*value)) {
  401. return __Pyx_IterFinish();
  402. }
  403. (void)orig_length;
  404. (void)ppos;
  405. return 0;
  406. }
  407. #if CYTHON_COMPILING_IN_CPYTHON
  408. if (unlikely(PySet_GET_SIZE(iter_obj) != orig_length)) {
  409. PyErr_SetString(
  410. PyExc_RuntimeError,
  411. "set changed size during iteration");
  412. return -1;
  413. }
  414. {
  415. Py_hash_t hash;
  416. int ret = _PySet_NextEntry(iter_obj, ppos, value, &hash);
  417. // CPython does not raise errors here, only if !isinstance(iter_obj, set/frozenset)
  418. assert (ret != -1);
  419. if (likely(ret)) {
  420. Py_INCREF(*value);
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. #endif
  426. }
  427. /////////////// py_set_discard_unhashable ///////////////
  428. //@requires: Builtins.c::pyfrozenset_new
  429. static int __Pyx_PySet_DiscardUnhashable(PyObject *set, PyObject *key) {
  430. PyObject *tmpkey;
  431. int rv;
  432. if (likely(!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)))
  433. return -1;
  434. PyErr_Clear();
  435. tmpkey = __Pyx_PyFrozenSet_New(key);
  436. if (tmpkey == NULL)
  437. return -1;
  438. rv = PySet_Discard(set, tmpkey);
  439. Py_DECREF(tmpkey);
  440. return rv;
  441. }
  442. /////////////// py_set_discard.proto ///////////////
  443. static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key); /*proto*/
  444. /////////////// py_set_discard ///////////////
  445. //@requires: py_set_discard_unhashable
  446. static CYTHON_INLINE int __Pyx_PySet_Discard(PyObject *set, PyObject *key) {
  447. int found = PySet_Discard(set, key);
  448. // Convert *key* to frozenset if necessary
  449. if (unlikely(found < 0)) {
  450. found = __Pyx_PySet_DiscardUnhashable(set, key);
  451. }
  452. // note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
  453. return found;
  454. }
  455. /////////////// py_set_remove.proto ///////////////
  456. static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key); /*proto*/
  457. /////////////// py_set_remove ///////////////
  458. //@requires: py_set_discard_unhashable
  459. static int __Pyx_PySet_RemoveNotFound(PyObject *set, PyObject *key, int found) {
  460. // Convert *key* to frozenset if necessary
  461. if (unlikely(found < 0)) {
  462. found = __Pyx_PySet_DiscardUnhashable(set, key);
  463. }
  464. if (likely(found == 0)) {
  465. // Not found
  466. PyObject *tup;
  467. tup = PyTuple_Pack(1, key);
  468. if (!tup)
  469. return -1;
  470. PyErr_SetObject(PyExc_KeyError, tup);
  471. Py_DECREF(tup);
  472. return -1;
  473. }
  474. // note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
  475. return found;
  476. }
  477. static CYTHON_INLINE int __Pyx_PySet_Remove(PyObject *set, PyObject *key) {
  478. int found = PySet_Discard(set, key);
  479. if (unlikely(found != 1)) {
  480. // note: returns -1 on error, 0 (not found) or 1 (found) otherwise => error check for -1 or < 0 works
  481. return __Pyx_PySet_RemoveNotFound(set, key, found);
  482. }
  483. return 0;
  484. }
  485. /////////////// unicode_iter.proto ///////////////
  486. static CYTHON_INLINE int __Pyx_init_unicode_iteration(
  487. PyObject* ustring, Py_ssize_t *length, void** data, int *kind); /* proto */
  488. /////////////// unicode_iter ///////////////
  489. static CYTHON_INLINE int __Pyx_init_unicode_iteration(
  490. PyObject* ustring, Py_ssize_t *length, void** data, int *kind) {
  491. #if CYTHON_PEP393_ENABLED
  492. if (unlikely(__Pyx_PyUnicode_READY(ustring) < 0)) return -1;
  493. *kind = PyUnicode_KIND(ustring);
  494. *length = PyUnicode_GET_LENGTH(ustring);
  495. *data = PyUnicode_DATA(ustring);
  496. #else
  497. *kind = 0;
  498. *length = PyUnicode_GET_SIZE(ustring);
  499. *data = (void*)PyUnicode_AS_UNICODE(ustring);
  500. #endif
  501. return 0;
  502. }
  503. /////////////// pyobject_as_double.proto ///////////////
  504. static double __Pyx__PyObject_AsDouble(PyObject* obj); /* proto */
  505. #if CYTHON_COMPILING_IN_PYPY
  506. #define __Pyx_PyObject_AsDouble(obj) \
  507. (likely(PyFloat_CheckExact(obj)) ? PyFloat_AS_DOUBLE(obj) : \
  508. likely(PyInt_CheckExact(obj)) ? \
  509. PyFloat_AsDouble(obj) : __Pyx__PyObject_AsDouble(obj))
  510. #else
  511. #define __Pyx_PyObject_AsDouble(obj) \
  512. ((likely(PyFloat_CheckExact(obj))) ? \
  513. PyFloat_AS_DOUBLE(obj) : __Pyx__PyObject_AsDouble(obj))
  514. #endif
  515. /////////////// pyobject_as_double ///////////////
  516. static double __Pyx__PyObject_AsDouble(PyObject* obj) {
  517. PyObject* float_value;
  518. #if !CYTHON_USE_TYPE_SLOTS
  519. float_value = PyNumber_Float(obj); if (0) goto bad;
  520. #else
  521. PyNumberMethods *nb = Py_TYPE(obj)->tp_as_number;
  522. if (likely(nb) && likely(nb->nb_float)) {
  523. float_value = nb->nb_float(obj);
  524. if (likely(float_value) && unlikely(!PyFloat_Check(float_value))) {
  525. PyErr_Format(PyExc_TypeError,
  526. "__float__ returned non-float (type %.200s)",
  527. Py_TYPE(float_value)->tp_name);
  528. Py_DECREF(float_value);
  529. goto bad;
  530. }
  531. } else if (PyUnicode_CheckExact(obj) || PyBytes_CheckExact(obj)) {
  532. #if PY_MAJOR_VERSION >= 3
  533. float_value = PyFloat_FromString(obj);
  534. #else
  535. float_value = PyFloat_FromString(obj, 0);
  536. #endif
  537. } else {
  538. PyObject* args = PyTuple_New(1);
  539. if (unlikely(!args)) goto bad;
  540. PyTuple_SET_ITEM(args, 0, obj);
  541. float_value = PyObject_Call((PyObject*)&PyFloat_Type, args, 0);
  542. PyTuple_SET_ITEM(args, 0, 0);
  543. Py_DECREF(args);
  544. }
  545. #endif
  546. if (likely(float_value)) {
  547. double value = PyFloat_AS_DOUBLE(float_value);
  548. Py_DECREF(float_value);
  549. return value;
  550. }
  551. bad:
  552. return (double)-1;
  553. }
  554. /////////////// PyNumberPow2.proto ///////////////
  555. #define __Pyx_PyNumber_InPlacePowerOf2(a, b, c) __Pyx__PyNumber_PowerOf2(a, b, c, 1)
  556. #define __Pyx_PyNumber_PowerOf2(a, b, c) __Pyx__PyNumber_PowerOf2(a, b, c, 0)
  557. static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace); /*proto*/
  558. /////////////// PyNumberPow2 ///////////////
  559. static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace) {
  560. // in CPython, 1<<N is substantially faster than 2**N
  561. // see http://bugs.python.org/issue21420
  562. #if !CYTHON_COMPILING_IN_PYPY
  563. Py_ssize_t shiftby;
  564. #if PY_MAJOR_VERSION < 3
  565. if (likely(PyInt_CheckExact(exp))) {
  566. shiftby = PyInt_AS_LONG(exp);
  567. } else
  568. #endif
  569. if (likely(PyLong_CheckExact(exp))) {
  570. #if CYTHON_USE_PYLONG_INTERNALS
  571. const Py_ssize_t size = Py_SIZE(exp);
  572. // tuned to optimise branch prediction
  573. if (likely(size == 1)) {
  574. shiftby = ((PyLongObject*)exp)->ob_digit[0];
  575. } else if (size == 0) {
  576. return PyInt_FromLong(1L);
  577. } else if (unlikely(size < 0)) {
  578. goto fallback;
  579. } else {
  580. shiftby = PyLong_AsSsize_t(exp);
  581. }
  582. #else
  583. shiftby = PyLong_AsSsize_t(exp);
  584. #endif
  585. } else {
  586. goto fallback;
  587. }
  588. if (likely(shiftby >= 0)) {
  589. if ((size_t)shiftby <= sizeof(long) * 8 - 2) {
  590. long value = 1L << shiftby;
  591. return PyInt_FromLong(value);
  592. #ifdef HAVE_LONG_LONG
  593. } else if ((size_t)shiftby <= sizeof(unsigned PY_LONG_LONG) * 8 - 1) {
  594. unsigned PY_LONG_LONG value = ((unsigned PY_LONG_LONG)1) << shiftby;
  595. return PyLong_FromUnsignedLongLong(value);
  596. #endif
  597. } else {
  598. PyObject *one = PyInt_FromLong(1L);
  599. if (unlikely(!one)) return NULL;
  600. return PyNumber_Lshift(one, exp);
  601. }
  602. } else if (shiftby == -1 && PyErr_Occurred()) {
  603. PyErr_Clear();
  604. }
  605. fallback:
  606. #endif
  607. return (inplace ? PyNumber_InPlacePower : PyNumber_Power)(two, exp, none);
  608. }
  609. /////////////// PyIntBinop.proto ///////////////
  610. #if !CYTHON_COMPILING_IN_PYPY
  611. static PyObject* __Pyx_PyInt_{{op}}{{order}}(PyObject *op1, PyObject *op2, long intval, int inplace); /*proto*/
  612. #else
  613. #define __Pyx_PyInt_{{op}}{{order}}(op1, op2, intval, inplace) \
  614. {{if op in ('Eq', 'Ne')}}PyObject_RichCompare(op1, op2, Py_{{op.upper()}})
  615. {{else}}(inplace ? PyNumber_InPlace{{op}}(op1, op2) : PyNumber_{{op}}(op1, op2))
  616. {{endif}}
  617. #endif
  618. /////////////// PyIntBinop ///////////////
  619. #if !CYTHON_COMPILING_IN_PYPY
  620. {{py: from Cython.Utility import pylong_join }}
  621. {{py: pyval, ival = ('op2', 'b') if order == 'CObj' else ('op1', 'a') }}
  622. {{py: slot_name = {'TrueDivide': 'true_divide', 'FloorDivide': 'floor_divide'}.get(op, op.lower()) }}
  623. {{py:
  624. c_op = {
  625. 'Add': '+', 'Subtract': '-', 'Remainder': '%', 'TrueDivide': '/', 'FloorDivide': '/',
  626. 'Or': '|', 'Xor': '^', 'And': '&', 'Rshift': '>>', 'Lshift': '<<',
  627. 'Eq': '==', 'Ne': '!=',
  628. }[op]
  629. }}
  630. static PyObject* __Pyx_PyInt_{{op}}{{order}}(PyObject *op1, PyObject *op2, CYTHON_UNUSED long intval, CYTHON_UNUSED int inplace) {
  631. {{if op in ('Eq', 'Ne')}}
  632. if (op1 == op2) {
  633. Py_RETURN_{{'TRUE' if op == 'Eq' else 'FALSE'}};
  634. }
  635. {{endif}}
  636. #if PY_MAJOR_VERSION < 3
  637. if (likely(PyInt_CheckExact({{pyval}}))) {
  638. const long {{'a' if order == 'CObj' else 'b'}} = intval;
  639. {{if c_op in '+-%' or op == 'FloorDivide'}}
  640. long x;
  641. {{endif}}
  642. long {{ival}} = PyInt_AS_LONG({{pyval}});
  643. {{if op in ('Eq', 'Ne')}}
  644. if (a {{c_op}} b) {
  645. Py_RETURN_TRUE;
  646. } else {
  647. Py_RETURN_FALSE;
  648. }
  649. {{elif c_op in '+-'}}
  650. // adapted from intobject.c in Py2.7:
  651. // casts in the line below avoid undefined behaviour on overflow
  652. x = (long)((unsigned long)a {{c_op}} b);
  653. if (likely((x^a) >= 0 || (x^{{ '~' if op == 'Subtract' else '' }}b) >= 0))
  654. return PyInt_FromLong(x);
  655. return PyLong_Type.tp_as_number->nb_{{slot_name}}(op1, op2);
  656. {{elif c_op == '%'}}
  657. // see ExprNodes.py :: mod_int_utility_code
  658. x = a % b;
  659. x += ((x != 0) & ((x ^ b) < 0)) * b;
  660. return PyInt_FromLong(x);
  661. {{elif op == 'TrueDivide'}}
  662. if (8 * sizeof(long) <= 53 || likely(labs({{ival}}) <= ((PY_LONG_LONG)1 << 53))) {
  663. return PyFloat_FromDouble((double)a / (double)b);
  664. }
  665. // let Python do the rounding
  666. return PyInt_Type.tp_as_number->nb_{{slot_name}}(op1, op2);
  667. {{elif op == 'FloorDivide'}}
  668. // INT_MIN / -1 is the only case that overflows
  669. if (unlikely(b == -1 && ((unsigned long)a) == 0-(unsigned long)a))
  670. return PyInt_Type.tp_as_number->nb_{{slot_name}}(op1, op2);
  671. else {
  672. long q, r;
  673. // see ExprNodes.py :: div_int_utility_code
  674. q = a / b;
  675. r = a - q*b;
  676. q -= ((r != 0) & ((r ^ b) < 0));
  677. x = q;
  678. }
  679. return PyInt_FromLong(x);
  680. {{elif op == 'Lshift'}}
  681. if (likely(b < (long) (sizeof(long)*8) && a == (a << b) >> b) || !a) {
  682. return PyInt_FromLong(a {{c_op}} b);
  683. }
  684. {{else}}
  685. // other operations are safe, no overflow
  686. return PyInt_FromLong(a {{c_op}} b);
  687. {{endif}}
  688. }
  689. #endif
  690. #if CYTHON_USE_PYLONG_INTERNALS
  691. if (likely(PyLong_CheckExact({{pyval}}))) {
  692. const long {{'a' if order == 'CObj' else 'b'}} = intval;
  693. long {{ival}}{{if op not in ('Eq', 'Ne')}}, x{{endif}};
  694. {{if op not in ('Eq', 'Ne', 'TrueDivide')}}
  695. #ifdef HAVE_LONG_LONG
  696. const PY_LONG_LONG ll{{'a' if order == 'CObj' else 'b'}} = intval;
  697. PY_LONG_LONG ll{{ival}}, llx;
  698. #endif
  699. {{endif}}
  700. const digit* digits = ((PyLongObject*){{pyval}})->ob_digit;
  701. const Py_ssize_t size = Py_SIZE({{pyval}});
  702. // handle most common case first to avoid indirect branch and optimise branch prediction
  703. if (likely(__Pyx_sst_abs(size) <= 1)) {
  704. {{ival}} = likely(size) ? digits[0] : 0;
  705. if (size == -1) {{ival}} = -{{ival}};
  706. } else {
  707. switch (size) {
  708. {{for _size in range(2, 5)}}
  709. {{for _case in (-_size, _size)}}
  710. case {{_case}}:
  711. if (8 * sizeof(long) - 1 > {{_size}} * PyLong_SHIFT{{if op == 'TrueDivide'}} && {{_size-1}} * PyLong_SHIFT < 53{{endif}}) {
  712. {{ival}} = {{'-' if _case < 0 else ''}}(long) {{pylong_join(_size, 'digits')}};
  713. break;
  714. {{if op not in ('Eq', 'Ne', 'TrueDivide')}}
  715. #ifdef HAVE_LONG_LONG
  716. } else if (8 * sizeof(PY_LONG_LONG) - 1 > {{_size}} * PyLong_SHIFT) {
  717. ll{{ival}} = {{'-' if _case < 0 else ''}}(PY_LONG_LONG) {{pylong_join(_size, 'digits', 'unsigned PY_LONG_LONG')}};
  718. goto long_long;
  719. #endif
  720. {{endif}}
  721. }
  722. // if size doesn't fit into a long or PY_LONG_LONG anymore, fall through to default
  723. CYTHON_FALLTHROUGH;
  724. {{endfor}}
  725. {{endfor}}
  726. {{if op in ('Eq', 'Ne')}}
  727. #if PyLong_SHIFT < 30 && PyLong_SHIFT != 15
  728. // unusual setup - your fault
  729. default: return PyLong_Type.tp_richcompare({{'op1, op2' if order == 'ObjC' else 'op2, op1'}}, Py_{{op.upper()}});
  730. #else
  731. // too large for the long values we allow => definitely not equal
  732. default: Py_RETURN_{{'FALSE' if op == 'Eq' else 'TRUE'}};
  733. #endif
  734. {{else}}
  735. default: return PyLong_Type.tp_as_number->nb_{{slot_name}}(op1, op2);
  736. {{endif}}
  737. }
  738. }
  739. {{if op in ('Eq', 'Ne')}}
  740. if (a {{c_op}} b) {
  741. Py_RETURN_TRUE;
  742. } else {
  743. Py_RETURN_FALSE;
  744. }
  745. {{else}}
  746. {{if c_op == '%'}}
  747. // see ExprNodes.py :: mod_int_utility_code
  748. x = a % b;
  749. x += ((x != 0) & ((x ^ b) < 0)) * b;
  750. {{elif op == 'TrueDivide'}}
  751. if ((8 * sizeof(long) <= 53 || likely(labs({{ival}}) <= ((PY_LONG_LONG)1 << 53)))
  752. || __Pyx_sst_abs(size) <= 52 / PyLong_SHIFT) {
  753. return PyFloat_FromDouble((double)a / (double)b);
  754. }
  755. return PyLong_Type.tp_as_number->nb_{{slot_name}}(op1, op2);
  756. {{elif op == 'FloorDivide'}}
  757. {
  758. long q, r;
  759. // see ExprNodes.py :: div_int_utility_code
  760. q = a / b;
  761. r = a - q*b;
  762. q -= ((r != 0) & ((r ^ b) < 0));
  763. x = q;
  764. }
  765. {{else}}
  766. x = a {{c_op}} b;
  767. {{if op == 'Lshift'}}
  768. #ifdef HAVE_LONG_LONG
  769. if (unlikely(!(b < (long) (sizeof(long)*8) && a == x >> b)) && a) {
  770. ll{{ival}} = {{ival}};
  771. goto long_long;
  772. }
  773. #else
  774. if (likely(b < (long) (sizeof(long)*8) && a == x >> b) || !a) /* execute return statement below */
  775. #endif
  776. {{endif}}
  777. {{endif}}
  778. return PyLong_FromLong(x);
  779. {{if op != 'TrueDivide'}}
  780. #ifdef HAVE_LONG_LONG
  781. long_long:
  782. {{if c_op == '%'}}
  783. // see ExprNodes.py :: mod_int_utility_code
  784. llx = lla % llb;
  785. llx += ((llx != 0) & ((llx ^ llb) < 0)) * llb;
  786. {{elif op == 'FloorDivide'}}
  787. {
  788. PY_LONG_LONG q, r;
  789. // see ExprNodes.py :: div_int_utility_code
  790. q = lla / llb;
  791. r = lla - q*llb;
  792. q -= ((r != 0) & ((r ^ llb) < 0));
  793. llx = q;
  794. }
  795. {{else}}
  796. llx = lla {{c_op}} llb;
  797. {{if op == 'Lshift'}}
  798. if (likely(lla == llx >> llb)) /* then execute 'return' below */
  799. {{endif}}
  800. {{endif}}
  801. return PyLong_FromLongLong(llx);
  802. #endif
  803. {{endif}}{{# if op != 'TrueDivide' #}}
  804. {{endif}}{{# if op in ('Eq', 'Ne') #}}
  805. }
  806. #endif
  807. {{if c_op in '+-' or op in ('TrueDivide', 'Eq', 'Ne')}}
  808. if (PyFloat_CheckExact({{pyval}})) {
  809. const long {{'a' if order == 'CObj' else 'b'}} = intval;
  810. double {{ival}} = PyFloat_AS_DOUBLE({{pyval}});
  811. {{if op in ('Eq', 'Ne')}}
  812. if ((double)a {{c_op}} (double)b) {
  813. Py_RETURN_TRUE;
  814. } else {
  815. Py_RETURN_FALSE;
  816. }
  817. {{else}}
  818. double result;
  819. // copied from floatobject.c in Py3.5:
  820. PyFPE_START_PROTECT("{{op.lower() if not op.endswith('Divide') else 'divide'}}", return NULL)
  821. result = ((double)a) {{c_op}} (double)b;
  822. PyFPE_END_PROTECT(result)
  823. return PyFloat_FromDouble(result);
  824. {{endif}}
  825. }
  826. {{endif}}
  827. {{if op in ('Eq', 'Ne')}}
  828. return PyObject_RichCompare(op1, op2, Py_{{op.upper()}});
  829. {{else}}
  830. return (inplace ? PyNumber_InPlace{{op}} : PyNumber_{{op}})(op1, op2);
  831. {{endif}}
  832. }
  833. #endif
  834. /////////////// PyFloatBinop.proto ///////////////
  835. #if !CYTHON_COMPILING_IN_PYPY
  836. static PyObject* __Pyx_PyFloat_{{op}}{{order}}(PyObject *op1, PyObject *op2, double floatval, int inplace); /*proto*/
  837. #else
  838. #define __Pyx_PyFloat_{{op}}{{order}}(op1, op2, floatval, inplace) \
  839. {{if op in ('Eq', 'Ne')}}PyObject_RichCompare(op1, op2, Py_{{op.upper()}})
  840. {{elif op == 'Divide'}}((inplace ? __Pyx_PyNumber_InPlaceDivide(op1, op2) : __Pyx_PyNumber_Divide(op1, op2)))
  841. {{else}}(inplace ? PyNumber_InPlace{{op}}(op1, op2) : PyNumber_{{op}}(op1, op2))
  842. {{endif}}
  843. #endif
  844. /////////////// PyFloatBinop ///////////////
  845. #if !CYTHON_COMPILING_IN_PYPY
  846. {{py: from Cython.Utility import pylong_join }}
  847. {{py: pyval, fval = ('op2', 'b') if order == 'CObj' else ('op1', 'a') }}
  848. {{py:
  849. c_op = {
  850. 'Add': '+', 'Subtract': '-', 'TrueDivide': '/', 'Divide': '/', 'Remainder': '%',
  851. 'Eq': '==', 'Ne': '!=',
  852. }[op]
  853. }}
  854. static PyObject* __Pyx_PyFloat_{{op}}{{order}}(PyObject *op1, PyObject *op2, double floatval, CYTHON_UNUSED int inplace) {
  855. const double {{'a' if order == 'CObj' else 'b'}} = floatval;
  856. double {{fval}}{{if op not in ('Eq', 'Ne')}}, result{{endif}};
  857. {{if op in ('Eq', 'Ne')}}
  858. if (op1 == op2) {
  859. Py_RETURN_{{'TRUE' if op == 'Eq' else 'FALSE'}};
  860. }
  861. {{endif}}
  862. if (likely(PyFloat_CheckExact({{pyval}}))) {
  863. {{fval}} = PyFloat_AS_DOUBLE({{pyval}});
  864. } else
  865. #if PY_MAJOR_VERSION < 3
  866. if (likely(PyInt_CheckExact({{pyval}}))) {
  867. {{fval}} = (double) PyInt_AS_LONG({{pyval}});
  868. } else
  869. #endif
  870. if (likely(PyLong_CheckExact({{pyval}}))) {
  871. #if CYTHON_USE_PYLONG_INTERNALS
  872. const digit* digits = ((PyLongObject*){{pyval}})->ob_digit;
  873. const Py_ssize_t size = Py_SIZE({{pyval}});
  874. switch (size) {
  875. case 0: {{fval}} = 0.0; break;
  876. case -1: {{fval}} = -(double) digits[0]; break;
  877. case 1: {{fval}} = (double) digits[0]; break;
  878. {{for _size in (2, 3, 4)}}
  879. case -{{_size}}:
  880. case {{_size}}:
  881. if (8 * sizeof(unsigned long) > {{_size}} * PyLong_SHIFT && ((8 * sizeof(unsigned long) < 53) || ({{_size-1}} * PyLong_SHIFT < 53))) {
  882. {{fval}} = (double) {{pylong_join(_size, 'digits')}};
  883. // let CPython do its own float rounding from 2**53 on (max. consecutive integer in double float)
  884. if ((8 * sizeof(unsigned long) < 53) || ({{_size}} * PyLong_SHIFT < 53) || ({{fval}} < (double) ((PY_LONG_LONG)1 << 53))) {
  885. if (size == {{-_size}})
  886. {{fval}} = -{{fval}};
  887. break;
  888. }
  889. }
  890. // Fall through if size doesn't fit safely into a double anymore.
  891. // It may not be obvious that this is a safe fall-through given the "fval < 2**53"
  892. // check above. However, the number of digits that CPython uses for a given PyLong
  893. // value is minimal, and together with the "(size-1) * SHIFT < 53" check above,
  894. // this should make it safe.
  895. CYTHON_FALLTHROUGH;
  896. {{endfor}}
  897. default:
  898. #else
  899. {
  900. #endif
  901. {{if op in ('Eq', 'Ne')}}
  902. return PyFloat_Type.tp_richcompare({{'op1, op2' if order == 'CObj' else 'op2, op1'}}, Py_{{op.upper()}});
  903. {{else}}
  904. {{fval}} = PyLong_AsDouble({{pyval}});
  905. if (unlikely({{fval}} == -1.0 && PyErr_Occurred())) return NULL;
  906. {{endif}}
  907. }
  908. } else {
  909. {{if op in ('Eq', 'Ne')}}
  910. return PyObject_RichCompare(op1, op2, Py_{{op.upper()}});
  911. {{elif op == 'Divide'}}
  912. return (inplace ? __Pyx_PyNumber_InPlaceDivide(op1, op2) : __Pyx_PyNumber_Divide(op1, op2));
  913. {{else}}
  914. return (inplace ? PyNumber_InPlace{{op}} : PyNumber_{{op}})(op1, op2);
  915. {{endif}}
  916. }
  917. {{if op in ('Eq', 'Ne')}}
  918. if (a {{c_op}} b) {
  919. Py_RETURN_TRUE;
  920. } else {
  921. Py_RETURN_FALSE;
  922. }
  923. {{else}}
  924. // copied from floatobject.c in Py3.5:
  925. PyFPE_START_PROTECT("{{op.lower() if not op.endswith('Divide') else 'divide'}}", return NULL)
  926. {{if c_op == '%'}}
  927. result = fmod(a, b);
  928. if (result)
  929. result += ((result < 0) ^ (b < 0)) * b;
  930. else
  931. result = copysign(0.0, b);
  932. {{else}}
  933. result = a {{c_op}} b;
  934. {{endif}}
  935. PyFPE_END_PROTECT(result)
  936. return PyFloat_FromDouble(result);
  937. {{endif}}
  938. }
  939. #endif