import { f as o, c as a } from "../../webgpu_util-DuofJBMo.js";
import { e as s } from "../../webgpu_program-CbjdYLYk.js";
class h {
  outputShape;
  shaderKey = "Pack16";
  dispatchLayout;
  dispatch;
  workgroupSize = [64, 1, 1];
  variableNames = ["x"];
  uniforms;
  size = !0;
  outputComponent = 4;
  scaling = !1;
  padding = 0;
  constructor(t, e = 0) {
    if (t[t.length - 1] % 2 !== 0 && e === 0)
      throw new Error("Last dimension of output shape must be even to use Pack16.");
    if (e % 4 !== 0)
      throw new Error("Padding must be a multiple of 4 to use Pack16.");
    if (this.outputShape = [...t.slice(0, -1), t[t.length - 1]], e > 0) {
      this.shaderKey += `_Padded${e}`, this.padding = e;
      for (let i = this.outputShape.length - 2; i < this.outputShape.length; i++)
        this.outputShape[i] % this.padding !== 0 && (this.outputShape[i] += this.padding - this.outputShape[i] % this.padding);
      this.outputComponent = 1;
    }
    this.outputShape[this.outputShape.length - 1] /= 2, this.outputShape[this.outputShape.length - 1] % this.outputComponent !== 0 && (this.outputComponent = 1), this.dispatchLayout = o(this.outputShape), this.dispatch = a(this.dispatchLayout, this.outputShape, this.workgroupSize, [
      this.outputComponent,
      1,
      1
    ]);
  }
  useScaling() {
    this.shaderKey += "_Scaled", this.uniforms = "scaling : f32,", this.scaling = !0;
  }
  getUserCode() {
    if (this.padding > 0 && this.outputComponent === 1) {
      const t = this.outputShape.length;
      return `
                ${s("index")} {
                    if (index < uniforms.size) {
                        var coords = getCoordsFromIndex(index);
                        coords[${t} - 1] = coords[${t} - 1] * 2;
                        let row = coords[${t} - 2];
                        let col = coords[${t} - 1];
                        let width = uniforms.xShape[${t} - 1];
                        let height = uniforms.xShape[${t} - 2];
                    
                        var value1 = 0.0f;
                        if (col < width && row < height) {
                            let baseInputIndex = getIndexFromCoords${t}D(coords, uniforms.xShape);
                            value1 = x[baseInputIndex] ${this.scaling ? "* uniforms.scaling" : ""};
                        }
                        var value2 = 0.0f;
                        if (col + 1 < width && row < height) {
                            coords[${t} - 1] = coords[${t} - 1] + 1;
                            let baseInputIndex = getIndexFromCoords${t}D(coords, uniforms.xShape);
                            value2 = x[baseInputIndex] ${this.scaling ? "* uniforms.scaling" : ""};
                        }
                        let packed = i32(pack2x16float(vec2<f32>(value1, value2)));
                        result[index] = packed;
                    }
                }`;
    }
    return this.outputComponent === 1 ? `
                ${s("index")} {
                    if (index < uniforms.size) {
                        let baseInputIndex =  index * 2;
                        let x1 = x[baseInputIndex] ${this.scaling ? "* uniforms.scaling" : ""};
                        let x2 = x[baseInputIndex + 1] ${this.scaling ? "* uniforms.scaling" : ""};
                        let packed = i32(pack2x16float(vec2<f32>(x1, x2)));
                        result[index] = packed;
                    }
                }` : `
                ${s("index")} {
                    if (index < uniforms.size) {
                        let baseInputIndex =  index * 2;
                        let x1 = x[baseInputIndex] ${this.scaling ? "* uniforms.scaling" : ""};
                        let x2 = x[baseInputIndex + 1] ${this.scaling ? "* uniforms.scaling" : ""};
                        let packed = vec4<i32>(
                            i32(pack2x16float(vec2<f32>(x1.x, x1.y))),
                            i32(pack2x16float(vec2<f32>(x1.z, x1.w))),
                            i32(pack2x16float(vec2<f32>(x2.x, x2.y))),
                            i32(pack2x16float(vec2<f32>(x2.z, x2.w)))
                        );
                        result[index] = packed;
                    }
                }`;
  }
}
export {
  h as default
};
