(no commit message)
[libreriscv.git] / ztrans_proposal.mdwn
1 # Zftrans - transcendental operations
2
3 With thanks to:
4
5 * Jacob Lifshay
6 * Dan Petroski
7 * Mitch Alsup
8 * Allen Baum
9 * Andrew Waterman
10 * Luis Vitorio Cargnini
11
12 [[!toc levels=2]]
13
14 See:
15
16 * <http://bugs.libre-riscv.org/show_bug.cgi?id=127>
17 * <https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html>
18 * Discussion: <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002342.html>
19 * [[rv_major_opcode_1010011]] for opcode listing.
20 * [[zfpacc_proposal]] for accuracy settings proposal
21
22 Extension subsets:
23
24 * **Zftrans**: standard transcendentals (best suited to 3D)
25 * **ZftransExt**: extra functions (useful, not generally needed for 3D,
26 can be synthesised using Ztrans)
27 * **Ztrigpi**: trig. xxx-pi sinpi cospi tanpi
28 * **Ztrignpi**: trig non-xxx-pi sin cos tan
29 * **Zarctrigpi**: arc-trig. a-xxx-pi: atan2pi asinpi acospi
30 * **Zarctrignpi**: arc-trig. non-a-xxx-pi: atan2, asin, acos
31 * **Zfhyp**: hyperbolic/inverse-hyperbolic. sinh, cosh, tanh, asinh,
32 acosh, atanh (can be synthesised - see below)
33 * **ZftransAdv**: much more complex to implement in hardware
34 * **Zfrsqrt**: Reciprocal square-root.
35
36 Minimum recommended requirements for 3D: Zftrans, Ztrigpi, Ztrignpi, Zarctrigpi,
37 Zarctrignpi
38
39 Minimum recommended requirements for Mobile-Embedded 3D: Ztrigpi, Zftrans, Ztrignpi
40
41 # TODO:
42
43 * Decision on accuracy, moved to [[zfpacc_proposal]]
44 <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002355.html>
45 * Errors **MUST** be repeatable.
46 * How about four Platform Specifications? 3DUNIX, UNIX, 3DEmbedded and Embedded?
47 <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002361.html>
48 Accuracy requirements for dual (triple) purpose implementations must
49 meet the higher standard.
50 * Reciprocal Square-root is in its own separate extension (Zfrsqrt) as
51 it is desirable on its own by other implementors. This to be evaluated.
52
53 # Requirements <a name="requirements"></a>
54
55 This proposal is designed to meet a wide range of extremely diverse needs,
56 allowing implementors from all of them to benefit from the tools and hardware
57 cost reductions associated with common standards adoption.
58
59 **There are *four* different, disparate platform's needs (two new)**:
60
61 * 3D Embedded Platform (new)
62 * Embedded Platform
63 * 3D UNIX Platform (new)
64 * UNIX Platform
65
66 **The use-cases are**:
67
68 * 3D GPUs
69 * Numerical Computation
70 * (Potentially) A.I. / Machine-learning (1)
71
72 (1) although approximations suffice in this field, making it more likely
73 to use a custom extension. High-end ML would inherently definitely
74 be excluded.
75
76 **The power and die-area requirements vary from**:
77
78 * Ultra-low-power (smartwatches where GPU power budgets are in milliwatts)
79 * Mobile-Embedded (good performance with high efficiency for battery life)
80 * Desktop Computing
81 * Server / HPC (2)
82
83 (2) Supercomputing is left out of the requirements as it is traditionally
84 covered by Supercomputer Vectorisation Standards (such as RVV).
85
86 **The software requirements are**:
87
88 * Full public integration into GNU math libraries (libm)
89 * Full public integration into well-known Numerical Computation systems (numpy)
90 * Full public integration into upstream GNU and LLVM Compiler toolchains
91 * Full public integration into Khronos OpenCL SPIR-V compatible Compilers
92 seeking public Certification and Endorsement from the Khronos Group
93 under their Trademarked Certification Programme.
94
95 **The "contra"-requirements are**:
96
97 * NOT for use with RVV (RISC-V Vector Extension). These are *scalar* opcodes.
98 Ultra Low Power Embedded platforms (smart watches) are sufficiently
99 resource constrained that Vectorisation (of any kind) is likely to be
100 unnecessary and inappropriate.
101 * The requirements are **not** for the purposes of developing a full custom
102 proprietary GPU with proprietary firmware driven by *hardware* centric
103 optimised design decisions as a priority over collaboration.
104 * A full custom proprietary GPU ASIC Manufacturer *may* benefit from
105 this proposal however the fact that they typically develop proprietary
106 software that is not shared with the rest of the community likely to
107 use this proposal means that they have completely different needs.
108 * This proposal is for *sharing* of effort in reducing development costs
109
110 # Requirements Analysis <a name="requirements_analysis"></a>
111
112 **Platforms**:
113
114 3D Embedded will require significantly less accuracy and will need to make
115 power budget and die area compromises that other platforms (including Embedded)
116 will not need to make.
117
118 3D UNIX Platform has to be performance-price-competitive: subtly-reduced
119 accuracy in FP32 is acceptable where, conversely, in the UNIX Platform,
120 IEEE754 compliance is a hard requirement that would compromise power
121 and efficiency on a 3D UNIX Platform.
122
123 Even in the Embedded platform, IEEE754 interoperability is beneficial,
124 where if it was a hard requirement the 3D Embedded platform would be severely
125 compromised in its ability to meet the demanding power budgets of that market.
126
127 Thus, learning from the lessons of
128 [SIMD considered harmful](https://www.sigarch.org/simd-instructions-considered-harmful/)
129 this proposal works in conjunction with the [[zfpacc_proposal]], so as
130 not to overburden the OP32 ISA space with extra "reduced-accuracy" opcodes.
131
132 **Use-cases**:
133
134 There really is little else in the way of suitable markets. 3D GPUs
135 have extremely competitive power-efficiency and power-budget requirements
136 that are completely at odds with the other market at the other end of
137 the spectrum: Numerical Computation.
138
139 Interoperability in Numerical Computation is absolutely critical: it
140 implies (correlates directly with) IEEE754 compliance. However full
141 IEEE754 compliance automatically and inherently penalises a GPU on
142 performance and die area, where accuracy is simply just not necessary.
143
144 To meet the needs of both markets, the two new platforms have to be created,
145 and [[zfpacc_proposal]] is a critical dependency. Runtime selection of
146 FP accuracy allows an implementation to be "Hybrid" - cover UNIX IEEE754
147 compliance *and* 3D performance in a single ASIC.
148
149 **Power and die-area requirements**:
150
151 This is where the conflicts really start to hit home.
152
153 A "Numerical High performance only" proposal (suitable for Server / HPC
154 only) would customise and target the Extension based on a quantitative
155 analysis of the value of certain opcodes *for HPC only*. It would
156 conclude, reasonably and rationally, that it is worthwhile adding opcodes
157 to RVV as parallel Vector operations, and that further discussion of
158 the matter is pointless.
159
160 A "Proprietary GPU effort" (even one that was intended for publication
161 of its API through, for example, a public libre-licensed Vulkan SPIR-V
162 Compiler) would conclude, reasonably and rationally, that, likewise, the
163 opcodes were best suited to be added to RVV, and, further, that their
164 requirements conflict with the HPC world, due to the reduced accuracy.
165 This on the basis that the silicon die area required for IEEE754 is far
166 greater than that needed for reduced-accuracy, and thus their product
167 would be completely unacceptable in the market if it had to meet IEEE754,
168 unnecessarily.
169
170 An "Embedded 3D" GPU has radically different performance, power
171 and die-area requirements (and may even target SoftCores in FPGA).
172 Sharing of the silicon to cover multi-function uses (CORDIC for example)
173 is absolutely essential in order to keep cost and power down, and high
174 performance simply is not. Multi-cycle FSMs instead of pipelines may
175 be considered acceptable, and so on. Subsets of functionality are
176 also essential.
177
178 An "Embedded Numerical" platform has requirements that are separate and
179 distinct from all of the above!
180
181 Mobile Computing needs (tablets, smartphones) again pull in a different
182 direction: high performance, reasonable accuracy, but efficiency is
183 critical. Screen sizes are not at the 4K range: they are within the
184 800x600 range at the low end (320x240 at the extreme budget end), and
185 only the high-performance smartphones and tablets provide 1080p (1920x1080).
186 With lower resolution, accuracy compromises are possible which the Desktop
187 market (4k and soon to be above) would find unacceptable.
188
189 Meeting these disparate markets may be achieved, again, through
190 [[zfpacc_proposal]], by subdividing into four platforms, yet, in addition
191 to that, subdividing the extension into subsets that best suit the different
192 market areas.
193
194 **Software requirements**:
195
196 A "custom" extension is developed in near-complete isolation from the
197 rest of the RISC-V Community. Cost savings to the Corporation are
198 large, with no direct beneficial feedback to (or impact on) the rest
199 of the RISC-V ecosystem.
200
201 However given that 3D revolves around Standards - DirectX, Vulkan, OpenGL,
202 OpenCL - users have much more influence than first appears. Compliance
203 with these standards is critical as the userbase (Games writers,
204 scientific applications) expects not to have to rewrite extremely large
205 and costly codebases to conform with *non-standards-compliant* hardware.
206
207 Therefore, compliance with public APIs (Vulkan, OpenCL, OpenGL, DirectX)
208 is paramount, and compliance with Trademarked Standards is critical.
209 Any deviation from Trademarked Standards means that an implementation
210 may not be sold and also make a claim of being, for example, "Vulkan
211 compatible".
212
213 This in turn reinforces and makes a hard requirement a need for public
214 compliance with such standards, over-and-above what would otherwise be
215 set by a RISC-V Standards Development Process, including both the
216 software compliance and the knock-on implications that has for hardware.
217
218 **Collaboration**:
219
220 The case for collaboration on any Extension is already well-known.
221 In this particular case, the precedent for inclusion of Transcendentals
222 in other ISAs, both from Graphics and High-performance Computing, has
223 these primitives well-established in high-profile software libraries and
224 compilers in both GPU and HPC Computer Science divisions. Collaboration
225 and shared public compliance with those standards brooks no argument.
226
227 The combined requirements of collaboration and multi accuracy requirements
228 mean that *overall this proposal is categorically and wholly unsuited
229 to relegation of "custom" status*.
230
231 # Quantitative Analysis <a name="analysis"></a>
232
233 This is extremely challenging. Normally, an Extension would require full,
234 comprehensive and detailed analysis of every single instruction, for every
235 single possible use-case, in every single market. The amount of silicon
236 area required would be balanced against the benefits of introducing extra
237 opcodes, as well as a full market analysis performed to see which divisions
238 of Computer Science benefit from the introduction of the instruction,
239 in each and every case.
240
241 With 34 instructions, four possible Platforms, and sub-categories of
242 implementations even within each Platform, over 136 separate and distinct
243 analyses is not a practical proposition.
244
245 A little more intelligence has to be applied to the problem space,
246 to reduce it down to manageable levels.
247
248 Fortunately, the subdivision by Platform, in combination with the
249 identification of only two primary markets (Numerical Computation and
250 3D), means that the logical reasoning applies *uniformly* and broadly
251 across *groups* of instructions rather than individually, making it a primarily
252 hardware-centric and accuracy-centric decision-making process.
253
254 In addition, hardware algorithms such as CORDIC can cover such a wide
255 range of operations (simply by changing the input parameters) that the
256 normal argument of compromising and excluding certain opcodes because they
257 would significantly increase the silicon area is knocked down.
258
259 However, CORDIC, whilst space-efficient, and thus well-suited to
260 Embedded, is an old iterative algorithm not well-suited to High-Performance
261 Computing or Mid to High-end GPUs, where commercially-competitive
262 FP32 pipeline lengths are only around 5 stages.
263
264 Not only that, but some operations such as LOG1P, which would normally
265 be excluded from one market (due to there being an alternative macro-op
266 fused sequence replacing it) are required for other markets due to
267 the higher accuracy obtainable at the lower range of input values when
268 compared to LOG(1+P).
269
270 (Thus we start to see why "proprietary" markets are excluded from this
271 proposal, because "proprietary" markets would make *hardware*-driven
272 optimisation decisions that would be completely inappropriate for a
273 common standard).
274
275 ATAN and ATAN2 is another example area in which one market's needs
276 conflict directly with another: the only viable solution, without compromising
277 one market to the detriment of the other, is to provide both opcodes
278 and let implementors make the call as to which (or both) to optimise,
279 at the *hardware* level.
280
281 Likewise it is well-known that loops involving "0 to 2 times pi", often
282 done in subdivisions of powers of two, are costly to do because they
283 involve floating-point multiplication by PI in each and every loop.
284 3D GPUs solved this by providing SINPI variants which range from 0 to 1
285 and perform the multiply *inside* the hardware itself. In the case of
286 CORDIC, it turns out that the multiply by PI is not even needed (is a
287 loop invariant magic constant).
288
289 However, some markets may not wish to *use* CORDIC, for reasons mentioned
290 above, and, again, one market would be penalised if SINPI was prioritised
291 over SIN, or vice-versa.
292
293 In essence, then, even when only the two primary markets (3D and
294 Numerical Computation) have been identified, this still leaves two
295 (three) diametrically-opposed *accuracy* sub-markets as the prime
296 conflict drivers:
297
298 * Embedded Ultra Low Power
299 * IEEE754 compliance
300 * Khronos Vulkan compliance
301
302 Thus the best that can be done is to use Quantitative Analysis to work
303 out which "subsets" - sub-Extensions - to include, provide an additional
304 "accuracy" extension, be as "inclusive" as possible, and thus allow
305 implementors to decide what to add to their implementation, and how best
306 to optimise them.
307
308 This approach *only* works due to the uniformity of the function space,
309 and is **not** an appropriate methodology for use in other Extensions
310 with huge (non-uniform) market diversity even with similarly large
311 numbers of potential opcodes. BitManip is the perfect counter-example.
312
313 # Proposed Opcodes vs Khronos OpenCL Opcodes <a name="khronos_equiv"></a>
314
315 This list shows the (direct) equivalence between proposed opcodes and
316 their Khronos OpenCL equivalents.
317 For RISCV opcode encodings see
318 [[rv_major_opcode_1010011]]
319
320 See
321 <https://www.khronos.org/registry/spir-v/specs/unified1/OpenCL.ExtendedInstructionSet.100.html>
322
323 * Special FP16 opcodes are *not* being proposed, except by indirect / inherent
324 use of the "fmt" field that is already present in the RISC-V Specification.
325 * "Native" opcodes are *not* being proposed: implementors will be expected
326 to use the (equivalent) proposed opcode covering the same function.
327 * "Fast" opcodes are *not* being proposed, because the Khronos Specification
328 fast\_length, fast\_normalise and fast\_distance OpenCL opcodes require
329 vectors (or can be done as scalar operations using other RISC-V instructions).
330
331 The OpenCL FP32 opcodes are **direct** equivalents to the proposed opcodes.
332 Deviation from conformance with the Khronos Specification - including the
333 Khronos Specification accuracy requirements - is not an option, as it
334 results in non-compliance, and the vendor may not use the Trademarked words
335 "Vulkan" etc. in conjunction with their product.
336
337 [[!table data="""
338 Proposed opcode | OpenCL FP32 | OpenCL FP16 | OpenCL native | OpenCL fast |
339 FSIN | sin | half\_sin | native\_sin | NONE |
340 FCOS | cos | half\_cos | native\_cos | NONE |
341 FTAN | tan | half\_tan | native\_tan | NONE |
342 NONE (1) | sincos | NONE | NONE | NONE |
343 FASIN | asin | NONE | NONE | NONE |
344 FACOS | acos | NONE | NONE | NONE |
345 FATAN | atan | NONE | NONE | NONE |
346 FSINPI | sinpi | NONE | NONE | NONE |
347 FCOSPI | cospi | NONE | NONE | NONE |
348 FTANPI | tanpi | NONE | NONE | NONE |
349 FASINPI | asinpi | NONE | NONE | NONE |
350 FACOSPI | acospi | NONE | NONE | NONE |
351 FATANPI | atanpi | NONE | NONE | NONE |
352 FSINH | sinh | NONE | NONE | NONE |
353 FCOSH | cosh | NONE | NONE | NONE |
354 FTANH | tanh | NONE | NONE | NONE |
355 FASINH | asinh | NONE | NONE | NONE |
356 FACOSH | acosh | NONE | NONE | NONE |
357 FATANH | atanh | NONE | NONE | NONE |
358 FRSQRT | rsqrt | half\_rsqrt | native\_rsqrt | NONE |
359 FCBRT | cbrt | NONE | NONE | NONE |
360 FEXP2 | exp2 | half\_exp2 | native\_exp2 | NONE |
361 FLOG2 | log2 | half\_log2 | native\_log2 | NONE |
362 FEXPM1 | expm1 | NONE | NONE | NONE |
363 FLOG1P | log1p | NONE | NONE | NONE |
364 FEXP | exp | half\_exp | native\_exp | NONE |
365 FLOG | log | half\_log | native\_log | NONE |
366 FEXP10 | exp10 | half\_exp10 | native\_exp10 | NONE |
367 FLOG10 | log10 | half\_log10 | native\_log10 | NONE |
368 FATAN2 | atan2 | NONE | NONE | NONE |
369 FATAN2PI | atan2pi | NONE | NONE | NONE |
370 FPOW | pow | NONE | NONE | NONE |
371 FROOT | rootn | NONE | NONE | NONE |
372 FHYPOT | hypot | NONE | NONE | NONE |
373 FRECIP | NONE | half\_recip | native\_recip | NONE |
374 """]]
375
376 Note (1) FSINCOS is macro-op fused (see below).
377
378 ## List of 2-arg opcodes
379
380 [[!table data="""
381 opcode | Description | pseudocode | Extension |
382 FATAN2 | atan2 arc tangent | rd = atan2(rs2, rs1) | Zarctrignpi |
383 FATAN2PI | atan2 arc tangent / pi | rd = atan2(rs2, rs1) / pi | Zarctrigpi |
384 FPOW | x power of y | rd = pow(rs1, rs2) | ZftransAdv |
385 FROOT | x power 1/y | rd = pow(rs1, 1/rs2) | ZftransAdv |
386 FHYPOT | hypotenuse | rd = sqrt(rs1^2 + rs2^2) | ZftransAdv |
387 """]]
388
389 ## List of 1-arg transcendental opcodes
390
391 [[!table data="""
392 opcode | Description | pseudocode | Extension |
393 FRSQRT | Reciprocal Square-root | rd = sqrt(rs1) | Zfrsqrt |
394 FCBRT | Cube Root | rd = pow(rs1, 1.0 / 3) | ZftransAdv |
395 FRECIP | Reciprocal | rd = 1.0 / rs1 | Zftrans |
396 FEXP2 | power-of-2 | rd = pow(2, rs1) | Zftrans |
397 FLOG2 | log2 | rd = log(2. rs1) | Zftrans |
398 FEXPM1 | exponential minus 1 | rd = pow(e, rs1) - 1.0 | ZftransExt |
399 FLOG1P | log plus 1 | rd = log(e, 1 + rs1) | ZftransExt |
400 FEXP | exponential | rd = pow(e, rs1) | ZftransExt |
401 FLOG | natural log (base e) | rd = log(e, rs1) | ZftransExt |
402 FEXP10 | power-of-10 | rd = pow(10, rs1) | ZftransExt |
403 FLOG10 | log base 10 | rd = log(10, rs1) | ZftransExt |
404 """]]
405
406 ## List of 1-arg trigonometric opcodes
407
408 [[!table data="""
409 opcode | Description | pseudo-code | Extension |
410 FSIN | sin (radians) | rd = sin(rs1) | Ztrignpi |
411 FCOS | cos (radians) | rd = cos(rs1) | Ztrignpi |
412 FTAN | tan (radians) | rd = tan(rs1) | Ztrignpi |
413 FASIN | arcsin (radians) | rd = asin(rs1) | Zarctrignpi |
414 FACOS | arccos (radians) | rd = acos(rs1) | Zarctrignpi |
415 FATAN | arctan (radians) | rd = atan(rs1) | Zarctrignpi |
416 FSINPI | sin times pi | rd = sin(pi * rs1) | Ztrigpi |
417 FCOSPI | cos times pi | rd = cos(pi * rs1) | Ztrigpi |
418
419 FTANPI | tan times pi | rd = tan(pi * rs1) | Ztrigpi |
420 FASINPI | arcsin / pi | rd = asin(rs1) / pi | Zarctrigpi |
421 FACOSPI | arccos / pi | rd = acos(rs1) / pi | Zarctrigpi |
422 FATANPI | arctan / pi | rd = atan(rs1) / pi | Zarctrigpi |
423 FSINH | hyperbolic sin (radians) | rd = sinh(rs1) | Zfhyp |
424 FCOSH | hyperbolic cos (radians) | rd = cosh(rs1) | Zfhyp |
425 FTANH | hyperbolic tan (radians) | rd = tanh(rs1) | Zfhyp |
426 FASINH | inverse hyperbolic sin | rd = asinh(rs1) | Zfhyp |
427 FACOSH | inverse hyperbolic cos | rd = acosh(rs1) | Zfhyp |
428 FATANH | inverse hyperbolic tan | rd = atanh(rs1) | Zfhyp |
429 """]]
430
431 # Subsets
432
433 The full set is based on the Khronos OpenCL opcodes. If implemented
434 entirely it would be too much for both Embedded and also 3D.
435
436 The subsets are organised by hardware complexity, need (3D, HPC), however
437 due to synthesis producing inaccurate results at the range limits,
438 the less common subsets are still required for IEEE754 HPC.
439
440 MALI Midgard, an embedded / mobile 3D GPU, for example only has the
441 following opcodes:
442
443 E8 - fatan_pt2
444 F0 - frcp (reciprocal)
445 F2 - frsqrt (inverse square root, 1/sqrt(x))
446 F3 - fsqrt (square root)
447 F4 - fexp2 (2^x)
448 F5 - flog2
449 F6 - fsin
450 F7 - fcos
451 F9 - fatan_pt1
452
453 These in FP32 and FP16 only: no FP32 hardware, at all.
454
455 Vivante Embedded/Mobile 3D (etnaviv <https://github.com/laanwj/etna_viv/blob/master/rnndb/isa.xml>) only has the following:
456
457 sin, cos2pi
458 cos, sin2pi
459 log2, exp
460 sqrt and rsqrt
461 recip.
462
463 It also has fast variants of some of these, as a CSR Mode.
464
465 AMD's R600 GPU (R600\_Instruction\_Set\_Architecture.pdf) and the
466 RDNA ISA (RDNA\_Shader\_ISA\_5August2019.pdf, Table 22, Section 6.3) have:
467
468 COS (appx)
469 EXP2
470 LOG (IEEE754)
471 RECIP
472 RSQRT
473 SQRT
474 SIN (appx)
475
476 AMD RDNA has F16 and F32 variants of all the above, and also has F64
477 variants of SQRT, RSQRT and RECIP. It is interesting that even the
478 modern high-end AMD GPU does not have TAN or ATAN, where MALI Midgard
479 does.
480
481 Also a general point, that customised optimised hardware targetting
482 FP32 3D with less accuracy simply can neither be used for IEEE754 nor
483 for FP64 (except as a starting point for hardware or software driven
484 Newton Raphson or other iterative method).
485
486 Also in cost/area sensitive applications even the extra ROM lookup tables
487 for certain algorithms may be too costly.
488
489 These wildly differing and incompatible driving factors lead to the
490 subset subdivisions, below.
491
492 ## Transcendental Subsets
493
494 ### Zftrans
495
496 LOG2 EXP2 RECIP RSQRT
497
498 Zftrans contains the minimum standard transcendentals best suited to
499 3D. They are also the minimum subset for synthesising log10, exp10,
500 exp1m, log1p, the hyperbolic trigonometric functions sinh and so on.
501
502 They are therefore considered "base" (essential) transcendentals.
503
504 ### ZftransExt
505
506 LOG, EXP, EXP10, LOG10, LOGP1, EXP1M
507
508 These are extra transcendental functions that are useful, not generally
509 needed for 3D, however for Numerical Computation they may be useful.
510
511 Although they can be synthesised using Ztrans (LOG2 multiplied
512 by a constant), there is both a performance penalty as well as an
513 accuracy penalty towards the limits, which for IEEE754 compliance is
514 unacceptable. In particular, LOG(1+rs1) in hardware may give much better
515 accuracy at the lower end (very small rs1) than LOG(rs1).
516
517 Their forced inclusion would be inappropriate as it would penalise
518 embedded systems with tight power and area budgets. However if they
519 were completely excluded the HPC applications would be penalised on
520 performance and accuracy.
521
522 Therefore they are their own subset extension.
523
524 ### Zfhyp
525
526 SINH, COSH, TANH, ASINH, ACOSH, ATANH
527
528 These are the hyperbolic/inverse-hyperbolic functions. Their use in 3D is limited.
529
530 They can all be synthesised using LOG, SQRT and so on, so depend
531 on Zftrans. However, once again, at the limits of the range, IEEE754
532 compliance becomes impossible, and thus a hardware implementation may
533 be required.
534
535 HPC and high-end GPUs are likely markets for these.
536
537 ### ZftransAdv
538
539 CBRT, POW, ROOT (inverse of POW): these are simply much more complex
540 to implement in hardware, and typically will only be put into HPC
541 applications.
542
543 ROOT is included as well as POW because at the extreme ranges one is
544 more accurate than the other.
545
546 * **Zfrsqrt**: Reciprocal square-root.
547
548 ## Trigonometric subsets
549
550 ### Ztrigpi vs Ztrignpi
551
552 * **Ztrigpi**: SINPI COSPI TANPI
553 * **Ztrignpi**: SIN COS TAN
554
555 Ztrignpi are the basic trigonometric functions through which all others
556 could be synthesised, and they are typically the base trigonometrics
557 provided by GPUs for 3D, warranting their own subset.
558
559 However as can be correspondingly seen from other sections, there is an
560 accuracy penalty for doing so which will not be acceptable for IEEE754
561 compliance.
562
563 In the case of the Ztrigpi subset, these are commonly used in for loops
564 with a power of two number of subdivisions, and the cost of multiplying
565 by PI inside each loop (or cumulative addition, resulting in cumulative
566 errors) is not acceptable.
567
568 In for example CORDIC the multiplication by PI may be moved outside of
569 the hardware algorithm as a loop invariant, with no power or area penalty.
570
571 Thus again, the same general argument applies to give Ztrignpi and
572 Ztrigpi as subsets.
573
574 ### Zarctrigpi and Zarctrignpi
575
576 * **Zarctrigpi**: ATAN2PI ASINPI ACOSPI
577 * **Zarctrignpi**: ATAN2 ACOS ADIN
578
579 These are extra trigonometric functions that are useful in some
580 applications, but even for 3D GPUs, particularly embedded and mobile class
581 GPUs, they are not so common and so are typically synthesised, there.
582
583 Although they can be synthesised using Ztrigpi and Ztrignpi, there is,
584 once again, both a performance penalty as well as an accuracy penalty
585 towards the limits, which for IEEE754 compliance is unacceptable, yet
586 is acceptable for 3D.
587
588 Therefore they are their own subset extensions.
589
590 # Synthesis, Pseudo-code ops and macro-ops
591
592 The pseudo-ops are best left up to the compiler rather than being actual
593 pseudo-ops, by allocating one scalar FP register for use as a constant
594 (loop invariant) set to "1.0" at the beginning of a function or other
595 suitable code block.
596
597 * FSINCOS - fused macro-op between FSIN and FCOS (issued in that order).
598 * FSINCOSPI - fused macro-op between FSINPI and FCOSPI (issued in that order).
599
600 FATANPI example pseudo-code:
601
602 lui t0, 0x3F800 // upper bits of f32 1.0
603 fmv.x.s ft0, t0
604 fatan2pi.s rd, rs1, ft0
605
606 Hyperbolic function example (obviates need for Zfhyp except for
607 high-performance or correctly-rounding):
608
609 ASINH( x ) = ln( x + SQRT(x**2+1))
610
611 # Evaluation and commentary
612
613 This section will move later to discussion.
614
615 ## Reciprocal
616
617 Used to be an alias. Some implementors may wish to implement divide as
618 y times recip(x).
619
620 Others may have shared hardware for recip and divide, others may not.
621
622 To avoid penalising one implementor over another, recip stays.
623
624 ## To evaluate: should LOG be replaced with LOG1P (and EXP with EXPM1)?
625
626 RISC principle says "exclude LOG because it's covered by LOGP1 plus an ADD".
627 Research needed to ensure that implementors are not compromised by such
628 a decision
629 <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002358.html>
630
631 > > correctly-rounded LOG will return different results than LOGP1 and ADD.
632 > > Likewise for EXP and EXPM1
633
634 > ok, they stay in as real opcodes, then.
635
636 ## ATAN / ATAN2 commentary
637
638 Discussion starts here:
639 <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002470.html>
640
641 from Mitch Alsup:
642
643 would like to point out that the general implementations of ATAN2 do a
644 bunch of special case checks and then simply call ATAN.
645
646 double ATAN2( double y, double x )
647 { // IEEE 754-2008 quality ATAN2
648
649 // deal with NANs
650 if( ISNAN( x ) ) return x;
651 if( ISNAN( y ) ) return y;
652
653 // deal with infinities
654 if( x == +∞ && |y|== +∞ ) return copysign( π/4, y );
655 if( x == +∞ ) return copysign( 0.0, y );
656 if( x == -∞ && |y|== +∞ ) return copysign( 3π/4, y );
657 if( x == -∞ ) return copysign( π, y );
658 if( |y|== +∞ ) return copysign( π/2, y );
659
660 // deal with signed zeros
661 if( x == 0.0 && y != 0.0 ) return copysign( π/2, y );
662 if( x >=+0.0 && y == 0.0 ) return copysign( 0.0, y );
663 if( x <=-0.0 && y == 0.0 ) return copysign( π, y );
664
665 // calculate ATAN2 textbook style
666 if( x > 0.0 ) return ATAN( |y / x| );
667 if( x < 0.0 ) return π - ATAN( |y / x| );
668 }
669
670
671 Yet the proposed encoding makes ATAN2 the primitive and has ATAN invent
672 a constant and then call/use ATAN2.
673
674 When one considers an implementation of ATAN, one must consider several
675 ranges of evaluation::
676
677 x [ -∞, -1.0]:: ATAN( x ) = -π/2 + ATAN( 1/x );
678 x (-1.0, +1.0]:: ATAN( x ) = + ATAN( x );
679 x [ 1.0, +∞]:: ATAN( x ) = +π/2 - ATAN( 1/x );
680
681 I should point out that the add/sub of π/2 can not lose significance
682 since the result of ATAN(1/x) is bounded 0..π/2
683
684 The bottom line is that I think you are choosing to make too many of
685 these into OpCodes, making the hardware function/calculation unit (and
686 sequencer) more complicated that necessary.
687
688 --------------------------------------------------------
689
690 We therefore I think have a case for bringing back ATAN and including ATAN2.
691
692 The reason is that whilst a microcode-like GPU-centric platform would do ATAN2 in terms of ATAN, a UNIX-centric platform would do it the other way round.
693
694 (that is the hypothesis, to be evaluated for correctness. feedback requested).
695
696 This because we cannot compromise or prioritise one platfrom's
697 speed/accuracy over another. That is not reasonable or desirable, to
698 penalise one implementor over another.
699
700 Thus, all implementors, to keep interoperability, must both have both
701 opcodes and may choose, at the architectural and routing level, which
702 one to implement in terms of the other.
703
704 Allowing implementors to choose to add either opcode and let traps sort it
705 out leaves an uncertainty in the software developer's mind: they cannot
706 trust the hardware, available from many vendors, to be performant right
707 across the board.
708
709 Standards are a pig.
710
711 ---
712
713 I might suggest that if there were a way for a calculation to be performed
714 and the result of that calculation chained to a subsequent calculation
715 such that the precision of the result-becomes-operand is wider than
716 what will fit in a register, then you can dramatically reduce the count
717 of instructions in this category while retaining
718
719 acceptable accuracy:
720
721 z = x / y
722
723 can be calculated as::
724
725 z = x * (1/y)
726
727 Where 1/y has about 26-to-32 bits of fraction. No, it's not IEEE 754-2008
728 accurate, but GPUs want speed and
729
730 1/y is fully pipelined (F32) while x/y cannot be (at reasonable area). It
731 is also not "that inaccurate" displaying 0.625-to-0.52 ULP.
732
733 Given that one has the ability to carry (and process) more fraction bits,
734 one can then do high precision multiplies of π or other transcendental
735 radixes.
736
737 And GPUs have been doing this almost since the dawn of 3D.
738
739 // calculate ATAN2 high performance style
740 // Note: at this point x != y
741 //
742 if( x > 0.0 )
743 {
744 if( y < 0.0 && |y| < |x| ) return - π/2 - ATAN( x / y );
745 if( y < 0.0 && |y| > |x| ) return + ATAN( y / x );
746 if( y > 0.0 && |y| < |x| ) return + ATAN( y / x );
747 if( y > 0.0 && |y| > |x| ) return + π/2 - ATAN( x / y );
748 }
749 if( x < 0.0 )
750 {
751 if( y < 0.0 && |y| < |x| ) return + π/2 + ATAN( x / y );
752 if( y < 0.0 && |y| > |x| ) return + π - ATAN( y / x );
753 if( y > 0.0 && |y| < |x| ) return + π - ATAN( y / x );
754 if( y > 0.0 && |y| > |x| ) return +3π/2 + ATAN( x / y );
755 }
756
757 This way the adds and subtracts from the constant are not in a precision
758 precarious position.