Overflow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. These functions provide integer arithmetic with integer checking. They do not
  3. actually raise an exception when an overflow is detected, but rather set a bit
  4. in the overflow parameter. (This parameter may be re-used across several
  5. arithmetic operations, so should be or-ed rather than assigned to.)
  6. The implementation is divided into two parts, the signed and unsigned basecases,
  7. which is where the magic happens, and a generic template matching a specific
  8. type to an implementation based on its (c-compile-time) size and signedness.
  9. When possible, branching is avoided, and preference is given to speed over
  10. accuracy (a low rate of falsely "detected" overflows are acceptable,
  11. undetected overflows are not).
  12. TODO: Hook up checking.
  13. TODO: Conditionally support 128-bit with intmax_t?
  14. */
  15. /////////////// Common.proto ///////////////
  16. static int __Pyx_check_twos_complement(void) {
  17. if ((-1 != ~0)) {
  18. PyErr_SetString(PyExc_RuntimeError, "Two's complement required for overflow checks.");
  19. return 1;
  20. } else if ((sizeof(short) == sizeof(int))) {
  21. PyErr_SetString(PyExc_RuntimeError, "sizeof(short) < sizeof(int) required for overflow checks.");
  22. return 1;
  23. } else {
  24. return 0;
  25. }
  26. }
  27. #define __PYX_IS_UNSIGNED(type) ((((type) -1) > 0))
  28. #define __PYX_SIGN_BIT(type) ((((unsigned type) 1) << (sizeof(type) * 8 - 1)))
  29. #define __PYX_HALF_MAX(type) ((((type) 1) << (sizeof(type) * 8 - 2)))
  30. #define __PYX_MIN(type) ((__PYX_IS_UNSIGNED(type) ? (type) 0 : 0 - __PYX_HALF_MAX(type) - __PYX_HALF_MAX(type)))
  31. #define __PYX_MAX(type) ((~__PYX_MIN(type)))
  32. #define __Pyx_add_no_overflow(a, b, overflow) ((a) + (b))
  33. #define __Pyx_add_const_no_overflow(a, b, overflow) ((a) + (b))
  34. #define __Pyx_sub_no_overflow(a, b, overflow) ((a) - (b))
  35. #define __Pyx_sub_const_no_overflow(a, b, overflow) ((a) - (b))
  36. #define __Pyx_mul_no_overflow(a, b, overflow) ((a) * (b))
  37. #define __Pyx_mul_const_no_overflow(a, b, overflow) ((a) * (b))
  38. #define __Pyx_div_no_overflow(a, b, overflow) ((a) / (b))
  39. #define __Pyx_div_const_no_overflow(a, b, overflow) ((a) / (b))
  40. /////////////// Common.init ///////////////
  41. //@substitute: naming
  42. // FIXME: Propagate the error here instead of just printing it.
  43. if (unlikely(__Pyx_check_twos_complement())) {
  44. PyErr_WriteUnraisable($module_cname);
  45. }
  46. /////////////// BaseCaseUnsigned.proto ///////////////
  47. static CYTHON_INLINE {{UINT}} __Pyx_add_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow);
  48. static CYTHON_INLINE {{UINT}} __Pyx_sub_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow);
  49. static CYTHON_INLINE {{UINT}} __Pyx_mul_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow);
  50. static CYTHON_INLINE {{UINT}} __Pyx_div_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow);
  51. // Use these when b is known at compile time.
  52. #define __Pyx_add_const_{{NAME}}_checking_overflow __Pyx_add_{{NAME}}_checking_overflow
  53. #define __Pyx_sub_const_{{NAME}}_checking_overflow __Pyx_sub_{{NAME}}_checking_overflow
  54. static CYTHON_INLINE {{UINT}} __Pyx_mul_const_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} constant, int *overflow);
  55. #define __Pyx_div_const_{{NAME}}_checking_overflow __Pyx_div_{{NAME}}_checking_overflow
  56. /////////////// BaseCaseUnsigned ///////////////
  57. static CYTHON_INLINE {{UINT}} __Pyx_add_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow) {
  58. {{UINT}} r = a + b;
  59. *overflow |= r < a;
  60. return r;
  61. }
  62. static CYTHON_INLINE {{UINT}} __Pyx_sub_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow) {
  63. {{UINT}} r = a - b;
  64. *overflow |= r > a;
  65. return r;
  66. }
  67. static CYTHON_INLINE {{UINT}} __Pyx_mul_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow) {
  68. if ((sizeof({{UINT}}) < sizeof(unsigned long))) {
  69. unsigned long big_r = ((unsigned long) a) * ((unsigned long) b);
  70. {{UINT}} r = ({{UINT}}) big_r;
  71. *overflow |= big_r != r;
  72. return r;
  73. #ifdef HAVE_LONG_LONG
  74. } else if ((sizeof({{UINT}}) < sizeof(unsigned PY_LONG_LONG))) {
  75. unsigned PY_LONG_LONG big_r = ((unsigned PY_LONG_LONG) a) * ((unsigned PY_LONG_LONG) b);
  76. {{UINT}} r = ({{UINT}}) big_r;
  77. *overflow |= big_r != r;
  78. return r;
  79. #endif
  80. } else {
  81. {{UINT}} prod = a * b;
  82. double dprod = ((double) a) * ((double) b);
  83. // Overflow results in an error of at least 2^sizeof(UINT),
  84. // whereas rounding represents an error on the order of 2^(sizeof(UINT)-53).
  85. *overflow |= fabs(dprod - prod) > (__PYX_MAX({{UINT}}) / 2);
  86. return prod;
  87. }
  88. }
  89. static CYTHON_INLINE {{UINT}} __Pyx_mul_const_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow) {
  90. if (b > 1) {
  91. *overflow |= a > __PYX_MAX({{UINT}}) / b;
  92. }
  93. return a * b;
  94. }
  95. static CYTHON_INLINE {{UINT}} __Pyx_div_{{NAME}}_checking_overflow({{UINT}} a, {{UINT}} b, int *overflow) {
  96. if (b == 0) {
  97. *overflow |= 1;
  98. return 0;
  99. }
  100. return a / b;
  101. }
  102. /////////////// BaseCaseSigned.proto ///////////////
  103. static CYTHON_INLINE {{INT}} __Pyx_add_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  104. static CYTHON_INLINE {{INT}} __Pyx_sub_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  105. static CYTHON_INLINE {{INT}} __Pyx_mul_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  106. static CYTHON_INLINE {{INT}} __Pyx_div_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  107. // Use when b is known at compile time.
  108. static CYTHON_INLINE {{INT}} __Pyx_add_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  109. static CYTHON_INLINE {{INT}} __Pyx_sub_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow);
  110. static CYTHON_INLINE {{INT}} __Pyx_mul_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} constant, int *overflow);
  111. #define __Pyx_div_const_{{NAME}}_checking_overflow __Pyx_div_{{NAME}}_checking_overflow
  112. /////////////// BaseCaseSigned ///////////////
  113. static CYTHON_INLINE {{INT}} __Pyx_add_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  114. if ((sizeof({{INT}}) < sizeof(long))) {
  115. long big_r = ((long) a) + ((long) b);
  116. {{INT}} r = ({{INT}}) big_r;
  117. *overflow |= big_r != r;
  118. return r;
  119. #ifdef HAVE_LONG_LONG
  120. } else if ((sizeof({{INT}}) < sizeof(PY_LONG_LONG))) {
  121. PY_LONG_LONG big_r = ((PY_LONG_LONG) a) + ((PY_LONG_LONG) b);
  122. {{INT}} r = ({{INT}}) big_r;
  123. *overflow |= big_r != r;
  124. return r;
  125. #endif
  126. } else {
  127. // Signed overflow undefined, but unsigned overflow is well defined.
  128. {{INT}} r = ({{INT}}) ((unsigned {{INT}}) a + (unsigned {{INT}}) b);
  129. // Overflow happened if the operands have the same sign, but the result
  130. // has opposite sign.
  131. // sign(a) == sign(b) != sign(r)
  132. {{INT}} sign_a = __PYX_SIGN_BIT({{INT}}) & a;
  133. {{INT}} sign_b = __PYX_SIGN_BIT({{INT}}) & b;
  134. {{INT}} sign_r = __PYX_SIGN_BIT({{INT}}) & r;
  135. *overflow |= (sign_a == sign_b) & (sign_a != sign_r);
  136. return r;
  137. }
  138. }
  139. static CYTHON_INLINE {{INT}} __Pyx_add_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  140. if (b > 0) {
  141. *overflow |= a > __PYX_MAX({{INT}}) - b;
  142. } else if (b < 0) {
  143. *overflow |= a < __PYX_MIN({{INT}}) - b;
  144. }
  145. return a + b;
  146. }
  147. static CYTHON_INLINE {{INT}} __Pyx_sub_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  148. *overflow |= b == __PYX_MIN({{INT}});
  149. return __Pyx_add_{{NAME}}_checking_overflow(a, -b, overflow);
  150. }
  151. static CYTHON_INLINE {{INT}} __Pyx_sub_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  152. *overflow |= b == __PYX_MIN({{INT}});
  153. return __Pyx_add_const_{{NAME}}_checking_overflow(a, -b, overflow);
  154. }
  155. static CYTHON_INLINE {{INT}} __Pyx_mul_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  156. if ((sizeof({{INT}}) < sizeof(long))) {
  157. long big_r = ((long) a) * ((long) b);
  158. {{INT}} r = ({{INT}}) big_r;
  159. *overflow |= big_r != r;
  160. return ({{INT}}) r;
  161. #ifdef HAVE_LONG_LONG
  162. } else if ((sizeof({{INT}}) < sizeof(PY_LONG_LONG))) {
  163. PY_LONG_LONG big_r = ((PY_LONG_LONG) a) * ((PY_LONG_LONG) b);
  164. {{INT}} r = ({{INT}}) big_r;
  165. *overflow |= big_r != r;
  166. return ({{INT}}) r;
  167. #endif
  168. } else {
  169. {{INT}} prod = a * b;
  170. double dprod = ((double) a) * ((double) b);
  171. // Overflow results in an error of at least 2^sizeof(INT),
  172. // whereas rounding represents an error on the order of 2^(sizeof(INT)-53).
  173. *overflow |= fabs(dprod - prod) > (__PYX_MAX({{INT}}) / 2);
  174. return prod;
  175. }
  176. }
  177. static CYTHON_INLINE {{INT}} __Pyx_mul_const_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  178. if (b > 1) {
  179. *overflow |= a > __PYX_MAX({{INT}}) / b;
  180. *overflow |= a < __PYX_MIN({{INT}}) / b;
  181. } else if (b == -1) {
  182. *overflow |= a == __PYX_MIN({{INT}});
  183. } else if (b < -1) {
  184. *overflow |= a > __PYX_MIN({{INT}}) / b;
  185. *overflow |= a < __PYX_MAX({{INT}}) / b;
  186. }
  187. return a * b;
  188. }
  189. static CYTHON_INLINE {{INT}} __Pyx_div_{{NAME}}_checking_overflow({{INT}} a, {{INT}} b, int *overflow) {
  190. if (b == 0) {
  191. *overflow |= 1;
  192. return 0;
  193. }
  194. *overflow |= (a == __PYX_MIN({{INT}})) & (b == -1);
  195. return a / b;
  196. }
  197. /////////////// SizeCheck.init ///////////////
  198. //@substitute: naming
  199. // FIXME: Propagate the error here instead of just printing it.
  200. if (unlikely(__Pyx_check_sane_{{NAME}}())) {
  201. PyErr_WriteUnraisable($module_cname);
  202. }
  203. /////////////// SizeCheck.proto ///////////////
  204. static int __Pyx_check_sane_{{NAME}}(void) {
  205. if (((sizeof({{TYPE}}) <= sizeof(int)) ||
  206. #ifdef HAVE_LONG_LONG
  207. (sizeof({{TYPE}}) == sizeof(PY_LONG_LONG)) ||
  208. #endif
  209. (sizeof({{TYPE}}) == sizeof(long)))) {
  210. return 0;
  211. } else {
  212. PyErr_Format(PyExc_RuntimeError, \
  213. "Bad size for int type %.{{max(60, len(TYPE))}}s: %d", "{{TYPE}}", (int) sizeof({{TYPE}}));
  214. return 1;
  215. }
  216. }
  217. /////////////// Binop.proto ///////////////
  218. static CYTHON_INLINE {{TYPE}} __Pyx_{{BINOP}}_{{NAME}}_checking_overflow({{TYPE}} a, {{TYPE}} b, int *overflow);
  219. /////////////// Binop ///////////////
  220. static CYTHON_INLINE {{TYPE}} __Pyx_{{BINOP}}_{{NAME}}_checking_overflow({{TYPE}} a, {{TYPE}} b, int *overflow) {
  221. if ((sizeof({{TYPE}}) < sizeof(int))) {
  222. return __Pyx_{{BINOP}}_no_overflow(a, b, overflow);
  223. } else if (__PYX_IS_UNSIGNED({{TYPE}})) {
  224. if ((sizeof({{TYPE}}) == sizeof(unsigned int))) {
  225. return ({{TYPE}}) __Pyx_{{BINOP}}_unsigned_int_checking_overflow(a, b, overflow);
  226. } else if ((sizeof({{TYPE}}) == sizeof(unsigned long))) {
  227. return ({{TYPE}}) __Pyx_{{BINOP}}_unsigned_long_checking_overflow(a, b, overflow);
  228. #ifdef HAVE_LONG_LONG
  229. } else if ((sizeof({{TYPE}}) == sizeof(unsigned PY_LONG_LONG))) {
  230. return ({{TYPE}}) __Pyx_{{BINOP}}_unsigned_long_long_checking_overflow(a, b, overflow);
  231. #endif
  232. } else {
  233. abort(); return 0; /* handled elsewhere */
  234. }
  235. } else {
  236. if ((sizeof({{TYPE}}) == sizeof(int))) {
  237. return ({{TYPE}}) __Pyx_{{BINOP}}_int_checking_overflow(a, b, overflow);
  238. } else if ((sizeof({{TYPE}}) == sizeof(long))) {
  239. return ({{TYPE}}) __Pyx_{{BINOP}}_long_checking_overflow(a, b, overflow);
  240. #ifdef HAVE_LONG_LONG
  241. } else if ((sizeof({{TYPE}}) == sizeof(PY_LONG_LONG))) {
  242. return ({{TYPE}}) __Pyx_{{BINOP}}_long_long_checking_overflow(a, b, overflow);
  243. #endif
  244. } else {
  245. abort(); return 0; /* handled elsewhere */
  246. }
  247. }
  248. }
  249. /////////////// LeftShift.proto ///////////////
  250. static CYTHON_INLINE {{TYPE}} __Pyx_lshift_{{NAME}}_checking_overflow({{TYPE}} a, {{TYPE}} b, int *overflow) {
  251. *overflow |=
  252. #if {{SIGNED}}
  253. (b < 0) |
  254. #endif
  255. (b > ({{TYPE}}) (8 * sizeof({{TYPE}}))) | (a > (__PYX_MAX({{TYPE}}) >> b));
  256. return a << b;
  257. }
  258. #define __Pyx_lshift_const_{{NAME}}_checking_overflow __Pyx_lshift_{{NAME}}_checking_overflow
  259. /////////////// UnaryNegOverflows.proto ///////////////
  260. //FIXME: shouldn't the macro name be prefixed by "__Pyx_" ? Too late now, I guess...
  261. // from intobject.c
  262. #define UNARY_NEG_WOULD_OVERFLOW(x) \
  263. (((x) < 0) & ((unsigned long)(x) == 0-(unsigned long)(x)))