source: mainline/boot/genarch/src/multiplication.c@ 84239b1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 84239b1 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

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