Source/Packages

Realization.Nvidia.SM86.RotaryTableSM86

packages/realizations/cooperative/nvidia-sm86/src/Realization/Nvidia/SM86/RotaryTableSM86.alpha

119 lines22 declarations7.8 KiBSHA-256 dc57d74ec7e7

Complete file · line 55

RotaryTableSM86.alpha

Definition view
1module Realization.Nvidia.SM86.RotaryTableSM86
2
3import Accelerator.SM86.Control
4import Accelerator.SM86.Immediate
5import Accelerator.SM86.Instruction
6import Accelerator.SM86.InstructionEncoding
7import Accelerator.SM86.NumericSemantics
8import Accelerator.SM86.Program
9import Accelerator.SM86.Types
10import Std.Natural
11
12-- One column of the rotary tables: for the positions p of a block, the
13-- cosine and sine of p theta, stored at `cos + p stride` and `sin + p
14-- stride` -- a launch per frequency theta, a thread per position.
15--
16-- The angle is carried in two words: theta = thetaHigh + thetaLow (the
17-- binary32 nearest theta, and the binary32 nearest what it misses), so
18-- p theta = p thetaHigh -- whose rounding error a fused multiply-add recovers
19-- exactly -- plus p thetaLow.  It is reduced by whole turns, k = the nearest
20-- integer to p thetaHigh / (2 pi) (the magic-number rounding: adding 1.5 x
21-- 2^23 leaves the integer in the low bits), with 2 pi likewise in two words
22-- (Cody and Waite): r = ((a - k twoPiHigh) - k twoPiLow) + p thetaLow, which
23-- lies within a turn of zero.  MUFU.COS and MUFU.SIN take their argument in
24-- turns (r / (2 pi)).
25--
26-- Constant bank: 0x160 the cosine column's address, 0x168 the sine column's;
27-- scalars from 0x190: thetaHigh, thetaLow, 1 / (2 pi), twoPiHigh, twoPiLow,
28-- 1.5 x 2^23, the stride between positions in bytes.
29def rotaryTableSM86CosineArgument : Nat = 0
30def rotaryTableSM86SineArgument : Nat = 1
31def rotaryTableSM86FirstScalarPairArgument : Nat = 6
32def rotaryTableSM86ScalarPairCount : Nat = 4
33def rotaryTableSM86BlockThreads : Nat = 256
34def rotaryTableRegister =
35  (lambda unrestricted index : Byte . (sm86Register index))
36
37def rotaryTableWord =
38  (lambda unrestricted a : Byte .
39    (lambda unrestricted b : Byte .
40      (constructor SM86Unsigned32 SM86Unsigned32Value a b (byte 0) (byte 0))))
41
42def rotaryTableCosine = (rotaryTableWord (byte 96) (byte 1))
43def rotaryTableSine = (rotaryTableWord (byte 104) (byte 1))
44def rotaryTableScalar =
45  (lambda unrestricted k : Nat .
46    (rotaryTableWord (nat-to-byte (naturalAdd 144 (naturalMultiply 4 k))) (byte 1)))
47def rotaryTableStackPointer = (rotaryTableWord (byte 40) (byte 0))
48def rotaryTableZero = (rotaryTableWord (byte 0) (byte 0))
49
50def rotaryTableSet0 = (sm86SetBarrierControl (constructor SM86Barrier SM86Barrier0))
51def rotaryTableWait0 = (sm86WaitBarrierControl (constructor SM86WaitBarrier SM86WaitBarrier0))
52def rotaryTableSet1 = (sm86SetBarrierControl (constructor SM86Barrier SM86Barrier1))
53def rotaryTableWait1 = (sm86WaitBarrierControl (constructor SM86WaitBarrier SM86WaitBarrier1))
54
55def rotaryTableNext =
56  (lambda unrestricted body : (family SM86InstructionBody) .
57    (lambda unrestricted tail : (family SM86Program) .
58      (constructor SM86Program SM86ProgramNext (sm86Instruction body) tail)))
59
60def rotaryTableFloat =
61  (lambda unrestricted d : Byte .
62    (lambda unrestricted k : Nat .
63      (constructor SM86InstructionBody SM86MoveConstant (rotaryTableRegister d) (byte 0) (rotaryTableScalar k) sm86SafeControl)))
64
65def rotaryTableFMA =
66  (lambda unrestricted d : Byte . (lambda unrestricted a : Byte . (lambda unrestricted b : Byte . (lambda unrestricted c : Byte .
67    (constructor SM86InstructionBody SM86FloatFusedMultiplyAdd
68      (rotaryTableRegister d) (rotaryTableRegister a) (rotaryTableRegister b) (rotaryTableRegister c) sm86SafeControl)))))
69
70def rotaryTableNegate =
71  (lambda unrestricted d : Byte . (lambda unrestricted a : Byte .
72    (constructor SM86InstructionBody SM86FloatNegate (rotaryTableRegister d) (rotaryTableRegister a) sm86SafeControl)))
73
74def rotaryTableSM86Program : (family SM86Program) =
75  -- R1 the stack pointer (the ABI's first word); R0 the position
76  (rotaryTableNext (constructor SM86InstructionBody SM86MoveConstant (rotaryTableRegister (byte 1)) (byte 0) rotaryTableStackPointer sm86SafeControl)
77  (rotaryTableNext (constructor SM86InstructionBody SM86SpecialToRegister (rotaryTableRegister (byte 0)) (constructor SM86SpecialRegister SM86CooperativeThreadArrayIdX) rotaryTableSet0)
78  (rotaryTableNext (constructor SM86InstructionBody SM86SpecialToRegister (rotaryTableRegister (byte 2)) (constructor SM86SpecialRegister SM86ThreadIdX) rotaryTableSet0)
79  (rotaryTableNext (constructor SM86InstructionBody SM86IntegerMultiplyAddConstant
80                     (rotaryTableRegister (byte 0)) (rotaryTableRegister (byte 0)) (byte 0) rotaryTableZero (rotaryTableRegister (byte 2)) rotaryTableWait0)
81  -- the scalars
82  (rotaryTableNext (rotaryTableFloat (byte 5) 0)
83  (rotaryTableNext (rotaryTableFloat (byte 6) 1)
84  (rotaryTableNext (rotaryTableFloat (byte 7) 2)
85  (rotaryTableNext (rotaryTableFloat (byte 8) 3)
86  (rotaryTableNext (rotaryTableFloat (byte 9) 4)
87  (rotaryTableNext (rotaryTableFloat (byte 10) 5)
88  (rotaryTableNext (rotaryTableFloat (byte 3) 6)
89  -- a = p thetaHigh, and the rounding error of that product, exactly
90  (rotaryTableNext (constructor SM86InstructionBody SM86IntegerToFloat (rotaryTableRegister (byte 4)) (rotaryTableRegister (byte 0)) sm86SafeControl)
91  (rotaryTableNext (constructor SM86InstructionBody SM86FloatMultiply (rotaryTableRegister (byte 11)) (rotaryTableRegister (byte 4)) (rotaryTableRegister (byte 5)) sm86SafeControl)
92  (rotaryTableNext (rotaryTableNegate (byte 12) (byte 11))
93  (rotaryTableNext (rotaryTableFMA (byte 13) (byte 4) (byte 5) (byte 12))
94  -- the angle's low part: the product's error plus p thetaLow
95  (rotaryTableNext (rotaryTableFMA (byte 14) (byte 4) (byte 6) (byte 13))
96  -- k, the nearest whole number of turns in a
97  (rotaryTableNext (rotaryTableFMA (byte 15) (byte 11) (byte 7) (byte 10))
98  (rotaryTableNext (rotaryTableNegate (byte 17) (byte 10))
99  (rotaryTableNext (constructor SM86InstructionBody SM86FloatAdd (rotaryTableRegister (byte 16)) (rotaryTableRegister (byte 15)) (rotaryTableRegister (byte 17)) sm86SafeControl)
100  (rotaryTableNext (rotaryTableNegate (byte 18) (byte 16))
101  -- r = ((a - k twoPiHigh) - k twoPiLow) + the low part
102  (rotaryTableNext (rotaryTableFMA (byte 19) (byte 18) (byte 8) (byte 11))
103  (rotaryTableNext (rotaryTableFMA (byte 19) (byte 18) (byte 9) (byte 19))
104  (rotaryTableNext (constructor SM86InstructionBody SM86FloatAdd (rotaryTableRegister (byte 19)) (rotaryTableRegister (byte 19)) (rotaryTableRegister (byte 14)) sm86SafeControl)
105  -- in turns, then the cosine and the sine
106  (rotaryTableNext (constructor SM86InstructionBody SM86FloatMultiply (rotaryTableRegister (byte 20)) (rotaryTableRegister (byte 19)) (rotaryTableRegister (byte 7)) sm86SafeControl)
107  (rotaryTableNext (constructor SM86InstructionBody SM86MultiFunctionUnitApproximation (rotaryTableRegister (byte 21)) (rotaryTableRegister (byte 20)) (constructor SM86MultiFunction SM86Cosine) rotaryTableSet1)
108  (rotaryTableNext (constructor SM86InstructionBody SM86MultiFunctionUnitApproximation (rotaryTableRegister (byte 22)) (rotaryTableRegister (byte 20)) (constructor SM86MultiFunction SM86Sine) rotaryTableSet1)
109  -- the column entries: base + p stride
110  (rotaryTableNext (constructor SM86InstructionBody SM86IntegerMultiplyAddWideConstant
111                     (rotaryTableRegister (byte 24)) (rotaryTableRegister (byte 0)) (rotaryTableRegister (byte 3)) (byte 0) rotaryTableCosine sm86SafeControl)
112  (rotaryTableNext (constructor SM86InstructionBody SM86IntegerMultiplyAddWideConstant
113                     (rotaryTableRegister (byte 26)) (rotaryTableRegister (byte 0)) (rotaryTableRegister (byte 3)) (byte 0) rotaryTableSine sm86SafeControl)
114  (rotaryTableNext (constructor SM86InstructionBody SM86StoreGlobal (rotaryTableRegister (byte 24)) (rotaryTableRegister (byte 21)) rotaryTableZero rotaryTableWait1)
115  (rotaryTableNext (constructor SM86InstructionBody SM86StoreGlobal (rotaryTableRegister (byte 26)) (rotaryTableRegister (byte 22)) rotaryTableZero sm86SafeControl)
116  (rotaryTableNext (constructor SM86InstructionBody SM86Exit sm86SafeControl)
117    (constructor SM86Program SM86ProgramEnd))))))))))))))))))))))))))))))))
118
119def rotaryTableSM86RegisterCount : Nat = 32

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.