|
8 | 8 | extern "C" {
|
9 | 9 | #endif
|
10 | 10 |
|
| 11 | +/* To keep ABI compatibility with older version of numpy, npy routines that |
| 12 | + * manipulates npy_float as a struct are renamed using the convention defined in |
| 13 | + * macro NPY_HALF_INTERNAL_API. |
| 14 | + * Each symbol is then redefined based on that naming scheme to keep client code |
| 15 | + * mostly unchanged at API level |
| 16 | + */ |
| 17 | +#define NPY_HALF_INTERNAL_API(name) npy_internal_##name |
| 18 | + |
11 | 19 | /*
|
12 | 20 | * Half-precision routines
|
13 | 21 | */
|
14 | 22 |
|
15 | 23 | /* Conversions */
|
| 24 | +#define npy_half_to_float NPY_HALF_INTERNAL_API(half_to_float) |
16 | 25 | float npy_half_to_float(npy_half h);
|
| 26 | + |
| 27 | +#define npy_half_to_double NPY_HALF_INTERNAL_API(half_to_double) |
17 | 28 | double npy_half_to_double(npy_half h);
|
| 29 | + |
| 30 | +#define npy_float_to_half NPY_HALF_INTERNAL_API(float_to_half) |
18 | 31 | npy_half npy_float_to_half(float f);
|
| 32 | + |
| 33 | +#define npy_double_to_half NPY_HALF_INTERNAL_API(double_to_half) |
19 | 34 | npy_half npy_double_to_half(double d);
|
| 35 | + |
20 | 36 | /* Comparisons */
|
| 37 | +#define npy_half_eq NPY_HALF_INTERNAL_API(half_eq) |
21 | 38 | int npy_half_eq(npy_half h1, npy_half h2);
|
| 39 | + |
| 40 | +#define npy_half_ne NPY_HALF_INTERNAL_API(half_ne) |
22 | 41 | int npy_half_ne(npy_half h1, npy_half h2);
|
| 42 | + |
| 43 | +#define npy_half_le NPY_HALF_INTERNAL_API(half_le) |
23 | 44 | int npy_half_le(npy_half h1, npy_half h2);
|
| 45 | + |
| 46 | +#define npy_half_lt NPY_HALF_INTERNAL_API(half_lt) |
24 | 47 | int npy_half_lt(npy_half h1, npy_half h2);
|
| 48 | + |
| 49 | +#define npy_half_ge NPY_HALF_INTERNAL_API(half_ge) |
25 | 50 | int npy_half_ge(npy_half h1, npy_half h2);
|
| 51 | + |
| 52 | +#define npy_half_gt NPY_HALF_INTERNAL_API(half_gt) |
26 | 53 | int npy_half_gt(npy_half h1, npy_half h2);
|
| 54 | + |
27 | 55 | /* faster *_nonan variants for when you know h1 and h2 are not NaN */
|
| 56 | +#define npy_half_eq_nonan NPY_HALF_INTERNAL_API(half_eq_nonan) |
28 | 57 | int npy_half_eq_nonan(npy_half h1, npy_half h2);
|
| 58 | + |
| 59 | +#define npy_half_lt_nonan NPY_HALF_INTERNAL_API(half_lt_nonan) |
29 | 60 | int npy_half_lt_nonan(npy_half h1, npy_half h2);
|
| 61 | + |
| 62 | +#define npy_half_le_nonan NPY_HALF_INTERNAL_API(half_le_nonan) |
30 | 63 | int npy_half_le_nonan(npy_half h1, npy_half h2);
|
| 64 | + |
31 | 65 | /* Miscellaneous functions */
|
32 |
| -int npy_half_iszero(npy_half h); |
33 |
| -int npy_half_isnan(npy_half h); |
34 |
| -int npy_half_isinf(npy_half h); |
35 |
| -int npy_half_isfinite(npy_half h); |
36 |
| -int npy_half_signbit(npy_half h); |
| 66 | +#define npy_half_copysign NPY_HALF_INTERNAL_API(half_copysign) |
37 | 67 | npy_half npy_half_copysign(npy_half x, npy_half y);
|
| 68 | + |
| 69 | +#define npy_half_spacing NPY_HALF_INTERNAL_API(half_spacing) |
38 | 70 | npy_half npy_half_spacing(npy_half h);
|
| 71 | + |
| 72 | +#define npy_half_nextafter NPY_HALF_INTERNAL_API(half_nextafter) |
39 | 73 | npy_half npy_half_nextafter(npy_half x, npy_half y);
|
| 74 | + |
| 75 | +#define npy_half_divmod NPY_HALF_INTERNAL_API(half_divmod) |
40 | 76 | npy_half npy_half_divmod(npy_half x, npy_half y, npy_half *modulus);
|
41 | 77 |
|
| 78 | +#define npy_half_iszero NPY_HALF_INTERNAL_API(half_iszero) |
| 79 | +NPY_INLINE int npy_half_iszero(npy_half h) { |
| 80 | + return (h.bits&0x7fff) == 0; |
| 81 | +} |
| 82 | + |
| 83 | +#define npy_half_isnan NPY_HALF_INTERNAL_API(half_isnan) |
| 84 | +NPY_INLINE int npy_half_isnan(npy_half h) { |
| 85 | + return ((h.bits&0x7c00u) == 0x7c00u) && ((h.bits&0x03ffu) != 0x0000u); |
| 86 | +} |
| 87 | + |
| 88 | +#define npy_half_isinf NPY_HALF_INTERNAL_API(half_isinf) |
| 89 | +NPY_INLINE int npy_half_isinf(npy_half h) { |
| 90 | + return ((h.bits&0x7fffu) == 0x7c00u); |
| 91 | +} |
| 92 | + |
| 93 | +#define npy_half_isfinite NPY_HALF_INTERNAL_API(half_isfinite) |
| 94 | +NPY_INLINE int npy_half_isfinite(npy_half h) { |
| 95 | + return (h.bits&0x7c00u) != 0x7c00u; |
| 96 | +} |
| 97 | + |
| 98 | +#define npy_half_signbit NPY_HALF_INTERNAL_API(half_signbit) |
| 99 | +NPY_INLINE int npy_half_signbit(npy_half h) { |
| 100 | + return (h.bits&0x8000u) != 0; |
| 101 | +} |
| 102 | + |
| 103 | +#define npy_half_neg NPY_HALF_INTERNAL_API(half_neg) |
| 104 | +NPY_INLINE npy_half npy_half_neg(npy_half h) { |
| 105 | + npy_half res = {(npy_uint16)(h.bits^0x8000u)}; |
| 106 | + return res; |
| 107 | +} |
| 108 | + |
| 109 | +#define npy_half_abs NPY_HALF_INTERNAL_API(half_abs) |
| 110 | +NPY_INLINE npy_half npy_half_abs(npy_half h) { |
| 111 | + npy_half res = {(npy_uint16)(h.bits&0x7fffu)}; |
| 112 | + return res; |
| 113 | +} |
| 114 | + |
| 115 | +#define npy_half_pos NPY_HALF_INTERNAL_API(half_pos) |
| 116 | +NPY_INLINE npy_half npy_half_pos(npy_half h) { |
| 117 | + npy_half res = {(npy_uint16)(+h.bits)}; |
| 118 | + return res; |
| 119 | +} |
| 120 | + |
42 | 121 | /*
|
43 | 122 | * Half-precision constants
|
44 | 123 | */
|
45 | 124 |
|
46 |
| -#define NPY_HALF_ZERO (0x0000u) |
47 |
| -#define NPY_HALF_PZERO (0x0000u) |
48 |
| -#define NPY_HALF_NZERO (0x8000u) |
49 |
| -#define NPY_HALF_ONE (0x3c00u) |
50 |
| -#define NPY_HALF_NEGONE (0xbc00u) |
51 |
| -#define NPY_HALF_PINF (0x7c00u) |
52 |
| -#define NPY_HALF_NINF (0xfc00u) |
53 |
| -#define NPY_HALF_NAN (0x7e00u) |
| 125 | +#define NPY_HALF_ZERO (npy_half){0x0000u} |
| 126 | +#define NPY_HALF_PZERO (npy_half){0x0000u} |
| 127 | +#define NPY_HALF_NZERO (npy_half){0x8000u} |
| 128 | +#define NPY_HALF_ONE (npy_half){0x3c00u} |
| 129 | +#define NPY_HALF_NEGONE (npy_half){0xbc00u} |
| 130 | +#define NPY_HALF_PINF (npy_half){0x7c00u} |
| 131 | +#define NPY_HALF_NINF (npy_half){0xfc00u} |
| 132 | +#define NPY_HALF_NAN (npy_half){0x7e00u} |
54 | 133 |
|
55 |
| -#define NPY_MAX_HALF (0x7bffu) |
| 134 | +#define NPY_MAX_HALF (npy_half){0x7bffu} |
56 | 135 |
|
57 | 136 | /*
|
58 | 137 | * Bit-level conversions
|
|
0 commit comments