1 # RFC ls013 Min/Max GPR/FPR
5 * <https://libre-soc.org/openpower/sv/rfc/ls013/>
6 * <https://git.openpower.foundation/isa/PowerISA/issues/TODO>
7 * <https://bugs.libre-soc.org/show_bug.cgi?id=1057>
19 **Books and Section affected**:
22 Book I Fixed-Point and Floating-Point Instructions
23 Appendix E Power ISA sorted by opcode
24 Appendix F Power ISA sorted by version
25 Appendix G Power ISA sorted by Compliancy Subset
26 Appendix H Power ISA sorted by mnemonic
35 **Submitter**: Luke Leighton (Libre-SOC)
37 **Requester**: Libre-SOC
39 **Impact on processor**:
42 Addition of new GPR-based and FPR-based instructions
45 **Impact on software**:
48 Requires support for new instructions in assembler, debuggers,
55 GPR, FPR, min, max, fmin, fmax
60 Minimum/Maximum are common operations that can take an astounding number of
61 operations to implement in software. Additionally, Vector Reduce-Min/Max are
62 common vector operations, and SVP64 Parallel Reduction needs a single Scalar
63 instruction in order to effectively implement Reduce-Min/Max.
65 **Notes and Observations**:
67 1. SVP64 REMAP Parallel Reduction needs a single Scalar instruction to
68 work with, for best effectiveness. With no SFFS minimum/maximum
69 instructions Simple-V min/max Parallel Reduction is severely compromised.
70 2. Once one FP min/max mode is implemented the rest are not much more hardware.
71 3. There exists similar instructions in VSX (not IEEE754-2019 though).
72 This is frequently used to justify not adding them. However SVP64/VSX may
73 have different meaning from SVP64/SFFS, so it is *really* crucial to have
74 SFFS ops even if "equivalent" to VSX in order for SVP64 to not be
75 compromised (non-orthogonal).
76 4. FP min/max are rather complex to implement in software, the most commonly
77 used FP max function `fmax` from glibc compiled for SFFS is an astounding
82 Add the following entries to:
84 * the Appendices of Book I
85 * Book I 3.3.9 Fixed-Point Arithmetic Instructions
86 * Book I 4.6.6.1 Floating-Point Elementary Arithmetic Instructions
87 * Book I 1.6.1 and 1.6.2
93 # Floating-Point Instructions
95 This group is to provide Floating-Point min/max however with IEEE754 having advanced
96 to 2019 there are now subtle differences. These are selectable with a Mode Field, `FMM`.
98 ## `FMM` -- Floating Min/Max Mode
100 <a id="fmm-floating-min-max-mode"></a>
102 <!-- hyphens in table determine width of columns for pandoc --
103 please don't change just to make markdown source look better -->
104 | `FMM` | Extended Mnemonic | Origin | Semantics |
105 | --- |---------------------------------| ------------------- |---------------------------------------------------|
106 | 0000 | fminnum08[s] FRT, FRA, FRB | IEEE 754-2008 | FRT = minNum(FRA, FRB) (1) |
107 | 0001 | fmin19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minimum(FRA, FRB) |
108 | 0010 | fminnum19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minimumNumber(FRA, FRB) |
109 | 0011 | fminc[s] FRT, FRA, FRB | x86 minss or<br>Win32's min macro | FRT = FRA \< FRB ? FRA : FRB |
110 | 0100 | fminmagnum08[s] FRT, FRA, FRB | IEEE 754-2008<br>(TODO: (3)) | FRT = minmaxmag(FRA, FRB, False, fminnum08) (2) |
111 | 0101 | fminmag19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minmaxmag(FRA, FRB, False, fmin19) (2) |
112 | 0110 | fminmagnum19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minmaxmag(FRA, FRB, False, fminnum19) (2) |
113 | 0111 | fminmagc[s] FRT, FRA, FRB | - | FRT = minmaxmag(FRA, FRB, False, fminc) (2) |
114 | 1000 | fmaxnum08[s] FRT, FRA, FRB | IEEE 754-2008 | FRT = maxNum(FRA, FRB) (1) |
115 | 1001 | fmax19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = maximum(FRA, FRB) |
116 | 1010 | fmaxnum19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = maximumNumber(FRA, FRB) |
117 | 1011 | fmaxc[s] FRT, FRA, FRB | x86 maxss or<br>Win32's max macro | FRT = FRA > FRB ? FRA : FRB |
118 | 1100 | fmaxmagnum08[s] FRT, FRA, FRB | IEEE 754-2008<br>(TODO: (3)) | FRT = minmaxmag(FRA, FRB, True, fmaxnum08) (2) |
119 | 1101 | fmaxmag19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minmaxmag(FRA, FRB, True, fmax19) (2) |
120 | 1110 | fmaxmagnum19[s] FRT, FRA, FRB | IEEE 754-2019 | FRT = minmaxmag(FRA, FRB, True, fmaxnum19) (2) |
121 | 1111 | fmaxmagc[s] FRT, FRA, FRB | - | FRT = minmaxmag(FRA, FRB, True, fmaxc) (2) |
123 Note (1): for the purposes of minNum/maxNum, -0.0 is defined to be less than
124 +0.0. This is left unspecified in IEEE 754-2008.
126 Note (2): minmaxmag(x, y, cmp, fallback) is defined as:
129 def minmaxmag(x, y, is_max, fallback):
138 # equal magnitudes, or NaN input(s)
139 return fallback(x, y)
142 Note (3): TODO: icr if IEEE 754-2008 has min/maxMagNum like IEEE 754-2019's
143 minimum/maximumMagnitudeNumber
149 ## Floating Minimum/Maximum MM-form
151 * fminmax FRT, FRA, FRB, FMM
152 * fminmax. FRT, FRA, FRB, FMM
155 |0 |6 |11 |16 |21 |25 |31 |
156 | PO | FRT | FRA | FRB | FMM | XO | Rc |
159 Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
162 Special Registers altered:
171 see [`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
175 ## Floating Minimum/Maximum Single MM-form
177 * fminmaxs FRT, FRA, FRB, FMM
178 * fminmaxs. FRT, FRA, FRB, FMM
181 |0 |6 |11 |16 |21 |25 |31 |
182 | PO | FRT | FRA | FRB | FMM | XO | Rc |
185 Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
188 Special Registers altered:
197 see [`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
203 # Fixed-Point Instructions
205 These are signed and unsigned, min or max. SVP64 Prefixing defines Saturation
206 semantics therefore Saturated variants of these instructions need not be proposed.
208 ## `MMM` -- Integer Min/Max Mode
210 <a id="mmm-integer-min-max-mode"></a>
212 * bit 0: set if word variant else dword
213 * bit 1: set if signed else unsigned
214 * bit 2: set if max else min
216 | `MMM` | Extended Mnemonic | Semantics |
217 |-------|-------------------|----------------------------------------------|
218 | 000 | `minu RT,RA,RB` | `RT = (uint64_t)RA < (uint64_t)RB ? RA : RB` |
219 | 001 | `maxu RT,RA,RB` | `RT = (uint64_t)RA > (uint64_t)RB ? RA : RB` |
220 | 010 | `mins RT,RA,RB` | `RT = (int64_t)RA < (int64_t)RB ? RA : RB` |
221 | 011 | `maxs RT,RA,RB` | `RT = (int64_t)RA > (int64_t)RB ? RA : RB` |
222 | 100 | `minuw RT,RA,RB` | `RT = (uint32_t)RA < (uint32_t)RB ? RA : RB` |
223 | 101 | `maxuw RT,RA,RB` | `RT = (uint32_t)RA > (uint32_t)RB ? RA : RB` |
224 | 110 | `minsw RT,RA,RB` | `RT = (int32_t)RA < (int32_t)RB ? RA : RB` |
225 | 111 | `maxsw RT,RA,RB` | `RT = (int32_t)RA > (int32_t)RB ? RA : RB` |
227 ## Minimum/Maximum MM-Form
229 * minmax RT, RA, RB, MMM
230 * minmax. RT, RA, RB, MMM
233 |0 |6 |11 |16 |21 |24 |25 |31 |
234 | PO | RT | RA | RB | MMM | / | XO | Rc |
240 if MMM[0] then # word mode
241 # shift left by XLEN/2 to make the dword comparison
242 # do word comparison of the original inputs
243 a <- a[XLEN/2:XLEN-1] || [0] * XLEN/2
244 b <- b[XLEN/2:XLEN-1] || [0] * XLEN/2
245 if MMM[1] then # signed mode
246 # invert sign bits to make the unsigned comparison
247 # do signed comparison of the original inputs
250 # if Rc = 1 then store the result of comparing a and b to CR0
253 CR0 <- 0b100 || XER.SO
255 CR0 <- 0b001 || XER.SO
257 CR0 <- 0b010 || XER.SO
258 if MMM[2] then # max mode
259 # swap a and b to make the less than comparison do
260 # greater than comparison of the original inputs
264 # store the entire selected source (even in word mode)
265 # if Rc = 1 then store the result of comparing a and b to CR0
266 if a <u b then RT <- (RA|0)
270 Compute the integer minimum/maximum according to `MMM` of `(RA|0)` and `(RB)`
271 and store the result in `RT`.
273 Special Registers altered:
281 see [`MMM` -- Integer Min/Max Mode](#mmm-integer-min-max-mode)
287 # Instruction Formats
289 Add the following entries to Book I 1.6.1 Word Instruction Formats:
294 |0 |6 |11 |16 |21 |24 |25 |31 |
295 | PO | FRT | FRA | FRB | FMM | XO | Rc |
296 | PO | RT | RA | RB | MMM | / | XO | Rc |
299 Add the following new fields to Book I 1.6.2 Word Instruction Fields:
303 Field used to specify minimum/maximum mode for fminmax[s].
308 Field used to specify minimum/maximum mode for integer minmax.
313 Add `MM` to the `Formats:` list for all of `FRT`, `FRA`, `FRB`, `XO (25:30)`,
314 `Rc`, `RT`, `RA` and `RB`.
322 Appendix E Power ISA sorted by opcode
323 Appendix F Power ISA sorted by version
324 Appendix G Power ISA sorted by Compliancy Subset
325 Appendix H Power ISA sorted by mnemonic
327 | Form | Book | Page | Version | Mnemonic | Description |
328 |------|------|------|---------|----------|-------------|
329 | MM | I | # | 3.2B | fminmax | Floating Minimum/Maximum |
330 | MM | I | # | 3.2B | fminmaxs | Floating Minimum/Maximum Single |
331 | MM | I | # | 3.2B | minmax | Minimum/Maximum |
333 ## fmax instruction count
335 32 instructions are required in SFFS to emulate fmax.
341 inline uint64_t asuint64(double f) {
349 inline int issignaling(double v) {
350 // copied from glibc:
351 // https://github.com/bminor/glibc/blob/e2756903329365134089d23548e9083d23bc3dd9/sysdeps/ieee754/dbl-64/math_config.h#L101
352 uint64_t ix = asuint64(v);
353 return 2 * (ix ^ 0x0008000000000000) > 2 * 0x7ff8000000000000ULL;
356 double fmax(double x, double y) {
357 // copied from glibc:
358 // https://github.com/bminor/glibc/blob/e2756903329365134089d23548e9083d23bc3dd9/math/s_fmax_template.c
359 if(__builtin_isgreaterequal(x, y))
361 else if(__builtin_isless(x, y))
363 else if(issignaling(x) || issignaling(y))
366 return __builtin_isnan(y) ? x : y;
373 fmax(double, double):
411 .byte 0,9,0,0,0,0,0,0