import { f as r, c as n } from "../../../webgpu_util-DuofJBMo.js";
import { g as p } from "../../../binary_op_util-pKXltfxI.js";
import { B as k } from "../../../binary_op_util-pKXltfxI.js";
import { _ as c, ai as s } from "../../../index-DSGwv2Yx.js";
import { e as u } from "../../../webgpu_program-CbjdYLYk.js";
class x {
  dispatch;
  dispatchLayout;
  outputComponent;
  op;
  outputShape;
  shaderKey;
  size = !0;
  variableNames = ["A", "B"];
  workgroupSize;
  variableComponents;
  constructor(e, t, o) {
    this.outputShape = c(t, o), this.dispatchLayout = r(this.outputShape), this.op = e;
    const i = t.length > 0 && t[t.length - 1] % 4 === 0, a = o.length > 0 && o[o.length - 1] % 4 === 0;
    if (i && a)
      this.outputComponent = 4, this.variableComponents = [4, 4];
    else throw i && (s(o) || o[o.length - 1] === 1) || a && (s(t) || t[t.length - 1] === 1) ? new Error("Cannot broadcast 16-bit float binary ops with mixed vector sizes") : new Error("16-bit float binary ops require inner dimension to be multiple of 4");
    this.shaderKey = `binary_${e}_${this.variableComponents}`, this.workgroupSize = [128, 1, 1], this.dispatch = n(this.dispatchLayout, this.outputShape, this.workgroupSize, [
      this.outputComponent,
      1,
      1
    ]);
  }
  getUserCode() {
    const e = this.outputComponent === 4 ? "vec4<f32>" : "f32";
    return `
       ${`
    fn binaryOperation(a : ${e}, b : ${e}) -> ${e} {
      ${p(this.op, this.outputComponent === 4)}
    };
    `}
       ${u("index")} {
         if (index < uniforms.size) {
            let a = A[index];
            let b = B[index];

            let v4a1 = vec4<f32>(
                unpack2x16float(u32(a.x)),
                unpack2x16float(u32(a.y))
            );
            let v4a2 = vec4<f32>(
                unpack2x16float(u32(a.z)),
                unpack2x16float(u32(a.w))
            );
            let v4b1 = vec4<f32>(
                unpack2x16float(u32(b.x)),
                unpack2x16float(u32(b.y))
            );
            let v4b2 = vec4<f32>(
                unpack2x16float(u32(b.z)),
                unpack2x16float(u32(b.w))
            );

            let v4res1 = binaryOperation(v4a1, v4b1);
            let v4res2 = binaryOperation(v4a2, v4b2);

            let res = vec4<i32>(
                i32(pack2x16float(v4res1.xy)),
                i32(pack2x16float(v4res1.zw)),
                i32(pack2x16float(v4res2.xy)),
                i32(pack2x16float(v4res2.zw))
            );

            result[index] = res;
         }
       }
       `;
  }
}
class b {
  dispatch;
  dispatchLayout;
  outputComponent;
  op;
  outputShape;
  shaderKey;
  size = !0;
  variableNames = ["A", "B"];
  workgroupSize;
  variableComponents;
  constructor(e, t) {
    if (this.outputShape = t, this.dispatchLayout = r(this.outputShape), this.op = e, t.length > 0 && t[t.length - 1] % 4 === 0)
      this.outputComponent = 4, this.variableComponents = [4, 1];
    else
      throw new Error("16-bit float binary ops require inner dimension to be multiple of 4");
    this.shaderKey = `binary_scal_${e}_${this.variableComponents}`, this.workgroupSize = [128, 1, 1], this.dispatch = n(this.dispatchLayout, this.outputShape, this.workgroupSize, [
      this.outputComponent,
      1,
      1
    ]);
  }
  getUserCode() {
    const e = this.outputComponent === 4 ? "vec4<f32>" : "f32";
    return `
       ${`
    fn binaryOperation(a : ${e}, b : ${e}) -> ${e} {
      ${p(this.op, this.outputComponent === 4)}
    };
    `}
       ${u("index")} {
         if (index < uniforms.size) {
            let a = A[index];
            let b = B[0];

            let v4a1 = vec4<f32>(
                unpack2x16float(u32(a.x)),
                unpack2x16float(u32(a.y))
            );
            let v4a2 = vec4<f32>(
                unpack2x16float(u32(a.z)),
                unpack2x16float(u32(a.w))
            );

            let v4res1 = binaryOperation(v4a1, vec4<f32>(b));
            let v4res2 = binaryOperation(v4a2, vec4<f32>(b));

            let res = vec4<i32>(
                i32(pack2x16float(v4res1.xy)),
                i32(pack2x16float(v4res1.zw)),
                i32(pack2x16float(v4res2.xy)),
                i32(pack2x16float(v4res2.zw))
            );

            result[index] = res;
         }
       }
       `;
  }
}
export {
  x as BinaryOpProgram,
  b as BinaryOpScalarProgram,
  k as BinaryOpType
};
