source: mainline/kernel/genarch/src/softint/multiplication.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was d27ed12, checked in by anachron <anachron@…>, 16 years ago

Added software emulation of 64bit multiplication (muldi3).

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Copyright (c) 2009 Josef Cejka
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genarch
30 * @{
31 */
32/**
33 * @file
34 * SW implementation of 32 and 64 bit multiplication.
35 */
36
37#include <genarch/softint/multiplication.h>
38
39/** Set 1 to return MAX_INT64 or MIN_INT64 on overflow */
40#ifndef SOFTINT_CHECK_OF
41# define SOFTINT_CHECK_OF 0
42#endif
43
44#define MAX_UINT16 (0xFFFFu)
45#define MAX_UINT32 (0xFFFFFFFFu)
46#define MAX_INT64 (0x7FFFFFFFFFFFFFFFll)
47#define MIN_INT64 (0x8000000000000000ll)
48
49/**
50 * Multiply two integers and return long long as result.
51 * This function is overflow safe.
52 * @param a
53 * @param b
54 * @result
55 */
56static unsigned long long mul(unsigned int a, unsigned int b) {
57 unsigned int a1, a2, b1, b2;
58 unsigned long long t1, t2, t3;
59
60 a1 = a >> 16;
61 a2 = a & MAX_UINT16;
62 b1 = b >> 16;
63 b2 = b & MAX_UINT16;
64
65 t1 = a1 * b1;
66 t2 = a1*b2;
67 t2 += a2*b1;
68 t3 = a2*b2;
69
70 t3 = (((t1 << 16) + t2) << 16) + t3;
71
72 return t3;
73}
74
75/** Emulate multiplication of two 64-bit long long integers.
76 *
77 */
78long long __muldi3 (long long a, long long b)
79{
80 long long result;
81 unsigned long long t1,t2;
82 unsigned long long a1, a2, b1, b2;
83 char neg = 0;
84
85 if (a < 0) {
86 neg = !neg;
87 a = -a;
88 }
89
90 if (b < 0) {
91 neg = !neg;
92 b = -b;
93 }
94
95 a1 = a >> 32;
96 b1 = b >> 32;
97
98 a2 = a & (MAX_UINT32);
99 b2 = b & (MAX_UINT32);
100
101 if (SOFTINT_CHECK_OF && (a1 != 0) && (b1 != 0)) {
102 // error, overflow
103 return (neg?MIN_INT64:MAX_INT64);
104 }
105
106 // (if OF checked) a1 or b1 is zero => result fits in 64 bits, no need to another overflow check
107 t1 = mul(a1,b2) + mul(b1,a2);
108
109 if (SOFTINT_CHECK_OF && t1 > MAX_UINT32) {
110 // error, overflow
111 return (neg?MIN_INT64:MAX_INT64);
112 }
113
114 t1 = t1 << 32;
115 t2 = mul(a2,b2);
116 t2 += t1;
117
118 /* t2 & (1ull << 63) - if this bit is set in unsigned long long,
119 * result does not fit in signed one */
120 if (SOFTINT_CHECK_OF && ((t2 < t1) || (t2 & (1ull << 63)))) {
121 // error, overflow
122 return (neg?MIN_INT64:MAX_INT64);
123 }
124
125 result = t2;
126
127 if (neg) {
128 result = -result;
129 }
130
131 return result;
132}
133
134/** @}
135 */
Note: See TracBrowser for help on using the repository browser.