Table of Contents

Class NDIterCasting

Namespace
NumSharp.Backends.Iteration
Assembly
NumSharp.dll

Type casting utilities for NDIter. Validates casting rules and performs type conversions.

public static class NDIterCasting
Inheritance
NDIterCasting
Inherited Members

Methods

CanCast(NPTypeCode, NPTypeCode, NPY_CASTING)

Check if casting from srcType to dstType is allowed under the given casting rule.

public static bool CanCast(NPTypeCode srcType, NPTypeCode dstType, NPY_CASTING casting)

Parameters

srcType NPTypeCode
dstType NPTypeCode
casting NPY_CASTING

Returns

bool

ConvertValue(void*, void*, NPTypeCode, NPTypeCode)

Convert a single value from srcType to dstType.

public static void ConvertValue(void* src, void* dst, NPTypeCode srcType, NPTypeCode dstType)

Parameters

src void*
dst void*
srcType NPTypeCode
dstType NPTypeCode

Remarks

Complex needs special handling on either end because a double intermediate would drop the imaginary component. Real -> Complex sets imaginary=0; Complex -> Real takes the real part (matching NumPy's ComplexWarning truncation).

CopyContiguousToStridedWithCast(void*, NPTypeCode, void*, long*, NPTypeCode, long*, int, long)

Copy contiguous buffer to strided data with type conversion.

public static void CopyContiguousToStridedWithCast(void* src, NPTypeCode srcType, void* dst, long* strides, NPTypeCode dstType, long* shape, int ndim, long count)

Parameters

src void*
srcType NPTypeCode
dst void*
strides long*
dstType NPTypeCode
shape long*
ndim int
count long

CopyStridedToContiguousWithCast(void*, long*, NPTypeCode, void*, NPTypeCode, long*, int, long)

Copy strided data to contiguous buffer with type conversion.

public static void CopyStridedToContiguousWithCast(void* src, long* strides, NPTypeCode srcType, void* dst, NPTypeCode dstType, long* shape, int ndim, long count)

Parameters

src void*
strides long*
srcType NPTypeCode
dst void*
dstType NPTypeCode
shape long*
ndim int
count long

CopyStridedToStridedWithCast(void*, long*, NPTypeCode, void*, long*, NPTypeCode, long*, int, long)

Copy strided source to strided destination with type conversion. Handles broadcast on the source via stride=0 dimensions and arbitrary destination strides. Strides are in element counts (not bytes); element size multiplication happens internally via GetSize(NPTypeCode).

The per-element conversion runs through an IL-emitted inner-loop kernel (DirectILKernelGenerator.TryGetInnerCastKernel) that issues a DIRECT call Converts.To{Dst} — the JIT inlines it, so it runs at hand-written direct-call speed (probed: ~1.1-4.8× faster than the Func delegate it replaces, the lighter the conversion the bigger the win). The addressing is the same incremental-coord outer walk; only the inner body is IL. Bit-exact: Converts.To{Dst} is the table FindConverter bound to. All 225 dtype pairs are covered (Complex/Decimal included); the scalar ConvertValue(void*, void*, NPTypeCode, NPTypeCode) path remains a fallback if IL generation is off.

public static void CopyStridedToStridedWithCast(void* src, long* srcStrides, NPTypeCode srcType, void* dst, long* dstStrides, NPTypeCode dstType, long* shape, int ndim, long count)

Parameters

src void*
srcStrides long*
srcType NPTypeCode
dst void*
dstStrides long*
dstType NPTypeCode
shape long*
ndim int
count long

CopyWithCast(void*, long, NPTypeCode, void*, long, NPTypeCode, long)

Copy array data with type conversion.

public static void CopyWithCast(void* src, long srcStride, NPTypeCode srcType, void* dst, long dstStride, NPTypeCode dstType, long count)

Parameters

src void*
srcStride long
srcType NPTypeCode
dst void*
dstStride long
dstType NPTypeCode
count long

FindCommonDtype(NDArray[], int)

Find common dtype for all operands (for COMMON_DTYPE flag). Returns the dtype that all operands can be safely promoted to.

public static NPTypeCode FindCommonDtype(NDArray[] operands, int nop)

Parameters

operands NDArray[]
nop int

Returns

NPTypeCode

ValidateCasts(ref NDIterState, NPY_CASTING)

Validate all operand casts in an iterator state. Throws InvalidCastException if any cast is not allowed. Also packs combined transfer flags into the top 8 bits of state.ItFlags per NumPy nditer_constr.c:3542.

public static void ValidateCasts(ref NDIterState state, NPY_CASTING casting)

Parameters

state NDIterState
casting NPY_CASTING