[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.
This commit is contained in:
Roman Masanin 2021-07-31 04:04:25 +03:00 committed by Stanislav Motylkov
parent d7f13aa696
commit be3c532bf4
No known key found for this signature in database
GPG key ID: AFE513258CBA9E92
12 changed files with 70 additions and 76 deletions

View file

@ -2,8 +2,7 @@
* PROJECT: ReactOS CRT library * PROJECT: ReactOS CRT library
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __dtoi64 * PURPOSE: Implementation of __dtoi64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2021 Roman Masanin <36927roma@gmail.com>
* Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __fto64 __dtoi64 #define __fto64 __dtoi64
@ -11,3 +10,5 @@
#define _USE_SIGNED_ #define _USE_SIGNED_
#include "__fto64.h" #include "__fto64.h"
/* __dtoi64 is implemented in __fto64.h */

View file

@ -2,11 +2,12 @@
* PROJECT: ReactOS CRT library * PROJECT: ReactOS CRT library
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __dtou64 * PURPOSE: Implementation of __dtou64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2021 Roman Masanin <36927roma@gmail.com>
* Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __fto64 __dtou64 #define __fto64 __dtou64
#define _USE_64_BITS_ #define _USE_64_BITS_
#include "__fto64.h" #include "__fto64.h"
/* __dtou64 is implemented in __fto64.h */

View file

@ -23,6 +23,12 @@ unsigned
#endif #endif
long long FTO64_RESULT; 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 SIGN_MASK (((FINT_TYPE)1) << (FRACTION_LEN + EXPONENT_LEN))
#define FRACTION_ONE (((FINT_TYPE)1) << FRACTION_LEN) #define FRACTION_ONE (((FINT_TYPE)1) << FRACTION_LEN)
@ -44,18 +50,14 @@ long long FTO64_RESULT;
#define NEGATE(x) (~(x) + 1) #define NEGATE(x) (~(x) + 1)
FTO64_RESULT FTO64_RESULT
__fto64(FLOAT_TYPE value) __fto64(FLOAT_TYPE fvalue)
{ {
union { FTO64_UNION u = { .value = fvalue };
FLOAT_TYPE val_float; FINT_TYPE value = u.raw;
FINT_TYPE val_int;
} u;
int exponent; int exponent;
FTO64_RESULT fraction; FTO64_RESULT fraction;
u.val_float = value; exponent = (int)(value >> FRACTION_LEN);
exponent = (int)(u.val_int >> FRACTION_LEN);
exponent &= EXPONENT_MASK; exponent &= EXPONENT_MASK;
/* infinity and other NaNs */ /* infinity and other NaNs */
@ -77,11 +79,11 @@ __fto64(FLOAT_TYPE value)
return INTNAN; return INTNAN;
#ifndef _USE_SIGNED_ #ifndef _USE_SIGNED_
if (u.val_int & SIGN_MASK) if (value & SIGN_MASK)
return INTNAN; return INTNAN;
#endif #endif
fraction = u.val_int & FRACTION_MASK; fraction = value & FRACTION_MASK;
fraction |= FRACTION_ONE; fraction |= FRACTION_ONE;
exponent -= FRACTION_LEN; exponent -= FRACTION_LEN;
@ -94,7 +96,7 @@ __fto64(FLOAT_TYPE value)
} }
#ifdef _USE_SIGNED_ #ifdef _USE_SIGNED_
if (u.val_int & SIGN_MASK) if (value & SIGN_MASK)
fraction = NEGATE(fraction); fraction = NEGATE(fraction);
#endif #endif

View file

@ -3,7 +3,7 @@
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __rt_div_worker * PURPOSE: Implementation of __rt_div_worker
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org>
* Copyright 2021 Raman Masanin <36927roma@gmail.com> * Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
/* /*
@ -20,10 +20,16 @@
#ifdef _USE_64_BITS_ #ifdef _USE_64_BITS_
typedef unsigned long long UINT3264; typedef unsigned long long UINT3264;
typedef long long INT3264; 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 #define _CountLeadingZeros _CountLeadingZeros64
#else #else
typedef unsigned int UINT3264; typedef unsigned int UINT3264;
typedef int INT3264; typedef int INT3264;
typedef unsigned long long RETURN_TYPE; /* to be returned in R0,R1 */
#endif #endif
__forceinline __forceinline
@ -35,27 +41,20 @@ __brkdiv0(void)
typedef union _ARM_DIVRESULT typedef union _ARM_DIVRESULT
{ {
#ifdef _USE_64_BITS_ RETURN_TYPE raw_data;
unsigned int raw_data[4];
#else
unsigned long long raw_data;
#endif
struct struct
{ {
UINT3264 quotient; /* to be returned in R0(R0,R1) */ UINT3264 quotient;
UINT3264 modulus; /* to be returned in R1(R2,R3) */ UINT3264 modulus;
} data; } data;
} ARM_DIVRESULT; } ARM_DIVRESULT;
#ifndef _USE_64_BITS_ RETURN_TYPE
__forceinline
#endif
void
__rt_div_worker( __rt_div_worker(
UINT3264 divisor, UINT3264 divisor,
UINT3264 dividend, UINT3264 dividend)
ARM_DIVRESULT* result)
{ {
ARM_DIVRESULT result;
UINT3264 shift; UINT3264 shift;
UINT3264 mask; UINT3264 mask;
UINT3264 quotient; UINT3264 quotient;
@ -86,13 +85,13 @@ __rt_div_worker(
if (divisor > dividend) if (divisor > dividend)
{ {
result->data.quotient = 0; result.data.quotient = 0;
#ifdef _SIGNED_DIV_ #ifdef _SIGNED_DIV_
if (dividend_sign) if (dividend_sign)
dividend = -(INT3264)dividend; dividend = -(INT3264)dividend;
#endif // _SIGNED_DIV_ #endif // _SIGNED_DIV_
result->data.modulus = dividend; result.data.modulus = dividend;
return; return result.raw_data;
} }
/* Get the difference in count of leading zeros between dividend and divisor */ /* Get the difference in count of leading zeros between dividend and divisor */
@ -130,6 +129,7 @@ __rt_div_worker(
} }
#endif // _SIGNED_DIV_ #endif // _SIGNED_DIV_
result->data.quotient = quotient; result.data.quotient = quotient;
result->data.modulus = dividend; result.data.modulus = dividend;
return result.raw_data;
} }

View file

@ -3,25 +3,12 @@
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __rt_sdiv * PURPOSE: Implementation of __rt_sdiv
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org>
* Copyright 2021 Raman Masanin <36927roma@gmail.com> * Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __rt_div_worker __rt_sdiv_worker #define __rt_div_worker __rt_sdiv
#define _SIGNED_DIV_ #define _SIGNED_DIV_
#include "__rt_div_worker.h" #include "__rt_div_worker.h"
/* /* __rt_sdiv is implemented in __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;
}

View file

@ -3,7 +3,7 @@
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __rt_sdiv64 * PURPOSE: Implementation of __rt_sdiv64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org>
* Copyright 2021 Raman Masanin <36927roma@gmail.com> * Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
/* INCLUDES ******************************************************************/ /* INCLUDES ******************************************************************/
@ -28,12 +28,17 @@
push {lr} push {lr}
sub sp,sp,0x10 sub sp,sp,0x10
mov r12,sp mov r12,sp
push {r12} push {r2,r3}
PROLOG_END PROLOG_END
/* r0 = ret*, r2:r3 = divisor, [sp] = dividend */
mov r3,r1
mov r2,r0
mov r0,r12
/* Call the C worker function */ /* Call the C worker function */
bl __rt_sdiv64_worker bl __rt_sdiv64_worker
add sp,sp,0x04 add sp,sp,0x08
/* Move result data into the appropriate registers and return */ /* Move result data into the appropriate registers and return */
pop {r0,r1,r2,r3,pc} pop {r0,r1,r2,r3,pc}

View file

@ -3,24 +3,11 @@
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __rt_udiv * PURPOSE: Implementation of __rt_udiv
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org>
* Copyright 2021 Raman Masanin <36927roma@gmail.com> * Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __rt_div_worker __rt_udiv_worker #define __rt_div_worker __rt_udiv
#include "__rt_div_worker.h" #include "__rt_div_worker.h"
/* /* __rt_udiv is implemented in __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;
}

View file

@ -3,7 +3,7 @@
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __rt_udiv64 * PURPOSE: Implementation of __rt_udiv64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org>
* Copyright 2021 Raman Masanin <36927roma@gmail.com> * Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
/* INCLUDES ******************************************************************/ /* INCLUDES ******************************************************************/
@ -28,12 +28,17 @@
push {lr} push {lr}
sub sp,sp,0x10 sub sp,sp,0x10
mov r12,sp mov r12,sp
push {r12} push {r2,r3}
PROLOG_END PROLOG_END
/* r0 = ret*, r2:r3 = divisor, [sp] = dividend */
mov r3,r1
mov r2,r0
mov r0,r12
/* Call the C worker function */ /* Call the C worker function */
bl __rt_udiv64_worker bl __rt_udiv64_worker
add sp,sp,0x04 add sp,sp,0x08
/* Move result data into the appropriate registers and return */ /* Move result data into the appropriate registers and return */
pop {r0,r1,r2,r3,pc} pop {r0,r1,r2,r3,pc}

View file

@ -2,11 +2,12 @@
* PROJECT: ReactOS CRT library * PROJECT: ReactOS CRT library
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __stoi64 * PURPOSE: Implementation of __stoi64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2021 Roman Masanin <36927roma@gmail.com>
* Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __fto64 __stoi64 #define __fto64 __stoi64
#define _USE_SIGNED_ #define _USE_SIGNED_
#include "__fto64.h" #include "__fto64.h"
/* __stoi64 is implemented in __fto64.h */

View file

@ -2,10 +2,11 @@
* PROJECT: ReactOS CRT library * PROJECT: ReactOS CRT library
* LICENSE: MIT (https://spdx.org/licenses/MIT) * LICENSE: MIT (https://spdx.org/licenses/MIT)
* PURPOSE: Implementation of __stou64 * PURPOSE: Implementation of __stou64
* COPYRIGHT: Copyright 2015 Timo Kreuzer <timo.kreuzer@reactos.org> * COPYRIGHT: Copyright 2021 Roman Masanin <36927roma@gmail.com>
* Copyright 2021 Roman Masanin <36927roma@gmail.com>
*/ */
#define __fto64 __stou64 #define __fto64 __stou64
#include "__fto64.h" #include "__fto64.h"
/* __stou64 is implemented in __fto64.h */

View file

@ -79,10 +79,12 @@ elseif(ARCH STREQUAL "arm")
math/arm/__rt_sdiv64_worker.c math/arm/__rt_sdiv64_worker.c
math/arm/__rt_udiv.c math/arm/__rt_udiv.c
math/arm/__rt_udiv64_worker.c math/arm/__rt_udiv64_worker.c
math/arm/__rt_div_worker.h
math/arm/__dtoi64.c math/arm/__dtoi64.c
math/arm/__dtou64.c math/arm/__dtou64.c
math/arm/__stoi64.c math/arm/__stoi64.c
math/arm/__stou64.c math/arm/__stou64.c
math/arm/__fto64.h
) )
list(APPEND CRT_MATH_SOURCE list(APPEND CRT_MATH_SOURCE
math/fabsf.c math/fabsf.c

View file

@ -72,10 +72,12 @@ elseif(ARCH STREQUAL "arm")
math/arm/__rt_sdiv64_worker.c math/arm/__rt_sdiv64_worker.c
math/arm/__rt_udiv.c math/arm/__rt_udiv.c
math/arm/__rt_udiv64_worker.c math/arm/__rt_udiv64_worker.c
math/arm/__rt_div_worker.h
math/arm/__dtoi64.c math/arm/__dtoi64.c
math/arm/__dtou64.c math/arm/__dtou64.c
math/arm/__stoi64.c math/arm/__stoi64.c
math/arm/__stou64.c math/arm/__stou64.c
math/arm/__fto64.h
) )
list(APPEND MSVCRTEX_ASM_SOURCE list(APPEND MSVCRTEX_ASM_SOURCE
except/arm/chkstk_asm.s except/arm/chkstk_asm.s