// bend-blas, the JS twins: plain loops, correct and slow, for the // interpreter and the tests. An Array is a JS array of numbers. // A failure is io_fail(code): the runtime's message for a code is not // this library's, so the code is the contract on this lane. function bend_fresh(n) { let slots = 1; while (slots < n) slots *= 2; return new Array(slots).fill(0); } function bend_copy(src, n) { const c = bend_fresh(n); for (let i = 0; i < n; i++) c[i] = src[i]; return c; } function bend_short(a, n) { return n > 0x7fffffff || a.length < n; } function bend_gemm(ta, tb, m, n, k, alpha, a, b, beta, c) { const out = bend_copy(c, m * n); for (let i = 0; i < m; i++) for (let j = 0; j < n; j++) { let acc = 0; for (let l = 0; l < k; l++) acc += (ta ? a[l * m + i] : a[i * k + l]) * (tb ? b[j * k + l] : b[l * n + j]); out[i * n + j] = alpha * acc + beta * out[i * n + j]; } return out; } function blas_sgemv(ta, m, n, alpha, a, x, beta, y) { if (bend_short(a, m * n)) return io_fail(2); if (bend_short(x, ta ? m : n)) return io_fail(2); if (bend_short(y, ta ? n : m)) return io_fail(2); const rows = ta ? n : m, cols = ta ? m : n; const out = bend_copy(y, rows); for (let i = 0; i < rows; i++) { let acc = 0; for (let j = 0; j < cols; j++) acc += (ta ? a[j * n + i] : a[i * n + j]) * x[j]; out[i] = alpha * acc + beta * out[i]; } return io_done(out); } io_eff(CID(Blas.sgemv), blas_sgemv);