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*

Address of one element of srcType.

dst void*

Address of one element of dstType to write.

srcType NPTypeCode

The source element type.

dstType NPTypeCode

The destination element type.

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).

float16 <-> float32 converts DIRECTLY, never through a double: NumPy converts float16 bit by bit (npy_halfbits_to_floatbits / npy_floatbits_to_halfbits), so a signalling NaN stays signalling, and the float32 <-> float64 hop in between would set the quiet bit. float16 <-> float64 goes through the double, which the NaN-exact ToDouble(Half) / ToHalf(double) make exact.

Exceptions

NotSupportedException

Either type is not one of the 15 storage dtypes.

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 (TryGetInnerCastKernel(NPTypeCode, NPTypeCode)) 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[]

The operands (the first nop are read; none may be null).

nop int

The operand count.

Returns

NPTypeCode

NumPy's np.result_type of the operands' dtypes (float64 for no operand).

Remarks

NumPy's npyiter_get_common_dtype is PyArray_ResultType over the operands, which does not depend on their order (int8, uint8, float16 is float16 in every order); the operands are arrays, so all of them are strong (NEP 50). This folded PromoteTypes(NPTypeCode, NPTypeCode) left to right before, whose own rules differed from NumPy's (int32 + float32 gave float32, bool + int8 gave int16).

Exceptions

NotSupportedException

An operand is a string array (the numeric promotion refuses text).

PackTransferFlags(ref NDIterState)

Packs the combined transfer flags into the top 8 bits of ItFlags WITHOUT validating the casts — the descriptor construction path, which runs NumPy's own descriptor-level cast checks (npyiter_check_casting) and must not meet ValidateCasts(ref NDIterState, NPY_CASTING)' type-code check, which knows no string width or class.

public static void PackTransferFlags(ref NDIterState state)

Parameters

state NDIterState

The iterator state (its operands' dtypes already set).

Remarks

Every transfer NumSharp runs — a cast, a same-type copy, a string copy — reports the same characteristics (ComputeCastTransferFlags(NPTypeCode, NPTypeCode): unaligned-safe, no floating-point errors, reorderable; never REQUIRES_PYAPI), so the packed value equals what ValidateCasts(ref NDIterState, NPY_CASTING) would pack.

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