** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment ** about luaV_div.) */
| 616 | ** about luaV_div.) |
| 617 | */ |
| 618 | lua_Integer luaV_mod(lua_State *L, lua_Integer m, lua_Integer n) { |
| 619 | if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ |
| 620 | if (n == 0) |
| 621 | luaG_runerror(L, "attempt to perform 'n%%0'"); |
| 622 | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ |
| 623 | } |
| 624 | else { |
| 625 | lua_Integer r = m % n; |
| 626 | if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ |
| 627 | r += n; /* correct result for different rounding */ |
| 628 | return r; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | |
| 633 | /* number of bits in an integer */ |
no test coverage detected