Table of Contents

Class BuiltinCastingImpl

Namespace
NumSharp
Assembly
NumSharp.dll

The cast between two storage-backed builtin classes (NumPy's add_numeric_cast, convert_datatype.c). Its casting is computed the way NumPy computes spec.casting: the same class, or two NumPy classes of one kind and one size (intc ↔ int32 on Windows, longdouble ↔ float64) → equiv (NumPy's numeric_copy_or_byteswap, resolved to no cast at all when both byte orders are native); safe when the frozen promotion table says promote(from, to) == to (NumPy's _npy_can_cast_safely_table); otherwise same_kind when the source kind orders at or below the destination kind (b < u < i < f < c), else unsafe. These are exactly the rules np.can_cast(NPTypeCode, NPTypeCode) always applied, so the builtin answers are unchanged; the class pair now owns them.

public sealed class BuiltinCastingImpl : CastingImpl
Inheritance
BuiltinCastingImpl
Inherited Members

Remarks

The equivalence holds between NumPy's own classes only (IsEquivalentNumeric(LegacyBuiltinDTypeMeta)): NumSharp's Char and UInt16 have one kind and one size too, but they are distinct storage types with distinct kernels, so Char ↔ UInt16 stays safe one way and same_kind the other, as before.

A lane-sharing class casts through its lane's tables: its storage code is the lane's, so the promotion-table test reads the lane pair (longdouble → float32 is float64's same_kind). Its strided loop (get_strided_loop(ArrayMethodContext, bool, long[], out NDArrayMethodFlags)) is the engine's IL inner cast kernel of the lane pair, the per-element cast the bulk casts are measured against; the bulk routes (astype, the iterator's numeric buffers) keep their own vector kernels.

Properties

casting

The minimal safety of this numeric cast — NumPy's add_numeric_cast value (equiv within a class, safe when the promotion table maps (from, to) back onto to, same_kind along the b < u < i < f < c kind order, else unsafe). Computed on FIRST READ, not at registration: the promotion table is np._nptypemap_arr_arr, built by np's static constructor, and np's field initializers (np.float64 is a DType) are what first construct DTypeRegistry — so reading the table while the registry registers the 15×15 impls would observe it before it exists. The value is a constant, so two threads racing the first read compute the same value and either store wins; each reads the cache with one volatile load (see NumSharp.BuiltinCastingImpl._casting).

public override NPY_CASTING casting { get; }

Property Value

NPY_CASTING

Methods

get_strided_loop(ArrayMethodContext, bool, long[], out NDArrayMethodFlags)

NumPy's numeric cast loop (strided_to_strided_cast* / numeric_copy_or_byteswap): the engine's IL inner cast kernel of the two storage lanes - the NumPy-exact per-element cast table (a lane-sharing class casts as its lane; within one lane it is a copy), or the scalar ConvertValue(void*, void*, NPTypeCode, NPTypeCode) loop where dynamic code is unavailable (the same table).

public override NDInnerLoopFunc get_strided_loop(ArrayMethodContext context, bool aligned, long[] strides, out NDArrayMethodFlags flags)

Parameters

context ArrayMethodContext

The resolved descriptors (both native: storage holds no other byte order).

aligned bool

Unused: the kernels read and write unaligned elements.

strides long[]

Unused: the kernel takes the strides of each call.

flags NDArrayMethodFlags

Receives the method's flags (no Python API, no floating-point errors raised).

Returns

NDInnerLoopFunc

The loop: [0] the source run, [1] the destination run, byte strides.

resolve_descriptors(DType[], DType[], out long)

NumPy's resolve_descriptors: given the operand descriptors (an output entry may be null — "please pick"), fills loopDescrs with the descriptors the loop will actually run on and returns the cast safety of doing so.

public override NPY_CASTING resolve_descriptors(DType[] givenDescrs, DType[] loopDescrs, out long viewOffset)

Parameters

givenDescrs DType[]

The operand descriptors, inputs first; an output entry may be null.

loopDescrs DType[]

Receives the descriptors the loop runs on, one per operand.

viewOffset long

The byte offset at which the operation is a plain VIEW (0 for a no-op cast between identical descriptors), or NoView.

Returns

NPY_CASTING

The cast safety of running the loop on loopDescrs.

Remarks

An equivalent pair resolves as NumPy's legacy_same_dtype_resolve_descriptors does, which NumPy installs for every numeric_copy_or_byteswap cast: no cast (a view) when both byte orders are native, an equiv byte swap otherwise — so np.can_cast('g', 'd', 'no') is True.

Exceptions

TypeError

The descriptors cannot be resolved (NumPy returns -1 with an error set).