(
component: 'real'|'imag', inputShape: [number, number],
inverse: boolean)
| 23 | userCode: string; |
| 24 | |
| 25 | constructor( |
| 26 | component: 'real'|'imag', inputShape: [number, number], |
| 27 | inverse: boolean) { |
| 28 | const innerDim = inputShape[1]; |
| 29 | this.outputShape = inputShape; |
| 30 | |
| 31 | const exponentMultiplierSnippet = |
| 32 | inverse ? `2.0 * ${Math.PI}` : `-2.0 * ${Math.PI}`; |
| 33 | const resultDenominator = inverse ? `${innerDim}.0` : '1.0'; |
| 34 | |
| 35 | let opString: string; |
| 36 | if (component === 'real') { |
| 37 | opString = 'return real * expR - imag * expI;'; |
| 38 | } else if (component === 'imag') { |
| 39 | opString = 'return real * expI + imag * expR;'; |
| 40 | } else { |
| 41 | throw new Error( |
| 42 | `FFT component must be either "real" or "imag", got ${component}.`); |
| 43 | } |
| 44 | |
| 45 | this.userCode = ` |
| 46 | const float exponentMultiplier = ${exponentMultiplierSnippet}; |
| 47 | |
| 48 | float unaryOpComplex(float real, float expR, float imag, float expI) { |
| 49 | ${opString} |
| 50 | } |
| 51 | |
| 52 | float mulMatDFT(int batch, int index) { |
| 53 | float indexRatio = float(index) / float(${innerDim}); |
| 54 | float exponentMultiplierTimesIndexRatio = |
| 55 | exponentMultiplier * indexRatio; |
| 56 | |
| 57 | float result = 0.0; |
| 58 | |
| 59 | for (int i = 0; i < ${innerDim}; i++) { |
| 60 | // x = (-2|2 * PI / N) * index * i; |
| 61 | float x = exponentMultiplierTimesIndexRatio * float(i); |
| 62 | float expR = cos(x); |
| 63 | float expI = sin(x); |
| 64 | float real = getReal(batch, i); |
| 65 | float imag = getImag(batch, i); |
| 66 | |
| 67 | result += |
| 68 | unaryOpComplex(real, expR, imag, expI) / ${resultDenominator}; |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | void main() { |
| 75 | ivec2 coords = getOutputCoords(); |
| 76 | setOutput(mulMatDFT(coords[0], coords[1])); |
| 77 | } |
| 78 | `; |
| 79 | } |
| 80 | } |
nothing calls this directly
no outgoing calls
no test coverage detected