(shape: number[], name = '')
| 448 | * dimension coordinate. |
| 449 | */ |
| 450 | export function getCoordsFromIndexSnippet(shape: number[], name = ''): string { |
| 451 | const rank = shape.length; |
| 452 | const funcName = name !== '' ? |
| 453 | `get${name.charAt(0).toUpperCase() + name.slice(1)}CoordsFromIndex` : |
| 454 | 'getCoordsFromIndex'; |
| 455 | const stridesName = name !== '' ? |
| 456 | `${name.charAt(0).toLowerCase() + name.slice(1)}ShapeStrides` : |
| 457 | `outShapeStrides`; |
| 458 | |
| 459 | if (rank <= 1) { |
| 460 | return `fn ${funcName}(index : i32) -> i32 { return index; }`; |
| 461 | } |
| 462 | |
| 463 | const strides = util.computeStrides(shape); |
| 464 | const dtype = getCoordsDataType(rank); |
| 465 | |
| 466 | const coords: string[] = []; |
| 467 | for (let i = 0; i < rank; i++) { |
| 468 | coords.push(`d${i}`); |
| 469 | } |
| 470 | |
| 471 | if (strides.length === 1) { |
| 472 | return ` fn ${funcName}(index : i32) -> vec2<i32> { |
| 473 | let d0 = index / uniforms.${ |
| 474 | stridesName}; let d1 = index - d0 * uniforms.${stridesName}; |
| 475 | return vec2<i32>(d0, d1); |
| 476 | }`; |
| 477 | } |
| 478 | let snippet; |
| 479 | snippet = 'var index2 = index;' + |
| 480 | strides |
| 481 | .map((_, i) => { |
| 482 | const line1 = `let ${coords[i]} = index2 / uniforms.${ |
| 483 | stridesName}.${getCoordsXYZ(i)}`; |
| 484 | const line2 = i === strides.length - 1 ? |
| 485 | `let ${coords[i + 1]} = index2 - ${coords[i]} * uniforms.${ |
| 486 | stridesName}.${getCoordsXYZ(i)}` : |
| 487 | `index2 = index2 - ${coords[i]} * uniforms.${stridesName}.${ |
| 488 | getCoordsXYZ(i)}`; |
| 489 | return `${line1}; ${line2};`; |
| 490 | }) |
| 491 | .join(''); |
| 492 | |
| 493 | return ` |
| 494 | fn ${funcName}(index : i32) -> ${dtype} { |
| 495 | ${snippet} |
| 496 | return ${dtype}(${coords.join(',')}); |
| 497 | } |
| 498 | `; |
| 499 | } |
| 500 | |
| 501 | function getInputAtCoordsSnippet( |
| 502 | inputInfo: InputInfo, component: number): string { |
no test coverage detected
searching dependent graphs…