summaryrefslogtreecommitdiff
path: root/numpy/core/src/common/templ_common.h.src
blob: d0d1917fec71cfb2dcab7b5be5c2f7bc78e7716e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef __NPY_TYPED_COMMON_INC
#define __NPY_TYPED_COMMON_INC

/* utility functions that profit from templates */

#include "numpy/npy_common.h"
#include <assert.h>

/**begin repeat
 *  #name = int, uint, long, ulong,
 *          longlong, ulonglong#
 *  #type = npy_int, npy_uint, npy_long, npy_ulong,
 *          npy_longlong, npy_ulonglong#
 *  #MAX = NPY_MAX_INT, NPY_MAX_UINT, NPY_MAX_LONG, NPY_MAX_ULONG,
 *         NPY_MAX_LONGLONG, NPY_MAX_ULONGLONG#
 *
 *  #neg = (1,0)*3#
 *  #abs_func = abs*2, labs*2, llabs*2#
 */
/*
 * writes result of a * b into r
 * returns 1 if a * b overflowed else returns 0
 * 
 * These functions are not designed to work if either a or b is negative, but
 * that is not checked. Could use absolute values and adjust the sign if that
 * functionality was desired.
 */
static inline int
npy_mul_with_overflow_@name@(@type@ * r, @type@ a, @type@ b)
{
#ifdef HAVE___BUILTIN_MUL_OVERFLOW
    return __builtin_mul_overflow(a, b, r);
#else
    const @type@ half_sz = ((@type@)1 << ((sizeof(a) * 8 - 1 ) / 2));

    *r = a * b;
    /*
     * avoid expensive division on common no overflow case
     */
    if ((NPY_UNLIKELY((a | b) >= half_sz) || (a | b) < 0) &&
            a != 0 && 
#if @neg@
            @abs_func@(b) > @abs_func@(@MAX@ / a)
#else
            b > @MAX@ / a
#endif
            ) {
        return 1;
    }
    return 0;
#endif
}
/**end repeat**/

static inline int
npy_mul_sizes_with_overflow (npy_intp * r, npy_intp a, npy_intp b)
{
#ifdef HAVE___BUILTIN_MUL_OVERFLOW
    return __builtin_mul_overflow(a, b, r);
#else
    /* this function only supports non-negative numbers */
    assert(a >= 0 && b >= 0);
    const npy_intp half_sz = ((npy_intp)1 << ((sizeof(a) * 8 - 1 ) / 2));

    *r = a * b;
    /*
     * avoid expensive division on common no overflow case
     */
    if (NPY_UNLIKELY((a | b) >= half_sz)
        && a != 0 && b > NPY_MAX_INTP / a) {
        return 1;
    }
    return 0;
#endif
}

#endif