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