Class DirectILKernelGenerator
Binary operations (same-type) - contiguous kernels and generic helpers.
public static class DirectILKernelGenerator
- Inheritance
-
DirectILKernelGenerator
- Inherited Members
Fields
VectorBits
Detected vector width at startup: 512, 256, 128, or 0 (no SIMD).
public static readonly int VectorBits
Field Value
VectorBytes
Number of bytes per vector register.
public static readonly int VectorBytes
Field Value
Properties
Enabled
Whether IL generation is enabled. Can be disabled for debugging.
public static bool Enabled { get; set; }
Property Value
Name
Provider name for diagnostics.
public static string Name { get; }
Property Value
Methods
Clip(NPTypeCode, ClipMode, ClipBoundsKind, void*, void*, long, void*, void*)
Run a clip operation. Picks (and on first call, IL-generates) the appropriate DynamicMethod for the (dtype, mode, kind) tuple and invokes it with the supplied pointers.
public static void Clip(NPTypeCode dtype, DirectILKernelGenerator.ClipMode mode, DirectILKernelGenerator.ClipBoundsKind kind, void* src, void* dst, long size, void* lo, void* hi)
Parameters
dtypeNPTypeCodemodeDirectILKernelGenerator.ClipModekindDirectILKernelGenerator.ClipBoundsKindsrcvoid*dstvoid*sizelonglovoid*hivoid*
FrexpHelper(decimal*, decimal*, int*, long)
Scalar frexp over a contiguous Decimal buffer (a NumSharp extension; NumPy has no decimal
dtype). Computed through the double bridge, so the mantissa carries double precision — consistent
with every other decimal transcendental in the engine.
public static void FrexpHelper(decimal* src, decimal* mant, int* exp, long n)
Parameters
srcdecimal*Source Decimal values (contiguous).
mantdecimal*Receives the Decimal mantissas.
expint*Receives one int32 exponent per element.
nlongElement count.
FrexpHelper(double*, double*, int*, long)
SIMD frexp over a contiguous double buffer: reads src[i] and writes the
mantissa to mant[i] and the int32 exponent to exp[i]. Out of
place so the two outputs share one C-contiguous layout regardless of the source's layout. The
all-normal chunk is vectorized (integer field manipulation); any chunk with a zero/subnormal/inf/
NaN lane falls to the scalar core so the fast path stays branch-free.
public static void FrexpHelper(double* src, double* mant, int* exp, long n)
Parameters
srcdouble*Source values (contiguous). May alias
mantonly if identical.mantdouble*Receives the mantissas.
expint*Receives one int32 exponent per element.
nlongElement count.
FrexpHelper(Half*, Half*, int*, long)
Scalar frexp over a contiguous Half buffer: widens each value to float, decomposes it
with FrexpStepF(float, out int) and narrows the mantissa back to Half — NumPy's exact
HALF_frexp shape. No vector path (the BCL has no Half arithmetic).
public static void FrexpHelper(Half* src, Half* mant, int* exp, long n)
Parameters
srcHalf*Source Half values (contiguous).
mantHalf*Receives the Half mantissas.
expint*Receives one int32 exponent per element.
nlongElement count.
FrexpHelper(float*, float*, int*, long)
SIMD frexp over a contiguous float buffer — the FrexpHelper(double*, double*, int*, long)
algorithm at single precision (exponents are computed as int32 lanes and stored directly).
public static void FrexpHelper(float* src, float* mant, int* exp, long n)
Parameters
srcfloat*Source values (contiguous).
mantfloat*Receives the mantissas.
expint*Receives one int32 exponent per element.
nlongElement count.
GetAdjacentDiffKernel(NPTypeCode)
Get or generate the adjacent-difference (subtract) stencil kernel for
dt. Returns null for Boolean (np.diff differences
booleans with not_equal, not subtract) and whenever IL generation is disabled
or the emitter rejects the dtype — the caller then falls back to the generic
view-subtract path.
public static DirectILKernelGenerator.AdjacentDiffKernel GetAdjacentDiffKernel(NPTypeCode dt)
Parameters
dtNPTypeCode
Returns
GetArgwhereCountKernel(Type)
IL-emitted count of non-zero elements. Returns null only when
Enabled is false — every supported dtype has a kernel
(SIMD where Vector{T} exists, scalar IL via op_Inequality otherwise).
public static ArgwhereCountKernel GetArgwhereCountKernel(Type elementType)
Parameters
elementTypeType
Returns
GetArgwhereExpandKernel()
IL-emitted coord expand (singleton — same kernel handles any ndim).
public static ArgwhereExpandKernel GetArgwhereExpandKernel()
Returns
GetArgwhereFlatKernel(Type)
IL-emitted bit-scan that writes flat indices of non-zero elements into a pre-sized buffer.
public static ArgwhereFlatKernel GetArgwhereFlatKernel(Type elementType)
Parameters
elementTypeType
Returns
GetBinaryScalarDelegate(BinaryScalarKernelKey)
Get or generate an IL-based binary scalar delegate. Returns a Func<TLhs, TRhs, TResult> delegate.
public static Delegate GetBinaryScalarDelegate(BinaryScalarKernelKey key)
Parameters
Returns
GetChooseFlatKernel(int, int)
IL-emitted flat choose kernel, cached per (element width, mode). Returns null
only when Enabled is false.
public static ChooseFlatKernel GetChooseFlatKernel(int elemBytes, int mode)
Parameters
Returns
GetChooseStridedKernel(int, int)
IL-emitted strided choose kernel, cached per (element width, mode). Returns null
only when Enabled is false.
public static ChooseStridedKernel GetChooseStridedKernel(int elemBytes, int mode)
Parameters
Returns
GetComparisonKernel(ComparisonKernelKey)
Get or generate a comparison kernel for the specified key.
public static ComparisonKernel GetComparisonKernel(ComparisonKernelKey key)
Parameters
Returns
GetComparisonScalarDelegate(ComparisonScalarKernelKey)
Get or generate a comparison scalar delegate. Returns a Func<TLhs, TRhs, bool> delegate.
public static Delegate GetComparisonScalarDelegate(ComparisonScalarKernelKey key)
Parameters
Returns
GetCopyKernel(CopyKernelKey)
public static CopyKernel GetCopyKernel(CopyKernelKey key)
Parameters
keyCopyKernelKey
Returns
GetCumulativeKernel(CumulativeKernelKey)
Get or generate a cumulative (scan) kernel. Returns a delegate that computes running accumulation over all elements.
public static CumulativeKernel GetCumulativeKernel(CumulativeKernelKey key)
Parameters
Returns
GetDiagWriteKernel(int)
IL-emitted diagonal-write kernel for a given copy-width
(copyKind — the CopyKindFor(long) of the dtype
itemsize; always 1/2/4/8/16 for real dtypes, so the store is a typed MOV
rather than a per-element cpblk). Returns null when
Enabled is false or emission fails, so callers can fall back
to the SetData path.
public static DiagWriteKernel GetDiagWriteKernel(int copyKind)
Parameters
copyKindint
Returns
GetDivModKernel(NPTypeCode)
Get or generate the fused divmod kernel for dt. Returns null
when IL generation is disabled or the dtype has no Divmod* helper (Boolean — never
reaches here, promoted to int8; Complex — divmod is unsupported), so the caller falls
back to composing floor_divide + remainder.
public static DirectILKernelGenerator.DivModKernel GetDivModKernel(NPTypeCode dt)
Parameters
dtNPTypeCode
Returns
GetElementCopyKernel(int, int)
The ElementCopyKernel that moves copyBytes bytes per element and NUL-fills
zeroBytes more, generated on first use and cached.
public static ElementCopyKernel GetElementCopyKernel(int copyBytes, int zeroBytes)
Parameters
copyBytesintThe bytes copied from each source element (at least 1).
zeroBytesintThe NUL bytes written after them (0 when the destination is not wider).
Returns
- ElementCopyKernel
The kernel, or null when IL generation is disabled or failed (the caller takes its general route).
Exceptions
- ArgumentOutOfRangeException
copyBytesis not positive, orzeroBytesis negative.
GetFilterAxisKernel(long)
IL-emitted kernel cached by innerSize. Pass the
actual innerSize you'll use at call time; the function buckets
{1,2,4,8,16} into typed-copy variants and anything else into the
bulk-cpblk variant. Returns null only when
Enabled is false.
public static FilterAxisKernel GetFilterAxisKernel(long innerSize)
Parameters
innerSizelong
Returns
GetGemvKernel(NPTypeCode)
Get the matrix·vector (gemv) kernel for dt, or null
when out of scope (only Single / Double — the dtypes NumPy routes through cblas
gemv), IL/SIMD unavailable, or Vector256 not hardware accelerated. A null
return routes the caller back to the general GEMM.
public static DirectILKernelGenerator.GemvKernel GetGemvKernel(NPTypeCode dt)
Parameters
dtNPTypeCode
Returns
GetGramKernel(NPTypeCode)
Get the symmetric-Gram kernel for dt, or null when
the dtype is outside scope (only Single / Double are served — cov's real
result dtypes), IL/SIMD is unavailable, or Vector256 is not hardware
accelerated. A null return routes the caller back to np.dot.
public static DirectILKernelGenerator.GramKernel GetGramKernel(NPTypeCode dt)
Parameters
dtNPTypeCode
Returns
GetHistogramCountKernel(NPTypeCode)
Returns the cached fused count kernel for compute dtype B (Half/Single/Double).
public static DirectILKernelGenerator.HistogramCountKernel GetHistogramCountKernel(NPTypeCode binDtype)
Parameters
binDtypeNPTypeCodeThe compute dtype B.
Returns
- DirectILKernelGenerator.HistogramCountKernel
The cached DirectILKernelGenerator.HistogramCountKernel.
Exceptions
- NotSupportedException
The dtype is not a supported float compute width.
GetHistogramIndicesKernel(NPTypeCode)
Returns the cached fused bin-index kernel for the given compute dtype B.
public static DirectILKernelGenerator.HistogramIndicesKernel GetHistogramIndicesKernel(NPTypeCode binDtype)
Parameters
binDtypeNPTypeCodeThe bin/compute dtype — one of Half, Single or Double. Any other value throws, because np.histogram resolves the bin dtype to exactly these three float widths before reaching the kernel.
Returns
- DirectILKernelGenerator.HistogramIndicesKernel
The cached DirectILKernelGenerator.HistogramIndicesKernel for
binDtype.
Exceptions
- NotSupportedException
The dtype is not one of the three float compute widths.
GetHistogramRangeCountKernel(NPTypeCode)
Returns the cached fused count kernel for float16 / float32 data counted over a range: the DirectILKernelGenerator.HistogramCountKernel contract, with NumPy's float64 quotient (see the file header).
public static DirectILKernelGenerator.HistogramCountKernel GetHistogramRangeCountKernel(NPTypeCode binDtype)
Parameters
binDtypeNPTypeCodeThe data's and the edges' dtype: Half or Single (float64 data's quotient is float64 either way: use GetHistogramCountKernel(NPTypeCode)).
Returns
- DirectILKernelGenerator.HistogramCountKernel
The cached kernel. It throws IndexError for a kept sample whose index passes the edges.
Exceptions
- NotSupportedException
Another dtype.
GetHistogramRangeWeightedKernel(NPTypeCode)
Returns the cached fused weighted-scatter kernel for float16 / float32 data counted over a range: the DirectILKernelGenerator.HistogramWeightedKernel contract (one block, float64 totals), with NumPy's float64 quotient.
public static DirectILKernelGenerator.HistogramWeightedKernel GetHistogramRangeWeightedKernel(NPTypeCode binDtype)
Parameters
binDtypeNPTypeCode
Returns
- DirectILKernelGenerator.HistogramWeightedKernel
The cached kernel. It throws IndexError for a kept sample whose index passes the edges.
Exceptions
- NotSupportedException
Another dtype.
GetHistogramSearchCountKernel(NPTypeCode)
Returns the cached fused non-uniform search+count kernel for the given compare dtype.
public static DirectILKernelGenerator.HistogramSearchCountKernel GetHistogramSearchCountKernel(NPTypeCode compareDtype)
Parameters
compareDtypeNPTypeCodeThe dtype both samples and edges are cast to (their common type).
Returns
- DirectILKernelGenerator.HistogramSearchCountKernel
The cached kernel, or null when the dtype has no ordering (Complex — caller composes instead).
GetHistogramSearchIndexKernel(NPTypeCode)
Returns the cached fused search-index kernel for the given compare dtype (null for Complex).
public static DirectILKernelGenerator.HistogramSearchIndexKernel GetHistogramSearchIndexKernel(NPTypeCode compareDtype)
Parameters
compareDtypeNPTypeCodeThe dtype samples and edges are cast to (their common type).
Returns
- DirectILKernelGenerator.HistogramSearchIndexKernel
The cached kernel, or null when the dtype has no ordering.
GetHistogramWeightedKernel(NPTypeCode)
Returns the cached fused weighted-scatter kernel for compute dtype B (Half/Single/Double).
public static DirectILKernelGenerator.HistogramWeightedKernel GetHistogramWeightedKernel(NPTypeCode binDtype)
Parameters
binDtypeNPTypeCodeThe compute dtype B.
Returns
- DirectILKernelGenerator.HistogramWeightedKernel
The cached DirectILKernelGenerator.HistogramWeightedKernel.
Exceptions
- NotSupportedException
The dtype is not a supported float compute width.
GetIndicesKernel()
IL-emitted indices fill kernel (singleton — same kernel handles any ndim).
Returns null only when Enabled is false.
public static IndicesKernel GetIndicesKernel()
Returns
GetMaskGatherKernel(int)
The mask gather kernel for one slab width: copyKind is the
FixedCopyKindFor(long) of the slab (its byte count, baked in), or 0 for a slab wider than
NumSharp.Backends.Kernels.DirectILKernelGenerator.MaxFixedCopyBytes (copied with the runtime count).
public static MaskGatherKernel GetMaskGatherKernel(int copyKind)
Parameters
copyKindintThe copy kind.
Returns
- MaskGatherKernel
The kernel, or
nullwhen runtime IL generation is unavailable (Enabled false, or the emission failed: the caller keeps its general route).
GetMixedTypeKernel(MixedTypeKernelKey)
Get or generate a mixed-type kernel for the specified key.
public static MixedTypeKernel GetMixedTypeKernel(MixedTypeKernelKey key)
Parameters
Returns
GetNonZeroPerDimKernel()
IL-emitted per-dim coord expander (singleton — same kernel handles any ndim).
Returns null only when Enabled is false.
public static NonZeroPerDimKernel GetNonZeroPerDimKernel()
Returns
GetPiecewiseScalarKernel(NPTypeCode, int)
Get or generate a fused piecewise scalar kernel for (dtype,
n conditions). Returns null if IL generation is disabled or
the dtype is not SIMD-eligible for this kernel (the caller then uses the composition).
public static PiecewiseScalarKernel GetPiecewiseScalarKernel(NPTypeCode dtype, int n)
Parameters
dtypeNPTypeCodenint
Returns
GetPlaceKernel(int)
IL-emitted place kernel, generated once per copy-width (copyKind — the
CopyKindFor(long) of the dtype itemsize; always 1/2/4/8/16 for real dtypes, so the
masked write is a typed MOV rather than a per-element cpblk).
Returns null only when Enabled is false.
public static PlaceKernel GetPlaceKernel(int copyKind)
Parameters
copyKindint
Returns
GetPredicateContiguousKernel(UnaryOp, NPTypeCode)
Dedicated SIMD contiguous kernel for the float-classification
predicates (IsNan / IsInf /
IsFinite) over Single /
Double input — the port of NumPy's vectorized
loops_unary_fp_le.dispatch.c.src loops (npyv_pack_isnan/isinf/isfinite).
The general unary route cannot vectorize these: its SIMD gate (CanUseUnarySimd(UnaryKernelKey)) requires input dtype == output dtype and a predicate is inherently float→bool, so IsNan/IsInf/IsFinite fell to a per-element scalar walk (measured 40.6 µs vs NumPy's 4.1 µs on 100K float32 isnan). Lane logic per vector, all in the float domain so no reinterpret casts are needed: isnan: NOT Equals(v, v) — NaN is the only self-unequal value isinf: Equals(Abs(v), +inf) — Abs(NaN) is NaN, NaN == inf is false isfinite: LessThan(Abs(v), +inf) — NaN < inf is false, inf < inf is false then the comparison kernels' mask→bool store (ExtractMostSignificantBits + BMI2 PDEP → one sized store per vector, per-lane fallback without BMI2).
Returns null when the (op, dtype) pair is outside this kernel's scope — integer/Half/Complex inputs keep the existing scalar routes (NumPy's own HALF/CFLOAT predicate loops are scalar too), and callers fall back to GetUnaryKernel(UnaryKernelKey).
public static UnaryKernel? GetPredicateContiguousKernel(UnaryOp op, NPTypeCode inputType)
Parameters
opUnaryOpinputTypeNPTypeCode
Returns
GetPredicateStridedKernel(UnaryOp, NPTypeCode)
Fused strided kernel for IsNan/IsInf/IsFinite over a 1-D strided
(stepped / reversed / offset) Single/Double source writing a fresh
contiguous bool result — NumPy's simd_unary_KIND_TYPE_NCONTIG_CONTIG
shape: each vector is assembled from lanes strided scalar loads
(CreateElements(int, Type), the npyv_loadn
equivalent NumPy prefers over hardware gather for these cheap lane ops),
the predicate mask applied, and the bools stored via the PDEP pack.
Replaces the scalar gather-to-tile route, which paid ~1 ns/element on
the gather alone (70 µs vs NumPy's 4.9 µs on a 50K stride-2 isnan).
Same (op, dtype) gates as GetPredicateContiguousKernel(UnaryOp, NPTypeCode).
public static StridedUnaryKernel? GetPredicateStridedKernel(UnaryOp op, NPTypeCode inputType)
Parameters
opUnaryOpinputTypeNPTypeCode
Returns
GetPutAlongAxisScatterKernel(int)
The IL scatter kernel for np.put_along_axis, cached per element width
(elemBytes ∈ {1,2,4,8,16}). Returns null only when
Enabled is false or IL generation fails.
public static PutAlongAxisScatterKernel GetPutAlongAxisScatterKernel(int elemBytes)
Parameters
elemBytesint
Returns
GetPutAlongAxisValidateKernel()
The IL index-validation kernel for np.put_along_axis (dtype-agnostic, generated
once). Returns null only when Enabled is false or IL generation fails.
public static PutAlongAxisValidateKernel GetPutAlongAxisValidateKernel()
Returns
GetPutFlatKernel(int, bool)
The flat scatter kernel for one element width and index width; null under the same
conditions as GetTakeFlatKernel(int, bool, bool).
public static PutFlatKernel GetPutFlatKernel(int copyKind, bool idx32)
Parameters
Returns
GetPutKernel(int)
IL-emitted put kernel, generated once per copy-width (copyKind — the
CopyKindFor(long) of the dtype itemsize; always 1/2/4/8/16 for real dtypes, so the
scatter copy is a typed MOV rather than a per-element cpblk). Handles any mode.
Returns null only when Enabled is false.
public static PutKernel GetPutKernel(int copyKind)
Parameters
copyKindint
Returns
GetPutKernel(int, bool)
As GetPutKernel(int), with the index width selectable: idx32
generates a kernel that reads the indices argument as int32 values (the
delegate still types the pointer long* — the caller reinterprets its int*),
so an int32 index array is scattered in place instead of widened to an int64 copy first.
public static PutKernel GetPutKernel(int copyKind, bool idx32)
Parameters
Returns
GetPutMaskKernel(int)
IL-emitted putmask kernel, generated once per copy-width (copyKind — the
CopyKindFor(long) of the dtype itemsize; always 1/2/4/8/16 for real dtypes, so the
masked write is a typed MOV rather than a per-element cpblk).
Returns null only when Enabled is false.
public static PutMaskKernel GetPutMaskKernel(int copyKind)
Parameters
copyKindint
Returns
GetRavelMultiIndexKernel()
IL-emitted multi→flat folder (singleton — same kernel handles any
ndim, both orders, and arbitrary per-axis mode tuples). Returns
null only when Enabled is false.
public static RavelMultiIndexKernel GetRavelMultiIndexKernel()
Returns
GetRepeatBroadcastKernel(int)
Returns the cached IL-emitted broadcast-repeat kernel for the given slab size. First call for a size triggers IL generation, later calls hit a dictionary lookup.
public static DirectILKernelGenerator.RepeatBroadcastKernel GetRepeatBroadcastKernel(int chunkBytes)
Parameters
chunkBytesint
Returns
GetRepeatPerJKernel(int)
Returns the cached IL-emitted per-j repeat kernel for the given slab size.
public static DirectILKernelGenerator.RepeatPerJKernel GetRepeatPerJKernel(int chunkBytes)
Parameters
chunkBytesint
Returns
GetReversedUnaryKernel(UnaryKernelKey)
Get or generate the reversed-run unary kernel of key: the
StridedUnaryKernel signature for a REVERSED source (a[::-1], byte stride −elemSize),
which loads each vector with one contiguous load and a lane reversal (InnerLoop.Reversed.cs) where the
strided kernel assembles it from lane-count scalar loads.
public static StridedUnaryKernel GetReversedUnaryKernel(UnaryKernelKey key)
Parameters
keyUnaryKernelKeyA same-width key (
InputType == OutputType) whose op has a vector body, on a dtype CanReverseLanes(NPTypeCode) approves; the caller checks both.
Returns
- StridedUnaryKernel
The kernel. Its
srcByteStrideargument is ignored: the source MUST be reversed.
Exceptions
- InvalidOperationException
IL generation is disabled.
- NotSupportedException
The op has no vector or scalar emission for the dtype.
GetSearchSortedKernel(NPTypeCode, bool, bool, bool)
Get or generate a searchsorted kernel.
public static DirectILKernelGenerator.SearchSortedKernel GetSearchSortedKernel(NPTypeCode type, bool leftSide, bool hasSorter, bool contiguousA)
Parameters
typeNPTypeCodeElement dtype of a (and contiguous v, after caller normalizes).
leftSidebooltrue = side='left', false = side='right'.
hasSorterbooltrue = sorter param non-null (kernel emits sort_idx indirection).
contiguousAbooltrue = a is contiguous (arrStrideBytes == elemSize). Lets JIT use scaled-index addressing instead of imul, which closes the gap to NumPy on the random-key hot path. When false, arrStrideBytes is honored as a runtime parameter.
Returns
GetSelectKernel(NPTypeCode, int, bool)
Get or generate a fused select kernel for (dtype, n
conditions, defScalar). Returns null if IL generation is disabled or
the dtype is not SIMD-eligible for this kernel (the caller then uses the composition).
public static SelectFusedKernel GetSelectKernel(NPTypeCode dtype, int n, bool defScalar)
Parameters
dtypeNPTypeCodenintdefScalarbool
Returns
GetShiftArrayKernel<T>(bool)
Get or generate a shift kernel for element-wise shift amounts.
public static DirectILKernelGenerator.ShiftArrayKernel<T>? GetShiftArrayKernel<T>(bool isLeftShift) where T : unmanaged
Parameters
isLeftShiftboolTrue for left shift, false for right shift
Returns
- DirectILKernelGenerator.ShiftArrayKernel<T>
Kernel delegate or null if not supported
Type Parameters
TInteger element type
GetShiftScalarKernel<T>(bool)
Get or generate a SIMD-optimized shift kernel for uniform shift amount.
public static DirectILKernelGenerator.ShiftScalarKernel<T>? GetShiftScalarKernel<T>(bool isLeftShift) where T : unmanaged
Parameters
isLeftShiftboolTrue for left shift, false for right shift
Returns
- DirectILKernelGenerator.ShiftScalarKernel<T>
Kernel delegate or null if not supported
Type Parameters
TInteger element type
GetStridedUnaryKernel(UnaryKernelKey)
Get or generate a fused strided-SIMD unary kernel for the given key. Gating (same-width SIMD-capable op, supported dtype) is the caller's responsibility.
public static StridedUnaryKernel GetStridedUnaryKernel(UnaryKernelKey key)
Parameters
keyUnaryKernelKey
Returns
GetTakeAlongAxisKernel(int)
IL-emitted take_along_axis gather kernel, cached per element width
(elemBytes ∈ {1,2,4,8,16}). Returns null only
when Enabled is false.
public static TakeAlongAxisKernel GetTakeAlongAxisKernel(int elemBytes)
Parameters
elemBytesint
Returns
GetTakeFlatKernel(int, bool, bool)
The flat gather kernel for one element width (copyKind: the
FixedCopyKindFor(long) of the per-index slab, 1 to NumSharp.Backends.Kernels.DirectILKernelGenerator.MaxFixedCopyBytes bytes) and
index width. null when runtime IL generation is unavailable or the slab is wider than a constant
copy (copyKind 0 — the general TakeKernel serves those with a runtime cpblk).
public static TakeFlatKernel GetTakeFlatKernel(int copyKind, bool idx32, bool prefetch)
Parameters
Returns
GetTakeKernel(int, bool)
IL-emitted take kernel, generated once per (copy-width, prefetch) pair. copyKind
is the CopyKindFor(long) or FixedCopyKindFor(long) of the per-index slab size (1/2/4/8/16 for a
typed MOV copy, any other positive width for a constant-size cpblk, 0 for a runtime-sized one; the slab
is ADDRESSED with the runtime innerSize either way); prefetch emits the software-prefetch
inner body (a large-source win, a small-source pessimization — the caller gates it on the
gathered region exceeding cache). One kernel handles any ndim, both axis=None and axis=k.
Returns null only when Enabled is false.
public static TakeKernel GetTakeKernel(int copyKind, bool prefetch)
Parameters
Returns
GetTakeKernel(int, bool, bool)
As GetTakeKernel(int, bool), with the index width selectable:
idx32 generates a kernel that reads the indices argument as
int32 values (the pointer is still typed long* by the delegate — the
caller reinterprets its int*), so an int32 index array is gathered in place
instead of being widened to a temporary int64 copy first.
public static TakeKernel GetTakeKernel(int copyKind, bool prefetch, bool idx32)
Parameters
Returns
GetTakeLeadingKernel(int)
The IL gather writing NumPy's fancy-index buffer order (indexed axis first) in one pass, compiled once per
copy width: copyKind 1/2/4/8/16 copies each slab with typed MOVs in a 4x-unrolled
loop; 0 copies runtime-sized slabs with cpblk (large slabs, where the loop overhead is moot).
public static TakeLeadingKernel GetTakeLeadingKernel(int copyKind)
Parameters
copyKindintThe CopyKindFor(long) of the slab size in bytes.
Returns
- TakeLeadingKernel
The kernel, or
nullwhen IL generation is switched off (Enabled false) or the emission failed — the caller then falls back tonp.takeover a moved view, which builds the same buffer after a failed emission but needs an IL kernel of its own (with Enabled false it raises NotSupportedException).
GetTraceAccumTypeCode(NPTypeCode)
Maps src NPTypeCode → (result NPTypeCode, supported). Convenience for callers that already have the type-code.
public static (NPTypeCode, bool) GetTraceAccumTypeCode(NPTypeCode src)
Parameters
srcNPTypeCode
Returns
- (NPTypeCode, bool)
GetTraceKernel(Type)
IL-emitted singleton per srcType. Returns
null when the dtype has no kernel (no triples are
unsupported in the current implementation; the field exists for
graceful future expansion).
public static TraceKernel GetTraceKernel(Type srcType)
Parameters
srcTypeType
Returns
GetTypedElementReductionKernel<TResult>(ElementReductionKernelKey)
Get or generate a typed element-wise reduction kernel. Returns a delegate that reduces all elements to a single value of type TResult.
public static TypedElementReductionKernel<TResult> GetTypedElementReductionKernel<TResult>(ElementReductionKernelKey key) where TResult : unmanaged
Parameters
Returns
- TypedElementReductionKernel<TResult>
Type Parameters
TResult
GetUnaryKernel(UnaryKernelKey)
Get or generate a unary kernel for the specified key.
public static UnaryKernel GetUnaryKernel(UnaryKernelKey key)
Parameters
keyUnaryKernelKey
Returns
GetUnaryScalarDelegate(UnaryScalarKernelKey)
Get or generate an IL-based unary scalar delegate. Returns a Func<TInput, TOutput> delegate.
public static Delegate GetUnaryScalarDelegate(UnaryScalarKernelKey key)
Parameters
Returns
GetUnravelIndexKernel()
IL-emitted unravel kernel (singleton — same kernel handles any ndim and
both C / F order via the runtime idxStart / idxStep args).
Returns null only when Enabled is false.
public static UnravelIndexKernel GetUnravelIndexKernel()
Returns
GetUnravelIndexMagicKernel()
IL-emitted reciprocal unravel kernel (singleton — any ndim, both orders). Returns null only when
Enabled is false or the emission fails (the caller then has no kernel at all, as with
GetUnravelIndexKernel()).
public static UnravelIndexMagicKernel GetUnravelIndexMagicKernel()
Returns
Remarks
Why it exists: the hardware 64-bit divide costs ~15-40 cycles and RyuJIT emits one for the remainder and another for the quotient of the same operands, so the divmod kernel ran two divides per peeled dimension — NumPy's own speed (its C compiler fuses them into one, but it peels every dimension). A multiply-high, a subtract, two shifts and a multiply-subtract do the same work in a few cycles, independent elements overlapping in the pipeline.
GetWeightedSumIterKernel(WeightedSumKernelKey)
Returns the cached IL-emitted weighted-sum kernel for the given dtype, or null if the dtype isn't supported (Bool/Char/Half/Complex/Decimal). The kernel signature matches NDInnerLoopFunc; pass it to NDIter.ForEach over the 4-operand [a, w, num_out, scl_out] iterator.
public static NDInnerLoopFunc? GetWeightedSumIterKernel(DirectILKernelGenerator.WeightedSumKernelKey key)
Parameters
Returns
GetWhereBytesKernel(int)
The WhereBytesKernel for elements of width bytes, generated on first use and
cached.
public static WhereBytesKernel GetWhereBytesKernel(int width)
Parameters
widthintThe element width in bytes (at least 1).
Returns
- WhereBytesKernel
The kernel, or null when IL generation is disabled (Enabled) or failed — the caller then takes its general route.
Exceptions
- ArgumentOutOfRangeException
widthis not positive.
GetWhereBytesStepKernel(int)
The WhereBytesStepKernel for elements of width bytes, generated on first use and
cached: the select of GetWhereBytesKernel(int) for inputs that are not all contiguous — a broadcast
one-element branch or condition, a strided or reversed 1-D view.
public static WhereBytesStepKernel GetWhereBytesStepKernel(int width)
Parameters
widthintThe element width in bytes (at least 1).
Returns
- WhereBytesStepKernel
The kernel, or null when IL generation is disabled (Enabled) or failed — the caller then takes its general route.
Exceptions
- ArgumentOutOfRangeException
widthis not positive.
GetWhereKernel<T>()
Get or generate an IL-based where kernel for the specified type. Returns null if IL generation is disabled or fails.
public static WhereKernel<T>? GetWhereKernel<T>() where T : unmanaged
Returns
- WhereKernel<T>
Type Parameters
T
GetWhereScalarXKernel<T>()
public static WhereScalarXKernel<T> GetWhereScalarXKernel<T>() where T : unmanaged
Returns
Type Parameters
T
GetWhereScalarXYKernel<T>()
public static WhereScalarXYKernel<T> GetWhereScalarXYKernel<T>() where T : unmanaged
Returns
Type Parameters
T
GetWhereScalarYKernel<T>()
public static WhereScalarYKernel<T> GetWhereScalarYKernel<T>() where T : unmanaged
Returns
Type Parameters
T
LdexpHelper(decimal*, int*, decimal*, long)
Element-wise ldexp for Decimal over an int32-exponent buffer (double bridge).
public static void LdexpHelper(decimal* x, int* exp, decimal* result, long n)
Parameters
xdecimal*Decimal mantissa/base values.
expint*Per-element exponents (int32).
resultdecimal*Receives
(decimal)(x[i] * 2^exp[i]).nlongElement count.
Exceptions
- OverflowException
A scaled value overflows the decimal range (double product ±inf).
LdexpHelper(decimal*, long*, decimal*, long)
Element-wise ldexp for Decimal through the double bridge (a NumSharp extension).
public static void LdexpHelper(decimal* x, long* exp, decimal* result, long n)
Parameters
xdecimal*Decimal mantissa/base values.
explong*Per-element exponents (int64, clamped to int range before scaling).
resultdecimal*Receives
(decimal)(x[i] * 2^exp[i]).nlongElement count.
Exceptions
- OverflowException
A scaled value overflows the decimal range (e.g. a huge exponent drives the double product to ±inf, which does not convert back to decimal) — the same bridge limit shared by every decimal transcendental in the engine.
LdexpHelper(double*, int*, double*, long)
Element-wise ldexp over contiguous double + int32-exponent buffers (no clamp needed).
public static void LdexpHelper(double* x, int* exp, double* result, long n)
Parameters
xdouble*Mantissa/base values.
expint*Per-element exponents (int32, always in the C-int range).
resultdouble*Receives
x[i] * 2^exp[i].nlongElement count.
LdexpHelper(double*, long*, double*, long)
Element-wise ldexp over contiguous double + int64-exponent buffers into a double result.
public static void LdexpHelper(double* x, long* exp, double* result, long n)
Parameters
xdouble*Mantissa/base values.
explong*Per-element exponents (int64, clamped to int range before scaling).
resultdouble*Receives
x[i] * 2^exp[i].nlongElement count.
LdexpHelper(Half*, int*, Half*, long)
Element-wise ldexp for Half over an int32-exponent buffer (widen-compute-narrow).
public static void LdexpHelper(Half* x, int* exp, Half* result, long n)
Parameters
xHalf*Half mantissa/base values.
expint*Per-element exponents (int32).
resultHalf*Receives
(Half)(x[i] * 2^exp[i]).nlongElement count.
LdexpHelper(Half*, long*, Half*, long)
Element-wise ldexp for Half: widen to float, scale, narrow back (NumPy's HALF_ldexp).
public static void LdexpHelper(Half* x, long* exp, Half* result, long n)
Parameters
xHalf*Half mantissa/base values.
explong*Per-element exponents (int64, clamped to int range before scaling).
resultHalf*Receives
(Half)(x[i] * 2^exp[i]).nlongElement count.
LdexpHelper(float*, int*, float*, long)
Element-wise ldexp over contiguous float + int32-exponent buffers (no clamp needed).
public static void LdexpHelper(float* x, int* exp, float* result, long n)
Parameters
xfloat*Mantissa/base values.
expint*Per-element exponents (int32).
resultfloat*Receives
x[i] * 2^exp[i].nlongElement count.
LdexpHelper(float*, long*, float*, long)
Element-wise ldexp over contiguous float + int64-exponent buffers into a float result.
public static void LdexpHelper(float* x, long* exp, float* result, long n)
Parameters
xfloat*Mantissa/base values.
explong*Per-element exponents (int64, clamped to int range before scaling).
resultfloat*Receives
x[i] * 2^exp[i].nlongElement count.
LdexpScalarExp(decimal*, int, decimal*, long)
Scalar-exponent fast path for Decimal (double bridge).
public static void LdexpScalarExp(decimal* x, int e, decimal* result, long n)
Parameters
xdecimal*Decimal mantissa/base values.
eintThe single exponent, already clamped to int range.
resultdecimal*Receives
(decimal)(x[i] * 2^e).nlongElement count.
Exceptions
- OverflowException
A scaled value overflows the decimal range (double product ±inf).
LdexpScalarExp(double*, int, double*, long)
Scalar-exponent fast path for double: scale a whole buffer by ONE (already-clamped) exponent
without materializing a per-element exponent array — the common np.ldexp(array, k) shape.
public static void LdexpScalarExp(double* x, int e, double* result, long n)
Parameters
xdouble*Mantissa/base values.
eintThe single exponent, already clamped to int range.
resultdouble*Receives
x[i] * 2^e.nlongElement count.
LdexpScalarExp(Half*, int, Half*, long)
Scalar-exponent fast path for Half (widen-compute-narrow).
public static void LdexpScalarExp(Half* x, int e, Half* result, long n)
Parameters
xHalf*Half mantissa/base values.
eintThe single exponent, already clamped to int range.
resultHalf*Receives
(Half)(x[i] * 2^e).nlongElement count.
LdexpScalarExp(float*, int, float*, long)
Scalar-exponent fast path for float (see LdexpScalarExp(double*, int, double*, long)).
public static void LdexpScalarExp(float* x, int e, float* result, long n)
Parameters
xfloat*Mantissa/base values.
eintThe single exponent, already clamped to int range.
resultfloat*Receives
x[i] * 2^e.nlongElement count.
ModfHelper(double*, double*, double*, long)
Out-of-place SIMD Modf for contiguous double arrays: reads input once and
writes fractional/integral to their own buffers (NumPy's 3-touch pattern). See the float
overload for the aliasing and special-value contract.
public static void ModfHelper(double* input, double* frac, double* integral, long size)
Parameters
inputdouble*Source array (read-only unless it aliases an output).
fracdouble*Destination for the fractional parts.
integraldouble*Destination for the integral parts.
sizelongNumber of elements.
ModfHelper(double*, double*, long)
In-place SIMD Modf wrapper for contiguous double arrays (see the float overload). Forwards to the out-of-place kernel with input==frac.
public static void ModfHelper(double* data, double* integral, long size)
Parameters
datadouble*Input array (holds the fractional parts on return).
integraldouble*Output array for the integral parts.
sizelongNumber of elements.
ModfHelper(Half*, Half*, Half*, long)
Out-of-place Half modf: reads input once, writes fractional/integral to their
own buffers (see the float overload; NumPy's HALF_modf is scalar so this is a scalar loop).
public static void ModfHelper(Half* input, Half* frac, Half* integral, long size)
Parameters
inputHalf*Source array (read-only unless it aliases an output).
fracHalf*Destination for the fractional parts.
integralHalf*Destination for the integral parts.
sizelongNumber of elements.
ModfHelper(Half*, Half*, long)
Modf over a contiguous Half array (scalar loop; see ModfScalar(Half, out Half, out Half)).
Byte-exact with NumPy's HALF_modf. Writes fractional parts back into data.
public static void ModfHelper(Half* data, Half* integral, long size)
Parameters
dataHalf*Input array (holds the fractional parts on return).
integralHalf*Output array for the integral parts.
sizelongNumber of elements.
ModfHelper(float*, float*, long)
In-place SIMD Modf wrapper for contiguous float arrays: data holds the input
on entry and the fractional parts on return. Kept for callers that mutate the input copy in
place; forwards to the out-of-place kernel with input==frac (which is a safe per-element alias).
public static void ModfHelper(float* data, float* integral, long size)
Parameters
datafloat*Input array (holds the fractional parts on return).
integralfloat*Output array for the integral parts.
sizelongNumber of elements.
ModfHelper(float*, float*, float*, long)
Out-of-place SIMD Modf for contiguous float arrays: reads input ONCE and
writes the fractional and integral parts to their own buffers — NumPy's 3-touch pattern (1 read,
2 writes), avoiding the extra input-copy + write-back pass an in-place kernel needs when the
caller supplies output arrays. Any two of the three pointers may alias per-element (input==frac
gives the in-place case; a provided out that equals the input is handled the same way). Handles
the special values (NaN, ±inf, signed zero) per C standard modf.
public static void ModfHelper(float* input, float* frac, float* integral, long size)
Parameters
inputfloat*Source array (read-only unless it aliases an output).
fracfloat*Destination for the fractional parts.
integralfloat*Destination for the integral parts.
sizelongNumber of elements.
NanToNum(NPTypeCode, void*, void*, long, void*, void*, void*)
Run a nan_to_num pass. Picks (and on first call IL-generates) the kernel
for dtype (Half/Single/Double) and invokes it.
public static void NanToNum(NPTypeCode dtype, void* src, void* dst, long size, void* nan, void* posinf, void* neginf)
Parameters
PiecewiseScalarKernelSupportsDtype(NPTypeCode)
True when the fused piecewise scalar kernel supports dtype — the
1/2/4/8-byte numeric dtypes (shared with the select kernel). Bool/Char/Half/Decimal/
Complex stay on the composition.
public static bool PiecewiseScalarKernelSupportsDtype(NPTypeCode dtype)
Parameters
dtypeNPTypeCode
Returns
Quantile(NPTypeCode, NPTypeCode, NPTypeCode, NPTypeCode, QuantileMethod, void*, void*, long, int, int*, int, int*, double*, double*, int, void*, long, bool, int*, long*, double*, bool, int*, int)
Run the cached quantile kernel for the given dtypes. First call for a tuple emits and caches the DynamicMethod; later calls jump straight into the specialized native code.
public static void Quantile(NPTypeCode srcType, NPTypeCode outType, NPTypeCode lerpType, NPTypeCode qType, QuantileMethod method, void* srcBase, void* scratchBase, long outer, int n, int* kSorted, int nKs, int* cellIndex, double* cellWeight, double* q, int nQs, void* dstBase, long dstOuterStride, bool ignoreNaN = false, int* rowKScratch = null, long* rowRaw = null, double* rowWeight = null, bool medianMean = false, int* kExact = null, int nKExact = 0)
Parameters
srcTypeNPTypeCodeThe data's dtype D.
outTypeNPTypeCodeThe buffer's dtype:
lerpType, or the caller's out dtype when it receives the lerp.lerpTypeNPTypeCodeThe lerp's dtype M (the data's dtype for a selection method).
qTypeNPTypeCodeq's precision: Half, Single or Double.
methodQuantileMethodThe method (after the engine's remaps).
srcBasevoid*The staged data.
scratchBasevoid*A scratch line.
outerlongThe number of lines.
nintThe line's length (at least 1).
kSortedint*The plain path's ascending needed partition indexes (the tie-blind select's list).
nKsintHow many.
cellIndexint*The plain path's per-quantile neighbours (partition-ready).
cellWeightdouble*The plain path's per-quantile weights.
qdouble*The quantiles.
nQsintHow many.
dstBasevoid*The result buffer.
dstOuterStridelongThe distance between two quantiles' blocks (0 for a scalar q).
ignoreNaNboolThe nan forms.
rowKScratchint*The nan path's partition indexes (2 per quantile).
rowRawlong*The nan path's neighbours (2 per quantile).
rowWeightdouble*The nan path's weights (2 per quantile).
medianMeanboolReduce even-size slices with NumPy's mean-of-middle
np.medianrule ((a+b)/2) instead of the quantile_lerpat q=0.5 — the two differ by up to 1 ULP. Set only bynp.median/np.nanmedian; baked into the cached kernel via NumSharp.Backends.Kernels.DirectILKernelGenerator.QuantileKey.MedianMean.kExactint*NumPy's own partition list for float data (see DirectILKernelGenerator.QuantileKernel); null with
nKExact0 for other data.nKExactintHow many positions
kExactholds.
Exceptions
- ValueError
The nan path: a line whose plan holds an index the partition refuses (NumPy's
kth(=k) out of bounds (m), m the line's NaN-free length) — only with a float16 / float32 q.
SelectKernelSupportsDtype(NPTypeCode)
True when the fused select kernel supports dtype — the 1/2/4/8-byte
numeric dtypes. Everything else stays on the copyto composition.
public static bool SelectKernelSupportsDtype(NPTypeCode dtype)
Parameters
dtypeNPTypeCode
Returns
TryGetAxisReductionKernel(AxisReductionKernelKey)
Try to get an axis reduction kernel. Supports all reduction operations and all types including type promotion. Uses SIMD for capable types, scalar loop for others.
public static AxisReductionKernel? TryGetAxisReductionKernel(AxisReductionKernelKey key)
Parameters
Returns
TryGetBooleanAxisReductionKernel(AxisReductionKernelKey)
Try to get a boolean axis reduction kernel (All / Any). Returns null for non-SIMD-capable dtypes (Half, Complex, Decimal, Char) so the caller can fall back to the NDAxisIter scalar path.
public static AxisReductionKernel? TryGetBooleanAxisReductionKernel(AxisReductionKernelKey key)
Parameters
Returns
TryGetCastKernel(NPTypeCode, NPTypeCode)
Get or generate a contig cast kernel for the given pair.
Returns null for unsupported pairs (Boolean/Char/Half/Complex/Decimal involved).
public static DirectILKernelGenerator.CastKernel TryGetCastKernel(NPTypeCode srcType, NPTypeCode dstType)
Parameters
srcTypeNPTypeCodedstTypeNPTypeCode
Returns
TryGetCopyKernel(CopyKernelKey)
public static CopyKernel? TryGetCopyKernel(CopyKernelKey key)
Parameters
keyCopyKernelKey
Returns
TryGetCumulativeAxisKernel(CumulativeAxisKernelKey)
Try to get or generate a cumulative axis kernel.
public static CumulativeAxisKernel? TryGetCumulativeAxisKernel(CumulativeAxisKernelKey key)
Parameters
Returns
TryGetCumulativeKernel(CumulativeKernelKey)
Try to get or generate a cumulative kernel.
public static CumulativeKernel? TryGetCumulativeKernel(CumulativeKernelKey key)
Parameters
Returns
TryGetInnerCastKernel(NPTypeCode, NPTypeCode)
Get or emit the scalar inner-loop cast kernel for the pair. Non-null for every
dtype pair (all 225 Converts.To{Dst}({Src}) methods exist); returns null
only if IL generation is disabled or a method unexpectedly fails to resolve.
public static DirectILKernelGenerator.InnerCastLoop TryGetInnerCastKernel(NPTypeCode srcType, NPTypeCode dstType)
Parameters
srcTypeNPTypeCodedstTypeNPTypeCode
Returns
TryGetMaskedAssignKernel(int)
The masked-assign kernel for elements of elementSize bytes, emitted on first use.
public static MaskedAssignKernel TryGetMaskedAssignKernel(int elementSize)
Parameters
elementSizeintThe element width in bytes: 1, 2, 4, 8 or 16 (the widths of the 15 dtypes).
Returns
- MaskedAssignKernel
The kernel, or
nullwhen IL generation is off (Enabled), the host has no dynamic code, the width is not one of the five, or the emission failed. Anullresult means "run MaskedAssignFallback(int, byte*, byte*, byte*, long*, long*, long*, long*, int)", which computes the same bits through the same run stage.
TryGetMaskedCastKernel(NPTypeCode, NPTypeCode)
Get or generate a masked-cast kernel for the given (src, dst) pair.
Returns null for unsupported pairs (Boolean/Char/Half/Complex/Decimal involved).
public static DirectILKernelGenerator.MaskedCastKernel TryGetMaskedCastKernel(NPTypeCode srcType, NPTypeCode dstType)
Parameters
srcTypeNPTypeCodedstTypeNPTypeCode
Returns
TryGetNanAxisReductionKernel(AxisReductionKernelKey)
Try to get a NaN-aware axis reduction kernel. SIMD kernels exist only for float/double; Half and Complex route to scalar fallback paths (Default.Reduction.Nan.cs ExecuteNanAxisReductionScalar / np.nanmean.cs / np.nanvar.cs / np.nanstd.cs) which handle them directly.
public static AxisReductionKernel? TryGetNanAxisReductionKernel(AxisReductionKernelKey key)
Parameters
Returns
TryGetStridedCastKernel(NPTypeCode, NPTypeCode)
Get or generate a strided/broadcast cast kernel for the given pair.
Returns null for unsupported pairs.
public static DirectILKernelGenerator.StridedCastKernel TryGetStridedCastKernel(NPTypeCode srcType, NPTypeCode dstType)
Parameters
srcTypeNPTypeCodedstTypeNPTypeCode
Returns
TryGetTypedElementReductionKernel<TResult>(ElementReductionKernelKey)
Try to get or generate an element reduction kernel.
public static TypedElementReductionKernel<TResult>? TryGetTypedElementReductionKernel<TResult>(ElementReductionKernelKey key) where TResult : unmanaged
Parameters
Returns
- TypedElementReductionKernel<TResult>
Type Parameters
TResult
WhereExecute<T>(bool*, T*, T*, T*, long)
Execute where operation using IL-generated kernel or fallback to static helper.
public static void WhereExecute<T>(bool* cond, T* x, T* y, T* result, long count) where T : unmanaged
Parameters
Type Parameters
T