** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment ** about luaV_idiv.) */
| 784 | ** about luaV_idiv.) |
| 785 | */ |
| 786 | lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { |
| 787 | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ |
| 788 | if (n == 0) |
| 789 | luaG_runerror(L, "attempt to perform 'n%%0'"); |
| 790 | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ |
| 791 | } |
| 792 | else { |
| 793 | lua_Integer r = m % n; |
| 794 | if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ |
| 795 | r += n; /* correct result for different rounding */ |
| 796 | return r; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | |
| 801 | /* |
no test coverage detected