blob: 6459af5b2af0a9c312c8c04d1dfc2009e24c30a6 [file] [log] [blame]
Greg Ungerer98d655a2011-03-28 15:58:06 +10001/* muldi3.c extracted from gcc-2.7.2.3/libgcc2.c and
2 gcc-2.7.2.3/longlong.h which is: */
3/* Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Geert Uytterhoevend6410ef2014-11-08 19:19:07 +010015GNU General Public License for more details. */
Greg Ungerer98d655a2011-03-28 15:58:06 +100016
Al Virod13ffb52016-01-11 13:04:04 -050017#include <linux/compiler.h>
18#include <linux/export.h>
19
Geert Uytterhoeven5df58f32012-06-06 18:35:13 +020020#ifdef CONFIG_CPU_HAS_NO_MULDIV64
Greg Ungerer98d655a2011-03-28 15:58:06 +100021
22#define SI_TYPE_SIZE 32
23#define __BITS4 (SI_TYPE_SIZE / 4)
24#define __ll_B (1L << (SI_TYPE_SIZE / 2))
25#define __ll_lowpart(t) ((USItype) (t) % __ll_B)
26#define __ll_highpart(t) ((USItype) (t) / __ll_B)
27
28#define umul_ppmm(w1, w0, u, v) \
29 do { \
30 USItype __x0, __x1, __x2, __x3; \
31 USItype __ul, __vl, __uh, __vh; \
32 \
33 __ul = __ll_lowpart (u); \
34 __uh = __ll_highpart (u); \
35 __vl = __ll_lowpart (v); \
36 __vh = __ll_highpart (v); \
37 \
38 __x0 = (USItype) __ul * __vl; \
39 __x1 = (USItype) __ul * __vh; \
40 __x2 = (USItype) __uh * __vl; \
41 __x3 = (USItype) __uh * __vh; \
42 \
43 __x1 += __ll_highpart (__x0);/* this can't give carry */ \
44 __x1 += __x2; /* but this indeed can */ \
45 if (__x1 < __x2) /* did we get it? */ \
46 __x3 += __ll_B; /* yes, add it in the proper pos. */ \
47 \
48 (w1) = __x3 + __ll_highpart (__x1); \
49 (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
50 } while (0)
51
Greg Ungerer734c3ce2011-06-02 16:07:33 +100052#else
53
54#define umul_ppmm(w1, w0, u, v) \
55 __asm__ ("mulu%.l %3,%1:%0" \
56 : "=d" ((USItype)(w0)), \
57 "=d" ((USItype)(w1)) \
58 : "%0" ((USItype)(u)), \
59 "dmi" ((USItype)(v)))
60
Greg Ungerer66d857b2011-03-22 13:39:27 +100061#endif
Greg Ungerer98d655a2011-03-28 15:58:06 +100062
63#define __umulsidi3(u, v) \
64 ({DIunion __w; \
65 umul_ppmm (__w.s.high, __w.s.low, u, v); \
66 __w.ll; })
67
68typedef int SItype __attribute__ ((mode (SI)));
69typedef unsigned int USItype __attribute__ ((mode (SI)));
70typedef int DItype __attribute__ ((mode (DI)));
71typedef int word_type __attribute__ ((mode (__word__)));
72
73struct DIstruct {SItype high, low;};
74
75typedef union
76{
77 struct DIstruct s;
78 DItype ll;
79} DIunion;
80
81DItype
82__muldi3 (DItype u, DItype v)
83{
84 DIunion w;
85 DIunion uu, vv;
86
87 uu.ll = u,
88 vv.ll = v;
89
90 w.ll = __umulsidi3 (uu.s.low, vv.s.low);
91 w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
92 + (USItype) uu.s.high * (USItype) vv.s.low);
93
94 return w.ll;
95}
Al Virod13ffb52016-01-11 13:04:04 -050096EXPORT_SYMBOL(__muldi3);