reformat ls013 FMM table for pandoc
[libreriscv.git] / openpower / sv / rfc / ls013.mdwn
index 4682b34340cc496cfaa2b88862a4d944b3689575..08e5817ebfb89aa031422ea877614da1524f5795 100644 (file)
 
 **Motivation**
 
-TODO
+Minimum/Maximum are common operations that can take an astounding number of
+operations to implement in software. Additionally, Vector Reduce-Min/Max are
+common vector operations, and SVP64 Parallel Reduction needs a single Scalar
+instruction in order to effectively implement Reduce-Min/Max.
 
 **Notes and Observations**:
 
 1. SVP64 REMAP Parallel Reduction needs a single Scalar instruction to
-   work with, for best effectiveness.  With no SFFS minimum/maximum instructions
-   Simple-V min/max Parallel Reduction is severely compromised.
-2. Once one FP min/max mode is implemented the rest are not much more
-   hardware.
-3.  There exists similar instructions in VSX (not IEEE754-2019 though).
-   This is frequently used to justify not
-   adding them. However SVP64/VSX may have different meaning from SVP64/SFFS,
-    so it is *really* crucial to have SFFS ops even if "equivalent" to VSX
-   in order for SVP64 to not be compromised (non-orthogonal).
+    work with, for best effectiveness.  With no SFFS minimum/maximum
+    instructions Simple-V min/max Parallel Reduction is severely compromised.
+2. Once one FP min/max mode is implemented the rest are not much more hardware.
+3. There exists similar instructions in VSX (not IEEE754-2019 though).
+    This is frequently used to justify not adding them. However SVP64/VSX may
+    have different meaning from SVP64/SFFS, so it is *really* crucial to have
+    SFFS ops even if "equivalent" to VSX in order for SVP64 to not be
+    compromised (non-orthogonal).
 4. FP min/max are rather complex to implement in software, the most commonly
-    used FP max function `fmax` from glibc compiled for SFFS is an
-    astounding 32 instructions.
+    used FP max function `fmax` from glibc compiled for SFFS is an astounding
+    32 instructions.
 
 **Changes**
 
@@ -97,24 +99,26 @@ to 2019 there are now subtle differences. These are selectable with a Mode Field
 
 <a id="fmm-floating-min-max-mode"></a>
 
-| `FMM` | Assembly Alias                | Origin                         | Semantics                                       |
-|-------|-------------------------------|--------------------------------|-------------------------------------------------|
-| 0000  | fminnum08[s] FRT, FRA, FRB    | IEEE 754-2008                  | FRT = minNum(FRA, FRB)  (1)                     |
-| 0001  | fmin19[s] FRT, FRA, FRB       | IEEE 754-2019                  | FRT = minimum(FRA, FRB)                         |
-| 0010  | fminnum19[s] FRT, FRA, FRB    | IEEE 754-2019                  | FRT = minimumNumber(FRA, FRB)                   |
-| 0011  | fminc[s] FRT, FRA, FRB        | x86 minss or Win32's min macro | FRT = FRA \< FRB ? FRA : FRB                    |
-| 0100  | fminmagnum08[s] FRT, FRA, FRB | IEEE 754-2008 (TODO: (3))      | FRT = minmaxmag(FRA, FRB, False, fminnum08) (2) |
-| 0101  | fminmag19[s] FRT, FRA, FRB    | IEEE 754-2019                  | FRT = minmaxmag(FRA, FRB, False, fmin19) (2)    |
-| 0110  | fminmagnum19[s] FRT, FRA, FRB | IEEE 754-2019                  | FRT = minmaxmag(FRA, FRB, False, fminnum19) (2) |
-| 0111  | fminmagc[s] FRT, FRA, FRB     | -                              | FRT = minmaxmag(FRA, FRB, False, fminc) (2)     |
-| 1000  | fmaxnum08[s] FRT, FRA, FRB    | IEEE 754-2008                  | FRT = maxNum(FRA, FRB)  (1)                     |
-| 1001  | fmax19[s] FRT, FRA, FRB       | IEEE 754-2019                  | FRT = maximum(FRA, FRB)                         |
-| 1010  | fmaxnum19[s] FRT, FRA, FRB    | IEEE 754-2019                  | FRT = maximumNumber(FRA, FRB)                   |
-| 1011  | fmaxc[s] FRT, FRA, FRB        | x86 maxss or Win32's max macro | FRT = FRA > FRB ? FRA : FRB                     |
-| 1100  | fmaxmagnum08[s] FRT, FRA, FRB | IEEE 754-2008 (TODO: (3))      | FRT = minmaxmag(FRA, FRB, True, fmaxnum08) (2)  |
-| 1101  | fmaxmag19[s] FRT, FRA, FRB    | IEEE 754-2019                  | FRT = minmaxmag(FRA, FRB, True, fmax19) (2)     |
-| 1110  | fmaxmagnum19[s] FRT, FRA, FRB | IEEE 754-2019                  | FRT = minmaxmag(FRA, FRB, True, fmaxnum19) (2)  |
-| 1111  | fmaxmagc[s] FRT, FRA, FRB     | -                              | FRT = minmaxmag(FRA, FRB, True, fmaxc) (2)      |
+<!-- hyphens in table determine width of columns for pandoc --
+please don't change just to make markdown source look better -->
+| `FMM` | Extended Mnemonic               | Origin                            | Semantics                                         |
+| ---   |---------------------------------|        -------------------        |---------------------------------------------------|
+| 0000  | fminnum08[s] FRT, FRA, FRB      | IEEE 754-2008                     | FRT = minNum(FRA, FRB)  (1)                       |
+| 0001  | fmin19[s] FRT, FRA, FRB         | IEEE 754-2019                     | FRT = minimum(FRA, FRB)                           |
+| 0010  | fminnum19[s] FRT, FRA, FRB      | IEEE 754-2019                     | FRT = minimumNumber(FRA, FRB)                     |
+| 0011  | fminc[s] FRT, FRA, FRB          | x86 minss or<br>Win32's min macro | FRT = FRA \< FRB ? FRA : FRB                      |
+| 0100  | fminmagnum08[s] FRT, FRA, FRB   | IEEE 754-2008<br>(TODO: (3))      | FRT = minmaxmag(FRA, FRB, False, fminnum08) (2)   |
+| 0101  | fminmag19[s] FRT, FRA, FRB      | IEEE 754-2019                     | FRT = minmaxmag(FRA, FRB, False, fmin19) (2)      |
+| 0110  | fminmagnum19[s] FRT, FRA, FRB   | IEEE 754-2019                     | FRT = minmaxmag(FRA, FRB, False, fminnum19) (2)   |
+| 0111  | fminmagc[s] FRT, FRA, FRB       | -                                 | FRT = minmaxmag(FRA, FRB, False, fminc) (2)       |
+| 1000  | fmaxnum08[s] FRT, FRA, FRB      | IEEE 754-2008                     | FRT = maxNum(FRA, FRB)  (1)                       |
+| 1001  | fmax19[s] FRT, FRA, FRB         | IEEE 754-2019                     | FRT = maximum(FRA, FRB)                           |
+| 1010  | fmaxnum19[s] FRT, FRA, FRB      | IEEE 754-2019                     | FRT = maximumNumber(FRA, FRB)                     |
+| 1011  | fmaxc[s] FRT, FRA, FRB          | x86 maxss or<br>Win32's max macro | FRT = FRA > FRB ? FRA : FRB                       |
+| 1100  | fmaxmagnum08[s] FRT, FRA, FRB   | IEEE 754-2008<br>(TODO: (3))      | FRT = minmaxmag(FRA, FRB, True, fmaxnum08) (2)    |
+| 1101  | fmaxmag19[s] FRT, FRA, FRB      | IEEE 754-2019                     | FRT = minmaxmag(FRA, FRB, True, fmax19) (2)       |
+| 1110  | fmaxmagnum19[s] FRT, FRA, FRB   | IEEE 754-2019                     | FRT = minmaxmag(FRA, FRB, True, fmaxnum19) (2)    |
+| 1111  | fmaxmagc[s] FRT, FRA, FRB       | -                                 | FRT = minmaxmag(FRA, FRB, True, fmaxc) (2)        |
 
 Note (1): for the purposes of minNum/maxNum, -0.0 is defined to be less than
     +0.0. This is left unspecified in IEEE 754-2008.
@@ -142,45 +146,45 @@ Note (3): TODO: icr if IEEE 754-2008 has min/maxMagNum like IEEE 754-2019's
 
 \newpage{}
 
-## Floating Minimum/Maximum
-
-A-Form
-
+## Floating Minimum/Maximum MM-form
 
 * fminmax FRT, FRA, FRB, FMM
 * fminmax. FRT, FRA, FRB, FMM
 
 ```
-    |0    |6    |11   |16   |21          |26  |31  |
-    | PO  | FRT | FRA | FRB | FMM[0:3] / | XO | Rc |
+    |0    |6    |11   |16   |21   |25  |31  |
+    | PO  | FRT | FRA | FRB | FMM | XO | Rc |
 ```
 
+Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
+result in FRT.
+
 Special Registers altered:
 
 ```
     FX VXSNAN
     CR1     (if Rc=1)
 ```
-Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
-result in FRT.
 
-Assembly Aliases: see
-[`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
+Extended Mnemonics:
 
-----------
+see [`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
 
-## Floating Minimum/Maximum Single
+----------
 
-A-Form
+## Floating Minimum/Maximum Single MM-form
 
 * fminmaxs FRT, FRA, FRB, FMM
 * fminmaxs. FRT, FRA, FRB, FMM
 
 ```
-    |0    |6    |11   |16   |21          |26  |31  |
-    | PO  | FRT | FRA | FRB | FMM[0:3] / | XO | Rc |
+    |0    |6    |11   |16   |21   |25  |31  |
+    | PO  | FRT | FRA | FRB | FMM | XO | Rc |
 ```
 
+Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
+result in FRT.
+
 Special Registers altered:
 
 ```
@@ -188,12 +192,9 @@ Special Registers altered:
     CR1     (if Rc=1)
 ```
 
+Extended Mnemonics:
 
-Compute the minimum/maximum of FRA and FRB, according to FMM, and store the
-result in FRT.
-
-Assembly Aliases: see
-[`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
+see [`FMM` -- Floating Min/Max Mode](#fmm-floating-min-max-mode)
 
 ----------
 
@@ -204,24 +205,26 @@ Assembly Aliases: see
 These are signed and unsigned, min or max.  SVP64 Prefixing defines Saturation
 semantics therefore Saturated variants of these instructions need not be proposed.
 
-## Integer Min/Max Mode
+## `MMM` -- Integer Min/Max Mode
+
+<a id="mmm-integer-min-max-mode"></a>
 
 * bit 0: set if word variant else dword
 * bit 1: set if signed else unsigned
 * bit 2: set if max else min
 
-| `IMM` | Assembly Alias   | Semantics                                    |
-|-------|------------------|----------------------------------------------|
-| 000   | `minu RT,RA,RB`  | `RT = (uint64_t)RA < (uint64_t)RB ? RA : RB` |
-| 001   | `maxu RT,RA,RB`  | `RT = (uint64_t)RA > (uint64_t)RB ? RA : RB` |
-| 010   | `mins RT,RA,RB`  | `RT =  (int64_t)RA < (int64_t)RB  ? RA : RB` |
-| 011   | `maxs RT,RA,RB`  | `RT =  (int64_t)RA > (int64_t)RB  ? RA : RB` |
-| 100   | `minuw RT,RA,RB` | `RT = (uint32_t)RA < (uint32_t)RB ? RA : RB` |
-| 101   | `maxuw RT,RA,RB` | `RT = (uint32_t)RA > (uint32_t)RB ? RA : RB` |
-| 110   | `minsw RT,RA,RB` | `RT =  (int32_t)RA < (int32_t)RB  ? RA : RB` |
-| 111   | `maxsw RT,RA,RB` | `RT =  (int32_t)RA > (int32_t)RB  ? RA : RB` |
+| `MMM` | Extended Mnemonic | Semantics                                    |
+|-------|-------------------|----------------------------------------------|
+| 000   | `minu RT,RA,RB`   | `RT = (uint64_t)RA < (uint64_t)RB ? RA : RB` |
+| 001   | `maxu RT,RA,RB`   | `RT = (uint64_t)RA > (uint64_t)RB ? RA : RB` |
+| 010   | `mins RT,RA,RB`   | `RT =  (int64_t)RA < (int64_t)RB  ? RA : RB` |
+| 011   | `maxs RT,RA,RB`   | `RT =  (int64_t)RA > (int64_t)RB  ? RA : RB` |
+| 100   | `minuw RT,RA,RB`  | `RT = (uint32_t)RA < (uint32_t)RB ? RA : RB` |
+| 101   | `maxuw RT,RA,RB`  | `RT = (uint32_t)RA > (uint32_t)RB ? RA : RB` |
+| 110   | `minsw RT,RA,RB`  | `RT =  (int32_t)RA < (int32_t)RB  ? RA : RB` |
+| 111   | `maxsw RT,RA,RB`  | `RT =  (int32_t)RA > (int32_t)RB  ? RA : RB` |
 
-## Integer Min/Max MM-Form
+## Minimum/Maximum MM-Form
 
 * minmax RT, RA, RB, MMM
 * minmax. RT, RA, RB, MMM
@@ -232,7 +235,7 @@ semantics therefore Saturated variants of these instructions need not be propose
 ```
 
 ```
-    a <- (RA)
+    a <- (RA|0)
     b <- (RB)
     if MMM[0] then  # word mode
         # shift left by XLEN/2 to make the dword comparison
@@ -242,8 +245,16 @@ semantics therefore Saturated variants of these instructions need not be propose
     if MMM[1] then  # signed mode
         # invert sign bits to make the unsigned comparison
         # do signed comparison of the original inputs
-        a[0] <- !a[0]  # convert
-        b[0] <- !b[0]
+        a[0] <- ¬a[0]
+        b[0] <- ¬b[0]
+    # if Rc = 1 then store the result of comparing a and b to CR0
+    if Rc = 1 then
+        if a <u b then
+            CR0 <- 0b100 || XER.SO
+        if a = b then
+            CR0 <- 0b001 || XER.SO
+        if a >u b then
+            CR0 <- 0b010 || XER.SO
     if MMM[2] then  # max mode
         # swap a and b to make the less than comparison do
         # greater than comparison of the original inputs
@@ -251,12 +262,13 @@ semantics therefore Saturated variants of these instructions need not be propose
         a <- b
         b <- t
     # store the entire selected source (even in word mode)
-    if a <u b then RT <- (RA)
-    else           RT <- (RB)
+    # if Rc = 1 then store the result of comparing a and b to CR0
+    if a <u b then RT <- (RA|0)
+    else RT <- (RB)
 ```
 
-Compute the integer minimum/maximum according to `MMM` of `RA` and `RB` and
-store the result in `RT`.
+Compute the integer minimum/maximum according to `MMM` of `(RA|0)` and `(RB)`
+and store the result in `RT`.
 
 Special Registers altered:
 
@@ -264,28 +276,43 @@ Special Registers altered:
     CR0     (if Rc=1)
 ```
 
+Extended Mnemonics:
+
+see [`MMM` -- Integer Min/Max Mode](#mmm-integer-min-max-mode)
+
 ----------
 
 \newpage{}
 
 # Instruction Formats
 
-Add the following entries to Book I 1.6.1.15 X-FORM:
+Add the following entries to Book I 1.6.1 Word Instruction Formats:
+
+## MM-FORM
 
 ```
-    |0    |6    |11   |16   |21          |26  |31  |
-    | PO  | FRT | FRA | FRB | FMM[0:3] / | XO | Rc |
+    |0    |6    |11   |16   |21   |24 |25  |31  |
+    | PO  | FRT | FRA | FRB | FMM     | XO | Rc |
+    | PO  | RT  | RA  | RB  | MMM | / | XO | Rc |
 ```
 
-Add a new field to Book I 1.6.2 Word Instruction Fields:
+Add the following new fields to Book I 1.6.2 Word Instruction Fields:
 
 ```
     FMM (21:24)
         Field used to specify minimum/maximum mode for fminmax[s].
 
-        Formats: A
+        Formats: MM
+
+    MMM (21:23)
+        Field used to specify minimum/maximum mode for integer minmax.
+
+        Formats: MM
 ```
 
+Add `MM` to the `Formats:` list for all of `FRT`, `FRA`, `FRB`, `XO (25:30)`,
+`Rc`, `RT`, `RA` and `RB`.
+
 ----------
 
 \newpage{}
@@ -297,17 +324,50 @@ Add a new field to Book I 1.6.2 Word Instruction Fields:
     Appendix G Power ISA sorted by Compliancy Subset
     Appendix H Power ISA sorted by mnemonic
 
-| Form | Book | Page | Version | mnemonic | Description |
+| Form | Book | Page | Version | Mnemonic | Description |
 |------|------|------|---------|----------|-------------|
-|    | I    | #    | 3.2B    | fminmax  | Floating Minimum/Maximum |
-|    | I    | #    | 3.2B    | fminmaxs | Floating Minimum/Maximum Single |
-| ???  | I    | #    | 3.2B    | minmax | Minimum/max Signed/Unsigned |
+| MM   | I    | #    | 3.2B    | fminmax  | Floating Minimum/Maximum |
+| MM   | I    | #    | 3.2B    | fminmaxs | Floating Minimum/Maximum Single |
+| MM   | I    | #    | 3.2B    | minmax   | Minimum/Maximum |
 
 ## fmax instruction count
 
 32 instructions are required in SFFS to emulate fmax.
-<https://gcc.godbolt.org/z/6xba61To6>
 
+```
+#include <stdint.h>
+#include <string.h>
+
+inline uint64_t asuint64(double f) {
+    union {
+        double f;
+        uint64_t i;
+    } u = {f};
+    return u.i;
+}
+
+inline int issignaling(double v) {
+    // copied from glibc:
+    // https://github.com/bminor/glibc/blob/e2756903329365134089d23548e9083d23bc3dd9/sysdeps/ieee754/dbl-64/math_config.h#L101
+    uint64_t ix = asuint64(v);
+    return 2 * (ix ^ 0x0008000000000000) > 2 * 0x7ff8000000000000ULL;
+}
+
+double fmax(double x, double y) {
+    // copied from glibc:
+    // https://github.com/bminor/glibc/blob/e2756903329365134089d23548e9083d23bc3dd9/math/s_fmax_template.c
+    if(__builtin_isgreaterequal(x, y))
+        return x;
+    else if(__builtin_isless(x, y))
+        return y;
+    else if(issignaling(x) || issignaling(y))
+        return x + y;
+    else
+        return __builtin_isnan(y) ? x : y;
+}
+```
+
+Assembly listing:
 
 ```
     fmax(double, double):