]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
x86/x64: Fix math.ceil(-0.9) result sign.
authorMike Pall <mike>
Fri, 8 Jul 2022 12:57:01 +0000 (14:57 +0200)
committerMike Pall <mike>
Fri, 8 Jul 2022 12:57:01 +0000 (14:57 +0200)
Reported by minoki.

src/vm_x86.dasc

index 8c2740c379b7badc430adf9b1ed38caf0db69994..7db5e7101d7e2e90865721b4003ebb9ef2f82510 100644 (file)
 |.macro sseconst_1, reg, tmp           // Synthesize 1.0.
 |  sseconst_hi reg, tmp, 3ff00000
 |.endmacro
-|.macro sseconst_m1, reg, tmp          // Synthesize -1.0.
-|  sseconst_hi reg, tmp, bff00000
-|.endmacro
 |.macro sseconst_2p52, reg, tmp                // Synthesize 2^52.
 |  sseconst_hi reg, tmp, 43300000
 |.endmacro
@@ -3114,15 +3111,17 @@ static void build_subroutines(BuildCtx *ctx)
   |  addsd xmm1, xmm3                  // (|x| + 2^52) - 2^52
   |  subsd xmm1, xmm3
   |  orpd xmm1, xmm2                   // Merge sign bit back in.
+  |  sseconst_1 xmm3, RDa
   |  .if mode == 1             // ceil(x)?
-  |    sseconst_m1 xmm2, RDa           // Must subtract -1 to preserve -0.
   |    cmpsd xmm0, xmm1, 6             // x > result?
+  |    andpd xmm0, xmm3
+  |    addsd xmm1, xmm0                        // If yes, add 1.
+  |    orpd xmm1, xmm2                 // Merge sign bit back in (again).
   |  .else                     // floor(x)?
-  |    sseconst_1 xmm2, RDa
   |    cmpsd xmm0, xmm1, 1             // x < result?
+  |    andpd xmm0, xmm3
+  |    subsd xmm1, xmm0                        // If yes, subtract 1.
   |  .endif
-  |  andpd xmm0, xmm2
-  |  subsd xmm1, xmm0                  // If yes, subtract +-1.
   |.endif
   |  movaps xmm0, xmm1
   |1: