math.c revision 1.1.1.1.6.2 1 1.1.1.1.6.2 christos /* $NetBSD: math.c,v 1.1.1.1.6.2 2019/06/10 22:08:36 christos Exp $ */
2 1.1.1.1.6.2 christos
3 1.1.1.1.6.2 christos /*
4 1.1.1.1.6.2 christos * Copright (C) 2014 Linaro Ltd.
5 1.1.1.1.6.2 christos * Author: Ard Biesheuvel <ard.biesheuvel (at) linaro.org>
6 1.1.1.1.6.2 christos *
7 1.1.1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.1.1.1.6.2 christos * modification, are permitted provided that the following conditions
9 1.1.1.1.6.2 christos * are met:
10 1.1.1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.1.1.1.6.2 christos * notice and this list of conditions, without modification.
12 1.1.1.1.6.2 christos * 2. The name of the author may not be used to endorse or promote products
13 1.1.1.1.6.2 christos * derived from this software without specific prior written permission.
14 1.1.1.1.6.2 christos *
15 1.1.1.1.6.2 christos * Alternatively, this software may be distributed under the terms of the
16 1.1.1.1.6.2 christos * GNU General Public License as published by the Free Software Foundation;
17 1.1.1.1.6.2 christos * either version 2 of the License, or (at your option) any later version.
18 1.1.1.1.6.2 christos */
19 1.1.1.1.6.2 christos
20 1.1.1.1.6.2 christos #include "lib.h"
21 1.1.1.1.6.2 christos
22 1.1.1.1.6.2 christos UINT64
23 1.1.1.1.6.2 christos LShiftU64 (
24 1.1.1.1.6.2 christos IN UINT64 Operand,
25 1.1.1.1.6.2 christos IN UINTN Count
26 1.1.1.1.6.2 christos )
27 1.1.1.1.6.2 christos // Left shift 64bit by 32bit and get a 64bit result
28 1.1.1.1.6.2 christos {
29 1.1.1.1.6.2 christos return Operand << Count;
30 1.1.1.1.6.2 christos }
31 1.1.1.1.6.2 christos
32 1.1.1.1.6.2 christos UINT64
33 1.1.1.1.6.2 christos RShiftU64 (
34 1.1.1.1.6.2 christos IN UINT64 Operand,
35 1.1.1.1.6.2 christos IN UINTN Count
36 1.1.1.1.6.2 christos )
37 1.1.1.1.6.2 christos // Right shift 64bit by 32bit and get a 64bit result
38 1.1.1.1.6.2 christos {
39 1.1.1.1.6.2 christos return Operand >> Count;
40 1.1.1.1.6.2 christos }
41 1.1.1.1.6.2 christos
42 1.1.1.1.6.2 christos
43 1.1.1.1.6.2 christos UINT64
44 1.1.1.1.6.2 christos MultU64x32 (
45 1.1.1.1.6.2 christos IN UINT64 Multiplicand,
46 1.1.1.1.6.2 christos IN UINTN Multiplier
47 1.1.1.1.6.2 christos )
48 1.1.1.1.6.2 christos // Multiply 64bit by 32bit and get a 64bit result
49 1.1.1.1.6.2 christos {
50 1.1.1.1.6.2 christos return Multiplicand * Multiplier;
51 1.1.1.1.6.2 christos }
52 1.1.1.1.6.2 christos
53 1.1.1.1.6.2 christos UINT64
54 1.1.1.1.6.2 christos DivU64x32 (
55 1.1.1.1.6.2 christos IN UINT64 Dividend,
56 1.1.1.1.6.2 christos IN UINTN Divisor,
57 1.1.1.1.6.2 christos OUT UINTN *Remainder OPTIONAL
58 1.1.1.1.6.2 christos )
59 1.1.1.1.6.2 christos {
60 1.1.1.1.6.2 christos /*
61 1.1.1.1.6.2 christos * GCC turns a division into a multiplication and shift with precalculated
62 1.1.1.1.6.2 christos * constants if the divisor is constant and the dividend fits into a 32 bit
63 1.1.1.1.6.2 christos * variable. Otherwise, it will turn this into calls into the 32-bit div
64 1.1.1.1.6.2 christos * library functions.
65 1.1.1.1.6.2 christos */
66 1.1.1.1.6.2 christos if (Remainder)
67 1.1.1.1.6.2 christos *Remainder = Dividend % Divisor;
68 1.1.1.1.6.2 christos return Dividend / Divisor;
69 1.1.1.1.6.2 christos }
70