blob: 5543f126c3549a4a4a762b6282cc65d9a8cadfe1 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien Georgeecf5b772014-04-04 11:13:51 +000027#include "misc.h"
28#include "mpconfig.h"
29#include "qstr.h"
30#include "obj.h"
Damien Georged1e355e2014-05-28 14:51:12 +010031#include "smallint.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000032
33bool mp_small_int_mul_overflow(machine_int_t x, machine_int_t y) {
34 // Check for multiply overflow; see CERT INT32-C
35 if (x > 0) { // x is positive
36 if (y > 0) { // x and y are positive
37 if (x > (MP_SMALL_INT_MAX / y)) {
38 return true;
39 }
40 } else { // x positive, y nonpositive
41 if (y < (MP_SMALL_INT_MIN / x)) {
42 return true;
43 }
44 } // x positive, y nonpositive
45 } else { // x is nonpositive
46 if (y > 0) { // x is nonpositive, y is positive
47 if (x < (MP_SMALL_INT_MIN / y)) {
48 return true;
49 }
50 } else { // x and y are nonpositive
51 if (x != 0 && y < (MP_SMALL_INT_MAX / x)) {
52 return true;
53 }
54 } // End if x and y are nonpositive
55 } // End if x is nonpositive
56 return false;
57}
58
59machine_int_t mp_small_int_modulo(machine_int_t dividend, machine_int_t divisor) {
60 machine_int_t lsign = (dividend >= 0) ? 1 :-1;
61 machine_int_t rsign = (divisor >= 0) ? 1 :-1;
62 dividend %= divisor;
63 if (lsign != rsign) {
64 dividend += divisor;
65 }
66 return dividend;
67}
68
69
70machine_int_t mp_small_int_floor_divide(machine_int_t num, machine_int_t denom) {
71 machine_int_t lsign = num > 0 ? 1 : -1;
72 machine_int_t rsign = denom > 0 ? 1 : -1;
73 if (lsign == -1) {num *= -1;}
74 if (rsign == -1) {denom *= -1;}
75 if (lsign != rsign){
76 return - ( num + denom - 1) / denom;
77 } else {
78 return num / denom;
79 }
80}