Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 1 | /* |
Bin Meng | a187559 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 2 | * U-Boot - muldi3.c contains routines for mult and div |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 3 | * |
Aubrey Li | 155fd76 | 2007-04-05 18:31:18 +0800 | [diff] [blame] | 4 | * Copyright (c) 2005-2007 Analog Devices Inc. |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 7 | */ |
| 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) \ |
| 20 | do { \ |
| 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.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 51 | typedef unsigned int USItype __attribute__ ((mode(SI))); |
| 52 | typedef int SItype __attribute__ ((mode(SI))); |
| 53 | typedef int DItype __attribute__ ((mode(DI))); |
| 54 | typedef int word_type __attribute__ ((mode(__word__))); |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 55 | |
Aubrey.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 56 | struct DIstruct { |
| 57 | SItype low, high; |
| 58 | }; |
| 59 | typedef union { |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 60 | struct DIstruct s; |
| 61 | DItype ll; |
| 62 | } DIunion; |
| 63 | |
Aubrey.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 64 | DItype __muldi3(DItype u, DItype v) |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 65 | { |
| 66 | DIunion w; |
| 67 | DIunion uu, vv; |
Wolfgang Denk | 8e7b703 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 68 | |
Aubrey.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 69 | uu.ll = u, vv.ll = v; |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 70 | /* panic("kernel panic for __muldi3"); */ |
Aubrey.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 71 | w.ll = __umulsidi3(uu.s.low, vv.s.low); |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 72 | w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high |
Aubrey.Li | 3f0606a | 2007-03-09 13:38:44 +0800 | [diff] [blame] | 73 | + (USItype) uu.s.high * (USItype) vv.s.low); |
Wolfgang Denk | 8e7b703 | 2006-03-12 02:55:22 +0100 | [diff] [blame] | 74 | |
Wolfgang Denk | 6cb142f | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 75 | return w.ll; |
| 76 | } |