| 1 |
"""Primitive float ops.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from mypyc.ir.ops import ERR_MAGIC, ERR_MAGIC_OVERLAPPING, ERR_NEVER |
| 6 |
from mypyc.ir.rtypes import ( |
| 7 |
bit_rprimitive, |
| 8 |
bool_rprimitive, |
| 9 |
float_rprimitive, |
| 10 |
int_rprimitive, |
| 11 |
object_rprimitive, |
| 12 |
str_rprimitive, |
| 13 |
) |
| 14 |
from mypyc.primitives.registry import binary_op, function_op, load_address_op |
| 15 |
|
| 16 |
# Get the 'builtins.float' type object. |
| 17 |
load_address_op(name="builtins.float", type=object_rprimitive, src="PyFloat_Type") |
| 18 |
|
| 19 |
binary_op( |
| 20 |
name="//", |
| 21 |
arg_types=[float_rprimitive, float_rprimitive], |
| 22 |
return_type=float_rprimitive, |
| 23 |
c_function_name="CPyFloat_FloorDivide", |
| 24 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 25 |
) |
| 26 |
|
| 27 |
# float(int) |
| 28 |
int_to_float_op = function_op( |
| 29 |
name="builtins.float", |
| 30 |
arg_types=[int_rprimitive], |
| 31 |
return_type=float_rprimitive, |
| 32 |
c_function_name="CPyFloat_FromTagged", |
| 33 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 34 |
) |
| 35 |
|
| 36 |
# float(str) |
| 37 |
function_op( |
| 38 |
name="builtins.float", |
| 39 |
arg_types=[str_rprimitive], |
| 40 |
return_type=object_rprimitive, |
| 41 |
c_function_name="PyFloat_FromString", |
| 42 |
error_kind=ERR_MAGIC, |
| 43 |
) |
| 44 |
|
| 45 |
# abs(float) |
| 46 |
function_op( |
| 47 |
name="builtins.abs", |
| 48 |
arg_types=[float_rprimitive], |
| 49 |
return_type=float_rprimitive, |
| 50 |
c_function_name="fabs", |
| 51 |
error_kind=ERR_NEVER, |
| 52 |
) |
| 53 |
|
| 54 |
# math.sin(float) |
| 55 |
function_op( |
| 56 |
name="math.sin", |
| 57 |
arg_types=[float_rprimitive], |
| 58 |
return_type=float_rprimitive, |
| 59 |
c_function_name="CPyFloat_Sin", |
| 60 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 61 |
) |
| 62 |
|
| 63 |
# math.cos(float) |
| 64 |
function_op( |
| 65 |
name="math.cos", |
| 66 |
arg_types=[float_rprimitive], |
| 67 |
return_type=float_rprimitive, |
| 68 |
c_function_name="CPyFloat_Cos", |
| 69 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 70 |
) |
| 71 |
|
| 72 |
# math.tan(float) |
| 73 |
function_op( |
| 74 |
name="math.tan", |
| 75 |
arg_types=[float_rprimitive], |
| 76 |
return_type=float_rprimitive, |
| 77 |
c_function_name="CPyFloat_Tan", |
| 78 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 79 |
) |
| 80 |
|
| 81 |
# math.sqrt(float) |
| 82 |
function_op( |
| 83 |
name="math.sqrt", |
| 84 |
arg_types=[float_rprimitive], |
| 85 |
return_type=float_rprimitive, |
| 86 |
c_function_name="CPyFloat_Sqrt", |
| 87 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 88 |
) |
| 89 |
|
| 90 |
# math.exp(float) |
| 91 |
function_op( |
| 92 |
name="math.exp", |
| 93 |
arg_types=[float_rprimitive], |
| 94 |
return_type=float_rprimitive, |
| 95 |
c_function_name="CPyFloat_Exp", |
| 96 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 97 |
) |
| 98 |
|
| 99 |
# math.log(float) |
| 100 |
function_op( |
| 101 |
name="math.log", |
| 102 |
arg_types=[float_rprimitive], |
| 103 |
return_type=float_rprimitive, |
| 104 |
c_function_name="CPyFloat_Log", |
| 105 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 106 |
) |
| 107 |
|
| 108 |
# math.floor(float) |
| 109 |
function_op( |
| 110 |
name="math.floor", |
| 111 |
arg_types=[float_rprimitive], |
| 112 |
return_type=int_rprimitive, |
| 113 |
c_function_name="CPyFloat_Floor", |
| 114 |
error_kind=ERR_MAGIC, |
| 115 |
) |
| 116 |
|
| 117 |
# math.ceil(float) |
| 118 |
function_op( |
| 119 |
name="math.ceil", |
| 120 |
arg_types=[float_rprimitive], |
| 121 |
return_type=int_rprimitive, |
| 122 |
c_function_name="CPyFloat_Ceil", |
| 123 |
error_kind=ERR_MAGIC, |
| 124 |
) |
| 125 |
|
| 126 |
# math.fabs(float) |
| 127 |
function_op( |
| 128 |
name="math.fabs", |
| 129 |
arg_types=[float_rprimitive], |
| 130 |
return_type=float_rprimitive, |
| 131 |
c_function_name="fabs", |
| 132 |
error_kind=ERR_NEVER, |
| 133 |
) |
| 134 |
|
| 135 |
# math.pow(float, float) |
| 136 |
pow_op = function_op( |
| 137 |
name="math.pow", |
| 138 |
arg_types=[float_rprimitive, float_rprimitive], |
| 139 |
return_type=float_rprimitive, |
| 140 |
c_function_name="CPyFloat_Pow", |
| 141 |
error_kind=ERR_MAGIC_OVERLAPPING, |
| 142 |
) |
| 143 |
|
| 144 |
# math.copysign(float, float) |
| 145 |
copysign_op = function_op( |
| 146 |
name="math.copysign", |
| 147 |
arg_types=[float_rprimitive, float_rprimitive], |
| 148 |
return_type=float_rprimitive, |
| 149 |
c_function_name="copysign", |
| 150 |
error_kind=ERR_NEVER, |
| 151 |
) |
| 152 |
|
| 153 |
# math.isinf(float) |
| 154 |
function_op( |
| 155 |
name="math.isinf", |
| 156 |
arg_types=[float_rprimitive], |
| 157 |
return_type=bool_rprimitive, |
| 158 |
c_function_name="CPyFloat_IsInf", |
| 159 |
error_kind=ERR_NEVER, |
| 160 |
) |
| 161 |
|
| 162 |
# math.isnan(float) |
| 163 |
function_op( |
| 164 |
name="math.isnan", |
| 165 |
arg_types=[float_rprimitive], |
| 166 |
return_type=bool_rprimitive, |
| 167 |
c_function_name="CPyFloat_IsNaN", |
| 168 |
error_kind=ERR_NEVER, |
| 169 |
) |
| 170 |
|
| 171 |
# translate isinstance(obj, float) |
| 172 |
isinstance_float = function_op( |
| 173 |
name="builtins.isinstance", |
| 174 |
arg_types=[object_rprimitive], |
| 175 |
return_type=bit_rprimitive, |
| 176 |
c_function_name="PyFloat_Check", |
| 177 |
error_kind=ERR_NEVER, |
| 178 |
) |