blob: 9f6f60d05f0469737ba344505c5c663bc4928398 [file] [log] [blame]
Wolfgang Denk97caf672006-03-12 02:12:27 +01001/*
Bin Meng75574052016-02-05 19:30:11 -08002 * U-Boot - muldi3.c contains routines for mult and div
Wolfgang Denk97caf672006-03-12 02:12:27 +01003 *
Aubrey Li314d22f2007-04-05 18:31:18 +08004 * Copyright (c) 2005-2007 Analog Devices Inc.
Wolfgang Denk97caf672006-03-12 02:12:27 +01005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk97caf672006-03-12 02:12:27 +01007 */
8
9/* Generic function got from GNU gcc package, libgcc2.c */
10#ifndef SI_TYPE_SIZE
11#define SI_TYPE_SIZE 32
12#endif
13#define __ll_B (1L << (SI_TYPE_SIZE / 2))
14#define __ll_lowpart(t) ((USItype) (t) % __ll_B)
15#define __ll_highpart(t) ((USItype) (t) / __ll_B)
16#define BITS_PER_UNIT 8
17
18#if !defined (umul_ppmm)
19#define umul_ppmm(w1, w0, u, v) \
20do { \
21 USItype __x0, __x1, __x2, __x3; \
22 USItype __ul, __vl, __uh, __vh; \
23 \
24 __ul = __ll_lowpart (u); \
25 __uh = __ll_highpart (u); \
26 __vl = __ll_lowpart (v); \
27 __vh = __ll_highpart (v); \
28 \
29 __x0 = (USItype) __ul * __vl; \
30 __x1 = (USItype) __ul * __vh; \
31 __x2 = (USItype) __uh * __vl; \
32 __x3 = (USItype) __uh * __vh; \
33 \
34 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
35 __x1 += __x2; /* but this indeed can */ \
36 if (__x1 < __x2) /* did we get it? */ \
37 __x3 += __ll_B; /* yes, add it in the proper pos. */ \
38 \
39 (w1) = __x3 + __ll_highpart (__x1); \
40 (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
41} while (0)
42#endif
43
44#if !defined (__umulsidi3)
45#define __umulsidi3(u, v) \
46 ({DIunion __w; \
47 umul_ppmm (__w.s.high, __w.s.low, u, v); \
48 __w.ll; })
49#endif
50
Aubrey.Li9da597f2007-03-09 13:38:44 +080051typedef unsigned int USItype __attribute__ ((mode(SI)));
52typedef int SItype __attribute__ ((mode(SI)));
53typedef int DItype __attribute__ ((mode(DI)));
54typedef int word_type __attribute__ ((mode(__word__)));
Wolfgang Denk97caf672006-03-12 02:12:27 +010055
Aubrey.Li9da597f2007-03-09 13:38:44 +080056struct DIstruct {
57 SItype low, high;
58};
59typedef union {
Wolfgang Denk97caf672006-03-12 02:12:27 +010060 struct DIstruct s;
61 DItype ll;
62} DIunion;
63
Aubrey.Li9da597f2007-03-09 13:38:44 +080064DItype __muldi3(DItype u, DItype v)
Wolfgang Denk97caf672006-03-12 02:12:27 +010065{
66 DIunion w;
67 DIunion uu, vv;
Wolfgang Denk2bad8682006-03-12 02:55:22 +010068
Aubrey.Li9da597f2007-03-09 13:38:44 +080069 uu.ll = u, vv.ll = v;
Wolfgang Denk97caf672006-03-12 02:12:27 +010070 /* panic("kernel panic for __muldi3"); */
Aubrey.Li9da597f2007-03-09 13:38:44 +080071 w.ll = __umulsidi3(uu.s.low, vv.s.low);
Wolfgang Denk97caf672006-03-12 02:12:27 +010072 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
Aubrey.Li9da597f2007-03-09 13:38:44 +080073 + (USItype) uu.s.high * (USItype) vv.s.low);
Wolfgang Denk2bad8682006-03-12 02:55:22 +010074
Wolfgang Denk97caf672006-03-12 02:12:27 +010075 return w.ll;
76}