| 1 |
"""Functions and helpers for converting between dtypes""" |
| 2 |
|
| 3 |
from rpython.rlib import jit, objectmodel |
| 4 |
from rpython.rlib.signature import signature, types as ann |
| 5 |
from pypy.interpreter.gateway import unwrap_spec |
| 6 |
from pypy.interpreter.error import OperationError, oefmt |
| 7 |
|
| 8 |
from pypy.module.micronumpy.base import W_NDimArray, convert_to_array |
| 9 |
from pypy.module.micronumpy import constants as NPY |
| 10 |
from .types import ( |
| 11 |
BaseType, Bool, ULong, Long, Float64, Complex64, |
| 12 |
StringType, UnicodeType, VoidType, ObjectType, |
| 13 |
int_types, float_types, complex_types, number_types, all_types) |
| 14 |
from .descriptor import ( |
| 15 |
W_Dtype, get_dtype_cache, as_dtype, is_scalar_w, variable_dtype, |
| 16 |
new_string_dtype, new_unicode_dtype, num2dtype) |
| 17 |
|
| 18 |
@jit.unroll_safe |
| 19 |
def result_type(space, __args__): |
| 20 |
args_w, kw_w = __args__.unpack() |
| 21 |
if kw_w: |
| 22 |
raise oefmt(space.w_TypeError, |
| 23 |
"result_type() takes no keyword arguments") |
| 24 |
if not args_w: |
| 25 |
raise oefmt(space.w_ValueError, |
| 26 |
"at least one array or dtype is required") |
| 27 |
arrays_w = [] |
| 28 |
dtypes_w = [] |
| 29 |
for w_arg in args_w: |
| 30 |
if isinstance(w_arg, W_NDimArray): |
| 31 |
arrays_w.append(w_arg) |
| 32 |
elif is_scalar_w(space, w_arg): |
| 33 |
w_scalar = as_scalar(space, w_arg) |
| 34 |
w_arr = W_NDimArray.from_scalar(space, w_scalar) |
| 35 |
arrays_w.append(w_arr) |
| 36 |
else: |
| 37 |
dtype = as_dtype(space, w_arg) |
| 38 |
dtypes_w.append(dtype) |
| 39 |
return find_result_type(space, arrays_w, dtypes_w) |
| 40 |
|
| 41 |
@jit.look_inside_iff(lambda space, arrays_w, dtypes_w: |
| 42 |
jit.loop_unrolling_heuristic(arrays_w, len(arrays_w)) and |
| 43 |
jit.loop_unrolling_heuristic(dtypes_w, len(dtypes_w))) |
| 44 |
def find_result_type(space, arrays_w, dtypes_w): |
| 45 |
|
| 46 |
if len(arrays_w) == 1 and not dtypes_w: |
| 47 |
return arrays_w[0].get_dtype() |
| 48 |
elif not arrays_w and len(dtypes_w) == 1: |
| 49 |
return dtypes_w[0] |
| 50 |
result = None |
| 51 |
if not _use_min_scalar(arrays_w, dtypes_w): |
| 52 |
for w_array in arrays_w: |
| 53 |
if result is None: |
| 54 |
result = w_array.get_dtype() |
| 55 |
else: |
| 56 |
result = promote_types(space, result, w_array.get_dtype()) |
| 57 |
for dtype in dtypes_w: |
| 58 |
if result is None: |
| 59 |
result = dtype |
| 60 |
else: |
| 61 |
result = promote_types(space, result, dtype) |
| 62 |
else: |
| 63 |
small_unsigned = False |
| 64 |
for w_array in arrays_w: |
| 65 |
dtype = w_array.get_dtype() |
| 66 |
small_unsigned_scalar = False |
| 67 |
if w_array.is_scalar() and dtype.is_number(): |
| 68 |
num, alt_num = w_array.get_scalar_value().min_dtype() |
| 69 |
small_unsigned_scalar = (num != alt_num) |
| 70 |
dtype = num2dtype(space, num) |
| 71 |
if result is None: |
| 72 |
result = dtype |
| 73 |
small_unsigned = small_unsigned_scalar |
| 74 |
else: |
| 75 |
result, small_unsigned = _promote_types_su( |
| 76 |
space, result, dtype, |
| 77 |
small_unsigned, small_unsigned_scalar) |
| 78 |
for dtype in dtypes_w: |
| 79 |
if result is None: |
| 80 |
result = dtype |
| 81 |
small_unsigned = False |
| 82 |
else: |
| 83 |
result, small_unsigned = _promote_types_su( |
| 84 |
space, result, dtype, |
| 85 |
small_unsigned, False) |
| 86 |
return result |
| 87 |
|
| 88 |
simple_kind_ordering = objectmodel.dict_to_switch({ |
| 89 |
Bool.kind: 0, ULong.kind: 1, Long.kind: 1, |
| 90 |
Float64.kind: 2, Complex64.kind: 2, |
| 91 |
NPY.STRINGLTR: 3, NPY.STRINGLTR2: 3, |
| 92 |
UnicodeType.kind: 3, VoidType.kind: 3, ObjectType.kind: 3}) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
@jit.unroll_safe |
| 97 |
def _use_min_scalar(arrays_w, dtypes_w): |
| 98 |
"""Helper for find_result_type()""" |
| 99 |
if not arrays_w: |
| 100 |
return False |
| 101 |
all_scalars = True |
| 102 |
max_scalar_kind = 0 |
| 103 |
max_array_kind = 0 |
| 104 |
for w_array in arrays_w: |
| 105 |
if w_array.is_scalar(): |
| 106 |
kind = simple_kind_ordering(w_array.get_dtype().kind) |
| 107 |
if kind > max_scalar_kind: |
| 108 |
max_scalar_kind = kind |
| 109 |
else: |
| 110 |
all_scalars = False |
| 111 |
kind = simple_kind_ordering(w_array.get_dtype().kind) |
| 112 |
if kind > max_array_kind: |
| 113 |
max_array_kind = kind |
| 114 |
for dtype in dtypes_w: |
| 115 |
all_scalars = False |
| 116 |
kind = simple_kind_ordering(dtype.kind) |
| 117 |
if kind > max_array_kind: |
| 118 |
max_array_kind = kind |
| 119 |
return not all_scalars and max_array_kind >= max_scalar_kind |
| 120 |
|
| 121 |
|
| 122 |
@unwrap_spec(casting='text') |
| 123 |
def can_cast(space, w_from, w_totype, casting='safe'): |
| 124 |
try: |
| 125 |
target = as_dtype(space, w_totype, allow_None=False) |
| 126 |
except TypeError: |
| 127 |
raise oefmt(space.w_TypeError, |
| 128 |
"did not understand one of the types; 'None' not accepted") |
| 129 |
if isinstance(w_from, W_NDimArray): |
| 130 |
return space.newbool(can_cast_array(space, w_from, target, casting)) |
| 131 |
elif is_scalar_w(space, w_from): |
| 132 |
w_scalar = as_scalar(space, w_from) |
| 133 |
w_arr = W_NDimArray.from_scalar(space, w_scalar) |
| 134 |
return space.newbool(can_cast_array(space, w_arr, target, casting)) |
| 135 |
|
| 136 |
try: |
| 137 |
origin = as_dtype(space, w_from, allow_None=False) |
| 138 |
except TypeError: |
| 139 |
raise oefmt(space.w_TypeError, |
| 140 |
"did not understand one of the types; 'None' not accepted") |
| 141 |
return space.newbool(can_cast_type(space, origin, target, casting)) |
| 142 |
|
| 143 |
kind_ordering = { |
| 144 |
Bool.kind: 0, ULong.kind: 1, Long.kind: 2, |
| 145 |
Float64.kind: 4, Complex64.kind: 5, |
| 146 |
NPY.STRINGLTR: 6, NPY.STRINGLTR2: 6, |
| 147 |
UnicodeType.kind: 7, VoidType.kind: 8, ObjectType.kind: 9} |
| 148 |
|
| 149 |
def can_cast_type(space, origin, target, casting): |
| 150 |
|
| 151 |
if origin == target: |
| 152 |
return True |
| 153 |
if casting == 'unsafe': |
| 154 |
return True |
| 155 |
elif casting == 'no': |
| 156 |
return origin.eq(space, target) |
| 157 |
if origin.num == target.num: |
| 158 |
if origin.is_record(): |
| 159 |
return (target.is_record() and |
| 160 |
can_cast_record(space, origin, target, casting)) |
| 161 |
else: |
| 162 |
if casting == 'equiv': |
| 163 |
return origin.elsize == target.elsize |
| 164 |
elif casting == 'safe': |
| 165 |
return origin.elsize <= target.elsize |
| 166 |
else: |
| 167 |
return True |
| 168 |
|
| 169 |
elif casting == 'same_kind': |
| 170 |
if can_cast_to(origin, target): |
| 171 |
return True |
| 172 |
if origin.kind in kind_ordering and target.kind in kind_ordering: |
| 173 |
return kind_ordering[origin.kind] <= kind_ordering[target.kind] |
| 174 |
return False |
| 175 |
elif casting == 'safe': |
| 176 |
return can_cast_to(origin, target) |
| 177 |
else: |
| 178 |
return origin.num == target.num and origin.elsize == target.elsize |
| 179 |
|
| 180 |
def can_cast_record(space, origin, target, casting): |
| 181 |
if origin is target: |
| 182 |
return True |
| 183 |
if origin.fields is None or target.fields is None: |
| 184 |
return False |
| 185 |
if len(origin.fields) != len(target.fields): |
| 186 |
return False |
| 187 |
for name, (offset, orig_field) in origin.fields.iteritems(): |
| 188 |
if name not in target.fields: |
| 189 |
return False |
| 190 |
target_field = target.fields[name][1] |
| 191 |
if not can_cast_type(space, orig_field, target_field, casting): |
| 192 |
return False |
| 193 |
return True |
| 194 |
|
| 195 |
|
| 196 |
def can_cast_array(space, w_from, target, casting): |
| 197 |
|
| 198 |
origin = w_from.get_dtype() |
| 199 |
if w_from.is_scalar(): |
| 200 |
return can_cast_scalar( |
| 201 |
space, origin, w_from.get_scalar_value(), target, casting) |
| 202 |
else: |
| 203 |
return can_cast_type(space, origin, target, casting) |
| 204 |
|
| 205 |
def can_cast_scalar(space, from_type, value, target, casting): |
| 206 |
|
| 207 |
if from_type == target or casting == 'unsafe': |
| 208 |
return True |
| 209 |
if not from_type.is_number() or casting in ('no', 'equiv'): |
| 210 |
return can_cast_type(space, from_type, target, casting) |
| 211 |
if not from_type.is_native(): |
| 212 |
value = value.descr_byteswap(space) |
| 213 |
dtypenum, altnum = value.min_dtype() |
| 214 |
if target.is_unsigned(): |
| 215 |
dtypenum = altnum |
| 216 |
dtype = num2dtype(space, dtypenum) |
| 217 |
return can_cast_type(space, dtype, target, casting) |
| 218 |
|
| 219 |
def as_scalar(space, w_obj): |
| 220 |
dtype = scalar2dtype(space, w_obj) |
| 221 |
return dtype.coerce(space, w_obj) |
| 222 |
|
| 223 |
def min_scalar_type(space, w_a): |
| 224 |
w_array = convert_to_array(space, w_a) |
| 225 |
dtype = w_array.get_dtype() |
| 226 |
if w_array.is_scalar() and dtype.is_number(): |
| 227 |
num, alt_num = w_array.get_scalar_value().min_dtype() |
| 228 |
return num2dtype(space, num) |
| 229 |
else: |
| 230 |
return dtype |
| 231 |
|
| 232 |
def w_promote_types(space, w_type1, w_type2): |
| 233 |
dt1 = as_dtype(space, w_type1, allow_None=False) |
| 234 |
dt2 = as_dtype(space, w_type2, allow_None=False) |
| 235 |
return promote_types(space, dt1, dt2) |
| 236 |
|
| 237 |
def find_binop_result_dtype(space, dt1, dt2): |
| 238 |
if dt2 is None: |
| 239 |
return dt1 |
| 240 |
if dt1 is None: |
| 241 |
return dt2 |
| 242 |
return promote_types(space, dt1, dt2) |
| 243 |
|
| 244 |
def promote_types(space, dt1, dt2): |
| 245 |
"""Return the smallest dtype to which both input dtypes can be safely cast""" |
| 246 |
|
| 247 |
num = promotion_table[dt1.num][dt2.num] |
| 248 |
if num != -1: |
| 249 |
return num2dtype(space, num) |
| 250 |
|
| 251 |
|
| 252 |
if dt1.num > dt2.num: |
| 253 |
dt1, dt2 = dt2, dt1 |
| 254 |
|
| 255 |
if dt2.is_str(): |
| 256 |
if dt1.is_str(): |
| 257 |
if dt1.elsize > dt2.elsize: |
| 258 |
return dt1 |
| 259 |
else: |
| 260 |
return dt2 |
| 261 |
else: |
| 262 |
dt1_size = dt1.itemtype.strlen |
| 263 |
if dt1_size > dt2.elsize: |
| 264 |
return new_string_dtype(space, dt1_size) |
| 265 |
else: |
| 266 |
return dt2 |
| 267 |
elif dt2.is_unicode(): |
| 268 |
if dt1.is_unicode(): |
| 269 |
if dt1.elsize > dt2.elsize: |
| 270 |
return dt1 |
| 271 |
else: |
| 272 |
return dt2 |
| 273 |
elif dt1.is_str(): |
| 274 |
if dt2.elsize >= 4 * dt1.elsize: |
| 275 |
return dt2 |
| 276 |
else: |
| 277 |
return new_unicode_dtype(space, dt1.elsize) |
| 278 |
else: |
| 279 |
dt1_size = dt1.itemtype.strlen |
| 280 |
if 4 * dt1_size > dt2.elsize: |
| 281 |
return new_unicode_dtype(space, dt1_size) |
| 282 |
else: |
| 283 |
return dt2 |
| 284 |
else: |
| 285 |
assert dt2.num == NPY.VOID |
| 286 |
if can_cast_type(space, dt1, dt2, casting='equiv'): |
| 287 |
return dt1 |
| 288 |
raise oefmt(space.w_TypeError, "invalid type promotion") |
| 289 |
|
| 290 |
def _promote_types_su(space, dt1, dt2, su1, su2): |
| 291 |
"""Like promote_types(), but handles the small_unsigned flag as well""" |
| 292 |
if su1: |
| 293 |
if dt2.is_bool() or dt2.is_unsigned(): |
| 294 |
dt1 = dt1.as_unsigned(space) |
| 295 |
else: |
| 296 |
dt1 = dt1.as_signed(space) |
| 297 |
elif su2: |
| 298 |
if dt1.is_bool() or dt1.is_unsigned(): |
| 299 |
dt2 = dt2.as_unsigned(space) |
| 300 |
else: |
| 301 |
dt2 = dt2.as_signed(space) |
| 302 |
if dt1.elsize < dt2.elsize: |
| 303 |
su = su2 and (su1 or not dt1.is_signed()) |
| 304 |
elif dt1.elsize == dt2.elsize: |
| 305 |
su = su1 and su2 |
| 306 |
else: |
| 307 |
su = su1 and (su2 or not dt2.is_signed()) |
| 308 |
return promote_types(space, dt1, dt2), su |
| 309 |
|
| 310 |
def scalar2dtype(space, w_obj): |
| 311 |
from .boxes import W_GenericBox |
| 312 |
bool_dtype = get_dtype_cache(space).w_booldtype |
| 313 |
long_dtype = get_dtype_cache(space).w_longdtype |
| 314 |
int64_dtype = get_dtype_cache(space).w_int64dtype |
| 315 |
uint64_dtype = get_dtype_cache(space).w_uint64dtype |
| 316 |
complex_dtype = get_dtype_cache(space).w_complex128dtype |
| 317 |
float_dtype = get_dtype_cache(space).w_float64dtype |
| 318 |
object_dtype = get_dtype_cache(space).w_objectdtype |
| 319 |
if isinstance(w_obj, W_GenericBox): |
| 320 |
return w_obj.get_dtype(space) |
| 321 |
|
| 322 |
if space.isinstance_w(w_obj, space.w_bool): |
| 323 |
return bool_dtype |
| 324 |
elif space.isinstance_w(w_obj, space.w_int): |
| 325 |
try: |
| 326 |
space.int_w(w_obj) |
| 327 |
except OperationError as e: |
| 328 |
if e.match(space, space.w_OverflowError): |
| 329 |
if space.is_true(space.le(w_obj, space.newint(0))): |
| 330 |
return int64_dtype |
| 331 |
return uint64_dtype |
| 332 |
raise |
| 333 |
return int64_dtype |
| 334 |
elif space.isinstance_w(w_obj, space.w_float): |
| 335 |
return float_dtype |
| 336 |
elif space.isinstance_w(w_obj, space.w_complex): |
| 337 |
return complex_dtype |
| 338 |
elif space.isinstance_w(w_obj, space.w_bytes): |
| 339 |
return variable_dtype(space, 'S%d' % space.len_w(w_obj)) |
| 340 |
elif space.isinstance_w(w_obj, space.w_unicode): |
| 341 |
return new_unicode_dtype(space, space.len_w(w_obj)) |
| 342 |
return object_dtype |
| 343 |
|
| 344 |
@signature(ann.instance(W_Dtype), ann.instance(W_Dtype), returns=ann.bool()) |
| 345 |
def can_cast_to(dt1, dt2): |
| 346 |
"""Return whether dtype `dt1` can be cast safely to `dt2`""" |
| 347 |
|
| 348 |
from .casting import can_cast_itemtype |
| 349 |
result = can_cast_itemtype(dt1.itemtype, dt2.itemtype) |
| 350 |
if result: |
| 351 |
if dt1.num == NPY.STRING: |
| 352 |
if dt2.num == NPY.STRING: |
| 353 |
return dt1.elsize <= dt2.elsize |
| 354 |
elif dt2.num == NPY.UNICODE: |
| 355 |
return dt1.elsize * 4 <= dt2.elsize |
| 356 |
elif dt1.num == NPY.UNICODE and dt2.num == NPY.UNICODE: |
| 357 |
return dt1.elsize <= dt2.elsize |
| 358 |
elif dt2.num in (NPY.STRING, NPY.UNICODE): |
| 359 |
if dt2.num == NPY.STRING: |
| 360 |
char_size = 1 |
| 361 |
else: |
| 362 |
char_size = 4 |
| 363 |
if dt2.elsize == 0: |
| 364 |
return True |
| 365 |
if dt1.is_int(): |
| 366 |
return dt2.elsize >= dt1.itemtype.strlen * char_size |
| 367 |
return result |
| 368 |
|
| 369 |
|
| 370 |
@signature(ann.instance(BaseType), ann.instance(BaseType), returns=ann.bool()) |
| 371 |
def can_cast_itemtype(tp1, tp2): |
| 372 |
|
| 373 |
return casting_table[tp1.num][tp2.num] |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
casting_table = [[False] * NPY.NTYPES for _ in range(NPY.NTYPES)] |
| 379 |
|
| 380 |
def enable_cast(type1, type2): |
| 381 |
casting_table[type1.num][type2.num] = True |
| 382 |
|
| 383 |
def _can_cast(type1, type2): |
| 384 |
"""NOT_RPYTHON: operates on BaseType subclasses""" |
| 385 |
return casting_table[type1.num][type2.num] |
| 386 |
|
| 387 |
for tp in all_types: |
| 388 |
enable_cast(tp, tp) |
| 389 |
if tp.num != NPY.DATETIME: |
| 390 |
enable_cast(Bool, tp) |
| 391 |
enable_cast(tp, ObjectType) |
| 392 |
enable_cast(tp, VoidType) |
| 393 |
enable_cast(StringType, UnicodeType) |
| 394 |
|
| 395 |
|
| 396 |
for tp in number_types: |
| 397 |
enable_cast(tp, StringType) |
| 398 |
enable_cast(tp, UnicodeType) |
| 399 |
|
| 400 |
for tp1 in int_types: |
| 401 |
for tp2 in int_types: |
| 402 |
if tp1.signed: |
| 403 |
if tp2.signed and tp1.basesize() <= tp2.basesize(): |
| 404 |
enable_cast(tp1, tp2) |
| 405 |
else: |
| 406 |
if tp2.signed and tp1.basesize() < tp2.basesize(): |
| 407 |
enable_cast(tp1, tp2) |
| 408 |
elif not tp2.signed and tp1.basesize() <= tp2.basesize(): |
| 409 |
enable_cast(tp1, tp2) |
| 410 |
for tp1 in int_types: |
| 411 |
for tp2 in float_types + complex_types: |
| 412 |
size1 = tp1.basesize() |
| 413 |
size2 = tp2.basesize() |
| 414 |
if (size1 < 8 and size2 > size1) or (size1 >= 8 and size2 >= size1): |
| 415 |
enable_cast(tp1, tp2) |
| 416 |
for tp1 in float_types: |
| 417 |
for tp2 in float_types + complex_types: |
| 418 |
if tp1.basesize() <= tp2.basesize(): |
| 419 |
enable_cast(tp1, tp2) |
| 420 |
for tp1 in complex_types: |
| 421 |
for tp2 in complex_types: |
| 422 |
if tp1.basesize() <= tp2.basesize(): |
| 423 |
enable_cast(tp1, tp2) |
| 424 |
|
| 425 |
promotion_table = [[-1] * NPY.NTYPES for _ in range(NPY.NTYPES)] |
| 426 |
def promotes(tp1, tp2, tp3): |
| 427 |
if tp3 is None: |
| 428 |
num = -1 |
| 429 |
else: |
| 430 |
num = tp3.num |
| 431 |
promotion_table[tp1.num][tp2.num] = num |
| 432 |
|
| 433 |
|
| 434 |
for tp in all_types: |
| 435 |
promotes(tp, ObjectType, ObjectType) |
| 436 |
promotes(ObjectType, tp, ObjectType) |
| 437 |
|
| 438 |
for tp1 in [Bool] + number_types: |
| 439 |
for tp2 in [Bool] + number_types: |
| 440 |
if tp1 is tp2: |
| 441 |
promotes(tp1, tp1, tp1) |
| 442 |
elif _can_cast(tp1, tp2): |
| 443 |
promotes(tp1, tp2, tp2) |
| 444 |
elif _can_cast(tp2, tp1): |
| 445 |
promotes(tp1, tp2, tp1) |
| 446 |
else: |
| 447 |
|
| 448 |
result = None |
| 449 |
for tp3 in number_types: |
| 450 |
if _can_cast(tp1, tp3) and _can_cast(tp2, tp3): |
| 451 |
if result is None: |
| 452 |
result = tp3 |
| 453 |
elif _can_cast(tp3, result) and not _can_cast(result, tp3): |
| 454 |
result = tp3 |
| 455 |
promotes(tp1, tp2, result) |