dtypeInfo¶
Summary¶
dtypeInfo looks up the storage representation for an ndarray element type. Use it to choose a compatible typed array, compute an element's byte size or describe packed elements in a shader.
Syntax¶
Parameters¶
| Name | Type | Required | Description |
|---|---|---|---|
dtype |
DType |
Yes | Real or complex ndarray dtype. An unknown dtype throws. |
Returns¶
DTypeInfo - Shared read-only metadata. Complex elements have two interleaved components in real, imaginary order.
Type Details¶
type RealDType = "i8" | "u8" | "i16" | "u16" | "i32" | "u32" | "f32" | "f64";
type ComplexDType = "c64" | "c128";
type DType = RealDType | ComplexDType;
type DTypeKind = "int" | "uint" | "float" | "complex";
type ComplexScalar = readonly [re: number, im: number];
type DTypeInfo<T extends NumberTypedArray = NumberTypedArray> = {
readonly dtype: DType;
readonly kind: DTypeKind;
readonly ctor: NumberTypedArrayConstructor<T>;
readonly components: 1 | 2;
readonly componentDType: RealDType;
readonly bytesPerElement: number;
readonly wgslElementType: "i32" | "u32" | "f32" | "vec2<f32>" | null;
};
Real dtypes use a typed array of the matching width and signedness. c64 uses pairs of f32 components in a Float32Array, and c128 uses pairs of f64 components in a Float64Array. wgslElementType is null when the dtype lacks a direct WGSL type. Available operations depend on the dtype and residency.
Example¶
import { dtypeInfo } from "@zushah/wasmgpu";
const info = dtypeInfo("c64");
const components = new info.ctor(new ArrayBuffer(info.bytesPerElement * 2));
components.set([1, -1, 2, -2]);