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