David Holsgrove | 2281ba5 | 2012-11-06 23:01:24 +1000 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - muldi3.c contains routines for mult and div |
| 3 | * |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 21 | * MA 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | /* Generic function got from GNU gcc package, libgcc2.c */ |
| 25 | #ifndef SI_TYPE_SIZE |
| 26 | #define SI_TYPE_SIZE 32 |
| 27 | #endif |
| 28 | #define __ll_B (1L << (SI_TYPE_SIZE / 2)) |
| 29 | #define __ll_lowpart(t) ((USItype) (t) % __ll_B) |
| 30 | #define __ll_highpart(t) ((USItype) (t) / __ll_B) |
| 31 | #define BITS_PER_UNIT 8 |
| 32 | |
| 33 | #if !defined(umul_ppmm) |
| 34 | #define umul_ppmm(w1, w0, u, v) \ |
| 35 | do { \ |
| 36 | USItype __x0, __x1, __x2, __x3; \ |
| 37 | USItype __ul, __vl, __uh, __vh; \ |
| 38 | \ |
| 39 | __ul = __ll_lowpart(u); \ |
| 40 | __uh = __ll_highpart(u); \ |
| 41 | __vl = __ll_lowpart(v); \ |
| 42 | __vh = __ll_highpart(v); \ |
| 43 | \ |
| 44 | __x0 = (USItype) __ul * __vl; \ |
| 45 | __x1 = (USItype) __ul * __vh; \ |
| 46 | __x2 = (USItype) __uh * __vl; \ |
| 47 | __x3 = (USItype) __uh * __vh; \ |
| 48 | \ |
| 49 | __x1 += __ll_highpart(__x0); /* this can't give carry */\ |
| 50 | __x1 += __x2; /* but this indeed can */ \ |
| 51 | if (__x1 < __x2) /* did we get it? */ \ |
| 52 | __x3 += __ll_B; /* yes, add it in the proper pos. */ \ |
| 53 | \ |
| 54 | (w1) = __x3 + __ll_highpart(__x1); \ |
| 55 | (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\ |
| 56 | } while (0) |
| 57 | #endif |
| 58 | |
| 59 | #if !defined(__umulsidi3) |
| 60 | #define __umulsidi3(u, v) \ |
| 61 | ({DIunion __w; \ |
| 62 | umul_ppmm(__w.s.high, __w.s.low, u, v); \ |
| 63 | __w.ll; }) |
| 64 | #endif |
| 65 | |
| 66 | typedef unsigned int USItype __attribute__ ((mode(SI))); |
| 67 | typedef int SItype __attribute__ ((mode(SI))); |
| 68 | typedef int DItype __attribute__ ((mode(DI))); |
| 69 | typedef int word_type __attribute__ ((mode(__word__))); |
| 70 | |
| 71 | struct DIstruct { |
| 72 | SItype low, high; |
| 73 | }; |
| 74 | typedef union { |
| 75 | struct DIstruct s; |
| 76 | DItype ll; |
| 77 | } DIunion; |
| 78 | |
| 79 | DItype __muldi3(DItype u, DItype v) |
| 80 | { |
| 81 | DIunion w; |
| 82 | DIunion uu, vv; |
| 83 | |
| 84 | uu.ll = u, vv.ll = v; |
| 85 | /* panic("kernel panic for __muldi3"); */ |
| 86 | w.ll = __umulsidi3(uu.s.low, vv.s.low); |
| 87 | w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high |
| 88 | + (USItype) uu.s.high * (USItype) vv.s.low); |
| 89 | |
| 90 | return w.ll; |
| 91 | } |