https://git.reactos.org/?p=reactos.git;a=commitdiff;h=be3c532bf41e29910d16e56b57c477095d91ec0b

commit be3c532bf41e29910d16e56b57c477095d91ec0b
Author:     Roman Masanin <[email protected]>
AuthorDate: Sat Jul 31 04:04:25 2021 +0300
Commit:     Stanislav Motylkov <[email protected]>
CommitDate: Sat Jul 31 11:24:03 2021 +0300

    [ARM/CRT] Refactor several functions (#3865)
    
    - Make __fto64 function more readable
    - Call worker function directly for __rt_sdiv/udiv
    - Adapt __rt_sdiv64/udiv64 asm shims accordingly
    - Add header files to CMake source list
    
    CORE-17607 CORE-17614 CORE-17703 CORE-17604
    
    Addendum to f2bc1f0e, e448094e and 54406bf4.
---
 sdk/lib/crt/math/arm/__dtoi64.c        |  5 +++--
 sdk/lib/crt/math/arm/__dtou64.c        |  5 +++--
 sdk/lib/crt/math/arm/__fto64.h         | 24 +++++++++++----------
 sdk/lib/crt/math/arm/__rt_div_worker.h | 38 +++++++++++++++++-----------------
 sdk/lib/crt/math/arm/__rt_sdiv.c       | 19 +++--------------
 sdk/lib/crt/math/arm/__rt_sdiv64.s     | 11 +++++++---
 sdk/lib/crt/math/arm/__rt_udiv.c       | 19 +++--------------
 sdk/lib/crt/math/arm/__rt_udiv64.s     | 11 +++++++---
 sdk/lib/crt/math/arm/__stoi64.c        |  5 +++--
 sdk/lib/crt/math/arm/__stou64.c        |  5 +++--
 sdk/lib/crt/math/math.cmake            |  2 ++
 sdk/lib/crt/msvcrtex.cmake             |  2 ++
 12 files changed, 70 insertions(+), 76 deletions(-)

diff --git a/sdk/lib/crt/math/arm/__dtoi64.c b/sdk/lib/crt/math/arm/__dtoi64.c
index d6471c83254..1e85e469111 100644
--- a/sdk/lib/crt/math/arm/__dtoi64.c
+++ b/sdk/lib/crt/math/arm/__dtoi64.c
@@ -2,8 +2,7 @@
  * PROJECT:     ReactOS CRT library
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __dtoi64
- * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Roman Masanin <[email protected]>
+ * COPYRIGHT:   Copyright 2021 Roman Masanin <[email protected]>
  */
 
 #define __fto64 __dtoi64
@@ -11,3 +10,5 @@
 #define _USE_SIGNED_
 
 #include "__fto64.h"
+
+/* __dtoi64 is implemented in __fto64.h */
diff --git a/sdk/lib/crt/math/arm/__dtou64.c b/sdk/lib/crt/math/arm/__dtou64.c
index 81dc083a0f9..4ab5529789b 100644
--- a/sdk/lib/crt/math/arm/__dtou64.c
+++ b/sdk/lib/crt/math/arm/__dtou64.c
@@ -2,11 +2,12 @@
  * PROJECT:     ReactOS CRT library
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __dtou64
- * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Roman Masanin <[email protected]>
+ * COPYRIGHT:   Copyright 2021 Roman Masanin <[email protected]>
  */
 
 #define __fto64 __dtou64
 #define _USE_64_BITS_
 
 #include "__fto64.h"
+
+/* __dtou64 is implemented in __fto64.h */
diff --git a/sdk/lib/crt/math/arm/__fto64.h b/sdk/lib/crt/math/arm/__fto64.h
index c933c711565..5b02e2a36a1 100644
--- a/sdk/lib/crt/math/arm/__fto64.h
+++ b/sdk/lib/crt/math/arm/__fto64.h
@@ -23,6 +23,12 @@ unsigned
 #endif
 long long FTO64_RESULT;
 
+typedef union _FTO64_UNION
+{
+    FLOAT_TYPE value;
+    FINT_TYPE raw;
+} FTO64_UNION;
+
 #define SIGN_MASK (((FINT_TYPE)1) << (FRACTION_LEN + EXPONENT_LEN))
 
 #define FRACTION_ONE (((FINT_TYPE)1) << FRACTION_LEN)
@@ -44,18 +50,14 @@ long long FTO64_RESULT;
 #define NEGATE(x) (~(x) + 1)
 
 FTO64_RESULT
-__fto64(FLOAT_TYPE value)
+__fto64(FLOAT_TYPE fvalue)
 {
-    union {
-        FLOAT_TYPE val_float;
-        FINT_TYPE val_int;
-    } u;
+    FTO64_UNION u = { .value = fvalue };
+    FINT_TYPE value = u.raw;
     int exponent;
     FTO64_RESULT fraction;
 
-    u.val_float = value;
-
-    exponent = (int)(u.val_int >> FRACTION_LEN);
+    exponent = (int)(value >> FRACTION_LEN);
     exponent &= EXPONENT_MASK;
 
     /* infinity and other NaNs */
@@ -77,11 +79,11 @@ __fto64(FLOAT_TYPE value)
         return INTNAN;
 
 #ifndef _USE_SIGNED_
-    if (u.val_int & SIGN_MASK)
+    if (value & SIGN_MASK)
         return INTNAN;
 #endif
 
-    fraction = u.val_int & FRACTION_MASK;
+    fraction = value & FRACTION_MASK;
     fraction |= FRACTION_ONE;
 
     exponent -= FRACTION_LEN;
@@ -94,7 +96,7 @@ __fto64(FLOAT_TYPE value)
     }
 
 #ifdef _USE_SIGNED_
-    if (u.val_int & SIGN_MASK)
+    if (value & SIGN_MASK)
         fraction = NEGATE(fraction);
 #endif
 
diff --git a/sdk/lib/crt/math/arm/__rt_div_worker.h 
b/sdk/lib/crt/math/arm/__rt_div_worker.h
index c920ec528e3..f3451b0bfe1 100644
--- a/sdk/lib/crt/math/arm/__rt_div_worker.h
+++ b/sdk/lib/crt/math/arm/__rt_div_worker.h
@@ -3,7 +3,7 @@
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __rt_div_worker
  * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Raman Masanin <[email protected]>
+ *              Copyright 2021 Roman Masanin <[email protected]>
  */
 
 /*
@@ -20,10 +20,16 @@
 #ifdef _USE_64_BITS_
 typedef unsigned long long UINT3264;
 typedef long long INT3264;
+typedef struct
+{
+    unsigned long long quotient; /* to be returned in R0,R1 */
+    unsigned long long modulus;  /* to be returned in R2,R3 */
+} RETURN_TYPE;
 #define _CountLeadingZeros _CountLeadingZeros64
 #else
 typedef unsigned int UINT3264;
 typedef int INT3264;
+typedef unsigned long long RETURN_TYPE; /* to be returned in R0,R1 */
 #endif
 
 __forceinline
@@ -35,27 +41,20 @@ __brkdiv0(void)
 
 typedef union _ARM_DIVRESULT
 {
-#ifdef _USE_64_BITS_
-    unsigned int raw_data[4];
-#else
-    unsigned long long raw_data;
-#endif
+    RETURN_TYPE raw_data;
     struct
     {
-        UINT3264 quotient; /* to be returned in R0(R0,R1) */
-        UINT3264 modulus;  /* to be returned in R1(R2,R3) */
+        UINT3264 quotient;
+        UINT3264 modulus;
     } data;
 } ARM_DIVRESULT;
 
-#ifndef _USE_64_BITS_
-__forceinline
-#endif
-void
+RETURN_TYPE
 __rt_div_worker(
     UINT3264 divisor,
-    UINT3264 dividend,
-    ARM_DIVRESULT* result)
+    UINT3264 dividend)
 {
+    ARM_DIVRESULT result;
     UINT3264 shift;
     UINT3264 mask;
     UINT3264 quotient;
@@ -86,13 +85,13 @@ __rt_div_worker(
 
     if (divisor > dividend)
     {
-        result->data.quotient = 0;
+        result.data.quotient = 0;
 #ifdef _SIGNED_DIV_
         if (dividend_sign)
             dividend = -(INT3264)dividend;
 #endif // _SIGNED_DIV_
-        result->data.modulus = dividend;
-        return;
+        result.data.modulus = dividend;
+        return result.raw_data;
     }
 
     /* Get the difference in count of leading zeros between dividend and 
divisor */
@@ -130,6 +129,7 @@ __rt_div_worker(
     }
 #endif // _SIGNED_DIV_
 
-    result->data.quotient = quotient;
-    result->data.modulus = dividend;
+    result.data.quotient = quotient;
+    result.data.modulus = dividend;
+    return result.raw_data;
 }
diff --git a/sdk/lib/crt/math/arm/__rt_sdiv.c b/sdk/lib/crt/math/arm/__rt_sdiv.c
index 96e0eafe334..56e562e9f69 100644
--- a/sdk/lib/crt/math/arm/__rt_sdiv.c
+++ b/sdk/lib/crt/math/arm/__rt_sdiv.c
@@ -3,25 +3,12 @@
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __rt_sdiv
  * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Raman Masanin <[email protected]>
+ *              Copyright 2021 Roman Masanin <[email protected]>
  */
 
-#define __rt_div_worker __rt_sdiv_worker
+#define __rt_div_worker __rt_sdiv
 #define _SIGNED_DIV_
 
 #include "__rt_div_worker.h"
 
-/*
- * Returns quotient in R0, remainder in R1
- */
-long long
-__rt_sdiv(
-    int divisor,
-    int dividend)
-{
-    ARM_DIVRESULT result;
-
-    __rt_sdiv_worker(divisor, dividend, &result);
-
-    return result.raw_data;
-}
+/* __rt_sdiv is implemented in __rt_div_worker.h */
diff --git a/sdk/lib/crt/math/arm/__rt_sdiv64.s 
b/sdk/lib/crt/math/arm/__rt_sdiv64.s
index 57f21edcaef..ed3dc69c110 100644
--- a/sdk/lib/crt/math/arm/__rt_sdiv64.s
+++ b/sdk/lib/crt/math/arm/__rt_sdiv64.s
@@ -3,7 +3,7 @@
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __rt_sdiv64
  * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Raman Masanin <[email protected]>
+ *              Copyright 2021 Roman Masanin <[email protected]>
  */
 
 /* INCLUDES ******************************************************************/
@@ -28,12 +28,17 @@
     push {lr}
     sub sp,sp,0x10
     mov r12,sp
-    push {r12}
+    push {r2,r3}
     PROLOG_END
 
+    /* r0 = ret*, r2:r3 = divisor, [sp] = dividend */
+    mov r3,r1
+    mov r2,r0
+    mov r0,r12
+
     /* Call the C worker function */
     bl __rt_sdiv64_worker
-    add sp,sp,0x04
+    add sp,sp,0x08
 
     /* Move result data into the appropriate registers and return */
     pop {r0,r1,r2,r3,pc}
diff --git a/sdk/lib/crt/math/arm/__rt_udiv.c b/sdk/lib/crt/math/arm/__rt_udiv.c
index 9863ce4d83d..708cfce0b38 100644
--- a/sdk/lib/crt/math/arm/__rt_udiv.c
+++ b/sdk/lib/crt/math/arm/__rt_udiv.c
@@ -3,24 +3,11 @@
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __rt_udiv
  * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Raman Masanin <[email protected]>
+ *              Copyright 2021 Roman Masanin <[email protected]>
  */
 
-#define __rt_div_worker __rt_udiv_worker
+#define __rt_div_worker __rt_udiv
 
 #include "__rt_div_worker.h"
 
- /*
-  * Returns quotient in R0, remainder in R1
-  */
-unsigned long long
-__rt_udiv(
-    unsigned int divisor,
-    unsigned int dividend)
-{
-    ARM_DIVRESULT result;
-
-    __rt_udiv_worker(divisor, dividend, &result);
-
-    return result.raw_data;
-}
+/* __rt_udiv is implemented in __rt_div_worker.h */
diff --git a/sdk/lib/crt/math/arm/__rt_udiv64.s 
b/sdk/lib/crt/math/arm/__rt_udiv64.s
index 182ff33595a..0f2bb09c2fc 100644
--- a/sdk/lib/crt/math/arm/__rt_udiv64.s
+++ b/sdk/lib/crt/math/arm/__rt_udiv64.s
@@ -3,7 +3,7 @@
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __rt_udiv64
  * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Raman Masanin <[email protected]>
+ *              Copyright 2021 Roman Masanin <[email protected]>
  */
 
 /* INCLUDES ******************************************************************/
@@ -28,12 +28,17 @@
     push {lr}
     sub sp,sp,0x10
     mov r12,sp
-    push {r12}
+    push {r2,r3}
     PROLOG_END
 
+    /* r0 = ret*, r2:r3 = divisor, [sp] = dividend */
+    mov r3,r1
+    mov r2,r0
+    mov r0,r12
+
     /* Call the C worker function */
     bl __rt_udiv64_worker
-    add sp,sp,0x04
+    add sp,sp,0x08
 
     /* Move result data into the appropriate registers and return */
     pop {r0,r1,r2,r3,pc}
diff --git a/sdk/lib/crt/math/arm/__stoi64.c b/sdk/lib/crt/math/arm/__stoi64.c
index 455d0cf383c..dc80d8db00e 100644
--- a/sdk/lib/crt/math/arm/__stoi64.c
+++ b/sdk/lib/crt/math/arm/__stoi64.c
@@ -2,11 +2,12 @@
  * PROJECT:     ReactOS CRT library
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __stoi64
- * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Roman Masanin <[email protected]>
+ * COPYRIGHT:   Copyright 2021 Roman Masanin <[email protected]>
  */
 
 #define __fto64 __stoi64
 #define _USE_SIGNED_
 
 #include "__fto64.h"
+
+/* __stoi64 is implemented in __fto64.h */
diff --git a/sdk/lib/crt/math/arm/__stou64.c b/sdk/lib/crt/math/arm/__stou64.c
index 65c9102a27f..92fba8fa001 100644
--- a/sdk/lib/crt/math/arm/__stou64.c
+++ b/sdk/lib/crt/math/arm/__stou64.c
@@ -2,10 +2,11 @@
  * PROJECT:     ReactOS CRT library
  * LICENSE:     MIT (https://spdx.org/licenses/MIT)
  * PURPOSE:     Implementation of __stou64
- * COPYRIGHT:   Copyright 2015 Timo Kreuzer <[email protected]>
- *              Copyright 2021 Roman Masanin <[email protected]>
+ * COPYRIGHT:   Copyright 2021 Roman Masanin <[email protected]>
  */
 
 #define __fto64 __stou64
 
 #include "__fto64.h"
+
+/* __stou64 is implemented in __fto64.h */
diff --git a/sdk/lib/crt/math/math.cmake b/sdk/lib/crt/math/math.cmake
index 8e2cd175e9a..9bb584d4bc7 100644
--- a/sdk/lib/crt/math/math.cmake
+++ b/sdk/lib/crt/math/math.cmake
@@ -79,10 +79,12 @@ elseif(ARCH STREQUAL "arm")
         math/arm/__rt_sdiv64_worker.c
         math/arm/__rt_udiv.c
         math/arm/__rt_udiv64_worker.c
+        math/arm/__rt_div_worker.h
         math/arm/__dtoi64.c
         math/arm/__dtou64.c
         math/arm/__stoi64.c
         math/arm/__stou64.c
+        math/arm/__fto64.h
     )
     list(APPEND CRT_MATH_SOURCE
         math/fabsf.c
diff --git a/sdk/lib/crt/msvcrtex.cmake b/sdk/lib/crt/msvcrtex.cmake
index 7c6544e85ff..756f0036faa 100644
--- a/sdk/lib/crt/msvcrtex.cmake
+++ b/sdk/lib/crt/msvcrtex.cmake
@@ -72,10 +72,12 @@ elseif(ARCH STREQUAL "arm")
         math/arm/__rt_sdiv64_worker.c
         math/arm/__rt_udiv.c
         math/arm/__rt_udiv64_worker.c
+        math/arm/__rt_div_worker.h
         math/arm/__dtoi64.c
         math/arm/__dtou64.c
         math/arm/__stoi64.c
         math/arm/__stou64.c
+        math/arm/__fto64.h
     )
     list(APPEND MSVCRTEX_ASM_SOURCE
         except/arm/chkstk_asm.s

Reply via email to