xref: /DragonStub/lib/riscv64/math.c (revision 3676bc353c83c2f9f141ae5c914a45a5e096f38d)
1*b95f138fSHeinrich Schuchardt // SPDX-License-Identifier: BSD-2-Clause-Patent
2*b95f138fSHeinrich Schuchardt /*
3*b95f138fSHeinrich Schuchardt  * This code is based on EDK II MdePkg/Library/BaseLib/Math64.c
4*b95f138fSHeinrich Schuchardt  * Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
5*b95f138fSHeinrich Schuchardt  */
6*b95f138fSHeinrich Schuchardt 
7*b95f138fSHeinrich Schuchardt #include "lib.h"
8*b95f138fSHeinrich Schuchardt 
9*b95f138fSHeinrich Schuchardt /**
10*b95f138fSHeinrich Schuchardt  * LShiftU64() - left shift
11*b95f138fSHeinrich Schuchardt  */
12*b95f138fSHeinrich Schuchardt UINT64
LShiftU64(IN UINT64 Operand,IN UINTN Count)13*b95f138fSHeinrich Schuchardt LShiftU64 (
14*b95f138fSHeinrich Schuchardt 	IN UINT64   Operand,
15*b95f138fSHeinrich Schuchardt 	IN UINTN    Count
16*b95f138fSHeinrich Schuchardt )
17*b95f138fSHeinrich Schuchardt {
18*b95f138fSHeinrich Schuchardt 	return Operand << Count;
19*b95f138fSHeinrich Schuchardt }
20*b95f138fSHeinrich Schuchardt 
21*b95f138fSHeinrich Schuchardt /**
22*b95f138fSHeinrich Schuchardt  * RShiftU64() - right shift
23*b95f138fSHeinrich Schuchardt  */
24*b95f138fSHeinrich Schuchardt UINT64
RShiftU64(IN UINT64 Operand,IN UINTN Count)25*b95f138fSHeinrich Schuchardt RShiftU64 (
26*b95f138fSHeinrich Schuchardt 	IN UINT64   Operand,
27*b95f138fSHeinrich Schuchardt 	IN UINTN    Count
28*b95f138fSHeinrich Schuchardt )
29*b95f138fSHeinrich Schuchardt {
30*b95f138fSHeinrich Schuchardt 	return Operand >> Count;
31*b95f138fSHeinrich Schuchardt }
32*b95f138fSHeinrich Schuchardt 
33*b95f138fSHeinrich Schuchardt /**
34*b95f138fSHeinrich Schuchardt  * MultU64x32() - multiply
35*b95f138fSHeinrich Schuchardt  */
36*b95f138fSHeinrich Schuchardt UINT64
MultU64x32(IN UINT64 Multiplicand,IN UINTN Multiplier)37*b95f138fSHeinrich Schuchardt MultU64x32 (
38*b95f138fSHeinrich Schuchardt 	IN UINT64   Multiplicand,
39*b95f138fSHeinrich Schuchardt 	IN UINTN    Multiplier
40*b95f138fSHeinrich Schuchardt )
41*b95f138fSHeinrich Schuchardt {
42*b95f138fSHeinrich Schuchardt 	return Multiplicand * Multiplier;
43*b95f138fSHeinrich Schuchardt }
44*b95f138fSHeinrich Schuchardt 
45*b95f138fSHeinrich Schuchardt /**
46*b95f138fSHeinrich Schuchardt  * DivU64x32() - divide
47*b95f138fSHeinrich Schuchardt  */
48*b95f138fSHeinrich Schuchardt UINT64
DivU64x32(IN UINT64 Dividend,IN UINTN Divisor,OUT UINTN * Remainder OPTIONAL)49*b95f138fSHeinrich Schuchardt DivU64x32 (
50*b95f138fSHeinrich Schuchardt 	IN UINT64   Dividend,
51*b95f138fSHeinrich Schuchardt 	IN UINTN    Divisor,
52*b95f138fSHeinrich Schuchardt 	OUT UINTN   *Remainder OPTIONAL
53*b95f138fSHeinrich Schuchardt )
54*b95f138fSHeinrich Schuchardt {
55*b95f138fSHeinrich Schuchardt 	ASSERT(Divisor != 0);
56*b95f138fSHeinrich Schuchardt 
57*b95f138fSHeinrich Schuchardt 	if (Remainder) {
58*b95f138fSHeinrich Schuchardt 		*Remainder = Dividend % Divisor;
59*b95f138fSHeinrich Schuchardt 	}
60*b95f138fSHeinrich Schuchardt 
61*b95f138fSHeinrich Schuchardt 	return Dividend / Divisor;
62*b95f138fSHeinrich Schuchardt }
63