xref: /DragonStub/lib/mips64el/math.c (revision 3f234f5b6ff5c16c6ddd5f2c2513fb329aecd30a)
1*3f234f5bSNigel Croxon /*
2*3f234f5bSNigel Croxon  * Copright (C) 2014 Linaro Ltd.
3*3f234f5bSNigel Croxon  * Author: Ard Biesheuvel <ard.biesheuvel@linaro.org>
4*3f234f5bSNigel Croxon  *
5*3f234f5bSNigel Croxon  * Redistribution and use in source and binary forms, with or without
6*3f234f5bSNigel Croxon  * modification, are permitted provided that the following conditions
7*3f234f5bSNigel Croxon  * are met:
8*3f234f5bSNigel Croxon  * 1. Redistributions of source code must retain the above copyright
9*3f234f5bSNigel Croxon  *    notice and this list of conditions, without modification.
10*3f234f5bSNigel Croxon  * 2. The name of the author may not be used to endorse or promote products
11*3f234f5bSNigel Croxon  *    derived from this software without specific prior written permission.
12*3f234f5bSNigel Croxon  *
13*3f234f5bSNigel Croxon  * Alternatively, this software may be distributed under the terms of the
14*3f234f5bSNigel Croxon  * GNU General Public License as published by the Free Software Foundation;
15*3f234f5bSNigel Croxon  * either version 2 of the License, or (at your option) any later version.
16*3f234f5bSNigel Croxon  */
17*3f234f5bSNigel Croxon 
18*3f234f5bSNigel Croxon #include "lib.h"
19*3f234f5bSNigel Croxon 
20*3f234f5bSNigel Croxon UINT64
LShiftU64(IN UINT64 Operand,IN UINTN Count)21*3f234f5bSNigel Croxon LShiftU64 (
22*3f234f5bSNigel Croxon     IN UINT64   Operand,
23*3f234f5bSNigel Croxon     IN UINTN    Count
24*3f234f5bSNigel Croxon     )
25*3f234f5bSNigel Croxon // Left shift 64bit by 32bit and get a 64bit result
26*3f234f5bSNigel Croxon {
27*3f234f5bSNigel Croxon     return Operand << Count;
28*3f234f5bSNigel Croxon }
29*3f234f5bSNigel Croxon 
30*3f234f5bSNigel Croxon UINT64
RShiftU64(IN UINT64 Operand,IN UINTN Count)31*3f234f5bSNigel Croxon RShiftU64 (
32*3f234f5bSNigel Croxon     IN UINT64   Operand,
33*3f234f5bSNigel Croxon     IN UINTN    Count
34*3f234f5bSNigel Croxon     )
35*3f234f5bSNigel Croxon // Right shift 64bit by 32bit and get a 64bit result
36*3f234f5bSNigel Croxon {
37*3f234f5bSNigel Croxon     return Operand >> Count;
38*3f234f5bSNigel Croxon }
39*3f234f5bSNigel Croxon 
40*3f234f5bSNigel Croxon 
41*3f234f5bSNigel Croxon UINT64
MultU64x32(IN UINT64 Multiplicand,IN UINTN Multiplier)42*3f234f5bSNigel Croxon MultU64x32 (
43*3f234f5bSNigel Croxon     IN UINT64   Multiplicand,
44*3f234f5bSNigel Croxon     IN UINTN    Multiplier
45*3f234f5bSNigel Croxon     )
46*3f234f5bSNigel Croxon // Multiple 64bit by 32bit and get a 64bit result
47*3f234f5bSNigel Croxon {
48*3f234f5bSNigel Croxon     return Multiplicand * Multiplier;
49*3f234f5bSNigel Croxon }
50*3f234f5bSNigel Croxon 
51*3f234f5bSNigel Croxon UINT64
DivU64x32(IN UINT64 Dividend,IN UINTN Divisor,OUT UINTN * Remainder OPTIONAL)52*3f234f5bSNigel Croxon DivU64x32 (
53*3f234f5bSNigel Croxon     IN UINT64   Dividend,
54*3f234f5bSNigel Croxon     IN UINTN    Divisor,
55*3f234f5bSNigel Croxon     OUT UINTN   *Remainder OPTIONAL
56*3f234f5bSNigel Croxon     )
57*3f234f5bSNigel Croxon // divide 64bit by 32bit and get a 64bit result
58*3f234f5bSNigel Croxon // N.B. only works for 31bit divisors!!
59*3f234f5bSNigel Croxon {
60*3f234f5bSNigel Croxon     if (Remainder)
61*3f234f5bSNigel Croxon 	*Remainder = Dividend % Divisor;
62*3f234f5bSNigel Croxon     return Dividend / Divisor;
63*3f234f5bSNigel Croxon }
64