diff --git a/reactos/lib/sdk/crt/crt.cmake b/reactos/lib/sdk/crt/crt.cmake index adec403a14f..2bf0752df84 100644 --- a/reactos/lib/sdk/crt/crt.cmake +++ b/reactos/lib/sdk/crt/crt.cmake @@ -475,6 +475,9 @@ elseif(ARCH STREQUAL "amd64") except/amd64/cpp.s) endif() elseif(ARCH STREQUAL "arm") + list(APPEND LIBCNTPR_SOURCE + math/arm/__rt_udiv.c + ) list(APPEND CRT_ASM_SOURCE math/arm/floor.s math/arm/log10.s @@ -483,7 +486,6 @@ elseif(ARCH STREQUAL "arm") math/arm/__u64tod.s math/arm/__rt_sdiv.s math/arm/__rt_sdiv64.s - math/arm/__rt_udiv.s math/arm/__rt_udiv64.s ) endif() diff --git a/reactos/lib/sdk/crt/libcntpr.cmake b/reactos/lib/sdk/crt/libcntpr.cmake index 4b63e1c5b5d..6747dc412a3 100644 --- a/reactos/lib/sdk/crt/libcntpr.cmake +++ b/reactos/lib/sdk/crt/libcntpr.cmake @@ -134,6 +134,9 @@ elseif(ARCH STREQUAL "amd64") math/cos.c math/sin.c) elseif(ARCH STREQUAL "arm") + list(APPEND LIBCNTPR_SOURCE + math/arm/__rt_udiv.c + ) list(APPEND LIBCNTPR_ASM_SOURCE math/arm/floor.s math/arm/log10.s @@ -142,7 +145,6 @@ elseif(ARCH STREQUAL "arm") math/arm/__u64tod.s math/arm/__rt_sdiv.s math/arm/__rt_sdiv64.s - math/arm/__rt_udiv.s math/arm/__rt_udiv64.s ) endif() diff --git a/reactos/lib/sdk/crt/math/arm/__rt_udiv.c b/reactos/lib/sdk/crt/math/arm/__rt_udiv.c new file mode 100644 index 00000000000..3cb1ddf3740 --- /dev/null +++ b/reactos/lib/sdk/crt/math/arm/__rt_udiv.c @@ -0,0 +1,73 @@ +/* + * COPYRIGHT: BSD, see COPYING.ARM in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/math/arm/__rt_udiv.c + * PURPOSE: Implementation of __rt_udiv + * PROGRAMMER: Timo Kreuzer + * REFERENCE: http://research.microsoft.com/en-us/um/redmond/projects/invisible/src/crt/md/arm/_div10.s.htm + * http://research.microsoft.com/en-us/um/redmond/projects/invisible/src/crt/md/arm/_udiv.c.htm + */ + +typedef struct _ARM_UDIVRESULT +{ + unsigned int quotient; /* to be returned in R0 */ + unsigned int modulus; /* to be returned in R1 */ +} ARM_UDIVRESULT; + +__forceinline +void +__brkdiv0(void) +{ + __emit(0xDEF9); +} + +ARM_UDIVRESULT +__rt_udiv( + unsigned int divisor, + unsigned int dividend) +{ + ARM_UDIVRESULT result; + unsigned int BitShift; + unsigned int BitMask; + unsigned int Quotient; + + if (divisor == 0) + { + /* Raise divide by zero error */ + __brkdiv0(); + } + + if (divisor > dividend) + { + result.quotient = 0; + result.modulus = divisor; + return result; + } + + /* Get the difference in count of leading zeros between dividend and divisor */ + BitShift = _CountLeadingZeros(divisor); + BitShift -= _CountLeadingZeros(dividend); + + /* Shift the divisor to the left, so that it's highest bit is the same + as the highest bit of the dividend */ + divisor <<= BitShift; + + BitMask = 1 << BitShift; + + do + { + if (dividend >= divisor) + { + Quotient |= BitMask; + dividend -= divisor; + } + divisor >>= 1; + BitMask >>= 1; + } + while (BitMask); + + result.quotient = Quotient; + result.modulus = dividend; + return result; +} + diff --git a/reactos/lib/sdk/crt/math/arm/__rt_udiv.s b/reactos/lib/sdk/crt/math/arm/__rt_udiv.s deleted file mode 100644 index 9a14a8b348c..00000000000 --- a/reactos/lib/sdk/crt/math/arm/__rt_udiv.s +++ /dev/null @@ -1,20 +0,0 @@ -/* - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * PURPOSE: Implementation of __rt_udiv - * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org) - */ - -/* INCLUDES ******************************************************************/ - -#include - -/* CODE **********************************************************************/ - TEXTAREA - - LEAF_ENTRY __rt_udiv - - LEAF_END __rt_udiv - - END -/* EOF */