Class NDIterCasting
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
srcTypeNPTypeCodedstTypeNPTypeCodecastingNPY_CASTING
Returns
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
srcvoid*Address of one element of
srcType.dstvoid*Address of one element of
dstTypeto write.srcTypeNPTypeCodeThe source element type.
dstTypeNPTypeCodeThe 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
srcvoid*srcTypeNPTypeCodedstvoid*strideslong*dstTypeNPTypeCodeshapelong*ndimintcountlong
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
srcvoid*strideslong*srcTypeNPTypeCodedstvoid*dstTypeNPTypeCodeshapelong*ndimintcountlong
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
srcvoid*srcStrideslong*srcTypeNPTypeCodedstvoid*dstStrideslong*dstTypeNPTypeCodeshapelong*ndimintcountlong
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
srcvoid*srcStridelongsrcTypeNPTypeCodedstvoid*dstStridelongdstTypeNPTypeCodecountlong
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
operandsNDArray[]The operands (the first
nopare read; none may be null).nopintThe operand count.
Returns
- NPTypeCode
NumPy's
np.result_typeof 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
stateNDIterStateThe 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
stateNDIterStatecastingNPY_CASTING