1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include "hal_base.h"
7 
8 /** @addtogroup RK_HAL_Driver
9  *  @{
10  */
11 
12 /** @addtogroup HAL_BASE_EX
13  *  @{
14  */
15 
16 /** @addtogroup HAL_BASE_EX_How_To_Use How To Use
17  *  @{
18 
19  The HAL_BASE_EX driver can be used as follows:
20 
21  @} */
22 
23 /** @defgroup HAL_BASE_EX_Private_Definition Private Definition
24  *  @{
25  */
26 /********************* Private MACRO Definition ******************************/
27 
28 /********************* Private Structure Definition **************************/
29 
30 /********************* Private Variable Definition ***************************/
31 
32 /********************* Private Function Definition ***************************/
33 
34 /** @} */
35 /********************* Public Function Definition ***************************/
36 /** @defgroup HAL_BASE_EX_Exported_Functions_Group5 Other Functions
37  *  @{
38  */
39 
40 /**
41  * @brief  uint64_t numerator / uint32_t denominator with remainder
42  * @param  numerator
43  * @param  denominator
44  * @param  pRemainder [out] pointer to unsigned 32bit remainder
45  * @return uint64_t result. sets *pRemainder if pRemainder is not null
46  */
HAL_DivU64Rem(uint64_t numerator,uint32_t denominator,uint32_t * pRemainder)47 uint64_t HAL_DivU64Rem(uint64_t numerator, uint32_t denominator, uint32_t *pRemainder)
48 {
49     uint64_t remainder = numerator;
50     uint64_t b = denominator;
51     uint64_t result;
52     uint64_t d = 1;
53     uint32_t high = numerator >> 32;
54 
55     result = 0;
56     if (high >= denominator) {
57         high /= denominator;
58         result = (uint64_t)high << 32;
59         remainder -= (uint64_t)(high * denominator) << 32;
60     }
61 
62     while ((int64_t)b > 0 && b < remainder) {
63         b = b + b;
64         d = d + d;
65     }
66 
67     do {
68         if (remainder >= b) {
69             remainder -= b;
70             result += d;
71         }
72         b >>= 1;
73         d >>= 1;
74     } while (d);
75 
76     if (pRemainder) {
77         *pRemainder = remainder;
78     }
79 
80     return result;
81 }
82 
83 /** @} */
84 
85 /** @} */
86 
87 /** @} */
88