source: mainline/uspace/lib/softint/generic/multiplication.c@ a69cb9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a69cb9a was 28a3e74, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix comment style.

  • 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 softint
30 * @{
31 */
32/**
33 * @file
34 * SW implementation of 32 and 64 bit multiplication.
35 */
36
37#include <multiplication.h>
38#include <stdint.h>
39
40/** Set 1 to return INT64_MAX or INT64_MIN on overflow */
41#ifndef SOFTINT_CHECK_OF
42 #define SOFTINT_CHECK_OF 0
43#endif
44
45/** Multiply two integers and return long long as result.
46 *
47 * This function is overflow safe.
48 *
49 */
50static unsigned long long mul(unsigned int a, unsigned int b) {
51 unsigned int a1 = a >> 16;
52 unsigned int a2 = a & UINT16_MAX;
53 unsigned int b1 = b >> 16;
54 unsigned int b2 = b & UINT16_MAX;
55
56 unsigned long long t1 = a1 * b1;
57 unsigned long long t2 = a1 * b2;
58 t2 += a2 * b1;
59 unsigned long long t3 = a2 * b2;
60
61 t3 = (((t1 << 16) + t2) << 16) + t3;
62
63 return t3;
64}
65
66/** Emulate multiplication of two 64-bit long long integers.
67 *
68 */
69long long __muldi3 (long long a, long long b)
70{
71 char neg = 0;
72
73 if (a < 0) {
74 neg = !neg;
75 a = -a;
76 }
77
78 if (b < 0) {
79 neg = !neg;
80 b = -b;
81 }
82
83 unsigned long long a1 = a >> 32;
84 unsigned long long b1 = b >> 32;
85
86 unsigned long long a2 = a & (UINT32_MAX);
87 unsigned long long b2 = b & (UINT32_MAX);
88
89 if (SOFTINT_CHECK_OF && (a1 != 0) && (b1 != 0)) {
90 /* Error (overflow) */
91 return (neg ? INT64_MIN : INT64_MAX);
92 }
93
94 /* (if OF checked) a1 or b1 is zero => result fits in 64 bits,
95 * no need to another overflow check
96 */
97 unsigned long long t1 = mul(a1, b2) + mul(b1, a2);
98
99 if ((SOFTINT_CHECK_OF) && (t1 > UINT32_MAX)) {
100 /* Error (overflow) */
101 return (neg ? INT64_MIN : INT64_MAX);
102 }
103
104 t1 = t1 << 32;
105 unsigned long long t2 = mul(a2, b2);
106 t2 += t1;
107
108 /* t2 & (1ull << 63) - if this bit is set in unsigned long long,
109 * result does not fit in signed one */
110 if (SOFTINT_CHECK_OF && ((t2 < t1) || (t2 & (1ull << 63)))) {
111 /* Error, overflow */
112 return (neg ? INT64_MIN : INT64_MAX);
113 }
114
115 long long result = t2;
116 if (neg)
117 result = -result;
118
119 return result;
120}
121
122/** @}
123 */
Note: See TracBrowser for help on using the repository browser.