add more fp -> int bfp* functions
authorJacob Lifshay <programmerjake@gmail.com>
Wed, 17 May 2023 04:50:07 +0000 (21:50 -0700)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 2 Jun 2023 18:51:18 +0000 (19:51 +0100)
openpower/isafunctions/bfp.mdwn

index 9bbef89b418f730e9f9d284cc81b6f12326b2af3..e651de9c70e84816d31e918c364635f38f9bdebb 100644 (file)
@@ -416,6 +416,74 @@ section 7.6.2.2
         result.sign <- 0
         return result
 
+    def si32_CONVERT_FROM_BFP(x):
+        # x is an integer value represented in the
+        # binary floating-point working format.
+
+        if IsNaN(x) then
+            vxcvi_flag <- 1
+            if IsSNaN(x) then vxsnan_flag <- 1
+            return 0x8000_0000
+        else
+            temp_xx <- xx_flag
+            temp_inc <- inc_flag
+            rnd <- bfp_ROUND_TO_INTEGER_TRUNC(x)
+            # TODO: does the spec say these are preserved?
+            xx_flag <- temp_xx
+            inc_flag <- temp_inc
+            exponent <- rnd.exponent
+            significand <- rnd.significand
+            si32max <- bfp_CONVERT_FROM_SI32(0b0 || [1] * 31)
+            si32min <- bfp_CONVERT_FROM_SI32(0b1 || [0] * 31)
+            if bfp_COMPARE_GT(rnd, si32max) then
+                vxcvi_flag <- 1
+                return 0x7FFF_FFFF
+            if bfp_COMPARE_LT(rnd, si32min) then
+                vxcvi_flag <- 1
+                return 0x8000_0000
+            else
+                if ¬bfp_COMPARE_EQ(rnd, x) then xx_flag <- 1
+                else xx_flag <- 0  # TODO: does the spec specify this?
+                inc_flag <- 0
+                # TODO: spec says this is logical shift right:
+                significand <- significand[0:31] / pow(2, 31 - exponent)
+                if IsNeg(rnd) then significand <- -significand
+                return significand[0:31]
+
+    def si64_CONVERT_FROM_BFP(x):
+        # x is an integer value represented in the
+        # binary floating-point working format.
+
+        if IsNaN(x) then
+            vxcvi_flag <- 1
+            if IsSNaN(x) then vxsnan_flag <- 1
+            return 0x8000_0000_0000_0000
+        else
+            temp_xx <- xx_flag
+            temp_inc <- inc_flag
+            rnd <- bfp_ROUND_TO_INTEGER_TRUNC(x)
+            # TODO: does the spec say these are preserved?
+            xx_flag <- temp_xx
+            inc_flag <- temp_inc
+            exponent <- rnd.exponent
+            significand <- rnd.significand
+            si64max <- bfp_CONVERT_FROM_SI64(0b0 || [1] * 63)
+            si64min <- bfp_CONVERT_FROM_SI64(0b1 || [0] * 63)
+            if bfp_COMPARE_GT(rnd, si64max) then
+                vxcvi_flag <- 1
+                return 0x7FFF_FFFF_FFFF_FFFF
+            if bfp_COMPARE_LT(rnd, si64min) then
+                vxcvi_flag <- 1
+                return 0x8000_0000_0000_0000
+            else
+                if ¬bfp_COMPARE_EQ(rnd, x) then xx_flag <- 1
+                else xx_flag <- 0  # TODO: does the spec specify this?
+                inc_flag <- 0
+                # TODO: spec says this is logical shift right:
+                significand <- significand[0:63] / pow(2, 63 - exponent)
+                if IsNeg(rnd) then significand <- -significand
+                return significand[0:63]
+
     def si128_CONVERT_FROM_BFP(x):
         # x is an integer value represented in the
         # binary floating-point working format.
@@ -449,3 +517,99 @@ section 7.6.2.2
                 significand <- significand[0:127] / pow(2, 127 - exponent)
                 if IsNeg(rnd) then significand <- -significand
                 return significand[0:127]
+
+    def ui32_CONVERT_FROM_BFP(x):
+        # x is an integer value represented in the
+        # binary floating-point working format.
+
+        if IsNaN(x) then
+            vxcvi_flag <- 1
+            if IsSNaN(x) then vxsnan_flag <- 1
+            return 0x0000_0000
+        else
+            temp_xx <- xx_flag
+            temp_inc <- inc_flag
+            rnd <- bfp_ROUND_TO_INTEGER_TRUNC(x)
+            # TODO: does the spec say these are preserved?
+            xx_flag <- temp_xx
+            inc_flag <- temp_inc
+            exponent <- rnd.exponent
+            significand <- rnd.significand
+            ui32max <- bfp_CONVERT_FROM_UI32([1] * 32)
+            if bfp_COMPARE_GT(rnd, ui32max) then
+                vxcvi_flag <- 1
+                return 0xFFFF_FFFF
+            if IsNeg(rnd) then
+                vxcvi_flag <- 1
+                return 0x0000_0000
+            else
+                if ¬bfp_COMPARE_EQ(rnd, x) then xx_flag <- 1
+                else xx_flag <- 0  # TODO: does the spec specify this?
+                inc_flag <- 0
+                # TODO: spec says this is logical shift right:
+                significand <- significand[0:31] / pow(2, 31 - exponent)
+                return significand[0:31]
+
+    def ui64_CONVERT_FROM_BFP(x):
+        # x is an integer value represented in the
+        # binary floating-point working format.
+
+        if IsNaN(x) then
+            vxcvi_flag <- 1
+            if IsSNaN(x) then vxsnan_flag <- 1
+            return 0x0000_0000_0000_0000
+        else
+            temp_xx <- xx_flag
+            temp_inc <- inc_flag
+            rnd <- bfp_ROUND_TO_INTEGER_TRUNC(x)
+            # TODO: does the spec say these are preserved?
+            xx_flag <- temp_xx
+            inc_flag <- temp_inc
+            exponent <- rnd.exponent
+            significand <- rnd.significand
+            ui64max <- bfp_CONVERT_FROM_UI64([1] * 64)
+            if bfp_COMPARE_GT(rnd, ui64max) then
+                vxcvi_flag <- 1
+                return 0xFFFF_FFFF_FFFF_FFFF
+            if IsNeg(rnd) then
+                vxcvi_flag <- 1
+                return 0x0000_0000_0000_0000
+            else
+                if ¬bfp_COMPARE_EQ(rnd, x) then xx_flag <- 1
+                else xx_flag <- 0  # TODO: does the spec specify this?
+                inc_flag <- 0
+                # TODO: spec says this is logical shift right:
+                significand <- significand[0:63] / pow(2, 63 - exponent)
+                return significand[0:63]
+
+    def ui128_CONVERT_FROM_BFP(x):
+        # x is an integer value represented in the
+        # binary floating-point working format.
+
+        if IsNaN(x) then
+            vxcvi_flag <- 1
+            if IsSNaN(x) then vxsnan_flag <- 1
+            return 0x0000_0000_0000_0000_0000_0000_0000_0000
+        else
+            temp_xx <- xx_flag
+            temp_inc <- inc_flag
+            rnd <- bfp_ROUND_TO_INTEGER_TRUNC(x)
+            # TODO: does the spec say these are preserved?
+            xx_flag <- temp_xx
+            inc_flag <- temp_inc
+            exponent <- rnd.exponent
+            significand <- rnd.significand
+            ui128max <- bfp_CONVERT_FROM_UI128([1] * 128)
+            if bfp_COMPARE_GT(rnd, ui128max) then
+                vxcvi_flag <- 1
+                return 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
+            if IsNeg(rnd) then
+                vxcvi_flag <- 1
+                return 0x0000_0000_0000_0000_0000_0000_0000_0000
+            else
+                if ¬bfp_COMPARE_EQ(rnd, x) then xx_flag <- 1
+                else xx_flag <- 0  # TODO: does the spec specify this?
+                inc_flag <- 0
+                # TODO: spec says this is logical shift right:
+                significand <- significand[0:127] / pow(2, 127 - exponent)
+                return significand[0:127]