1 | /*
|
---|
2 | * Utility compute operations used by translated code.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2003 Fabrice Bellard
|
---|
5 | * Copyright (c) 2007 Aurelien Jarno
|
---|
6 | *
|
---|
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
8 | * of this software and associated documentation files (the "Software"), to deal
|
---|
9 | * in the Software without restriction, including without limitation the rights
|
---|
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
11 | * copies of the Software, and to permit persons to whom the Software is
|
---|
12 | * furnished to do so, subject to the following conditions:
|
---|
13 | *
|
---|
14 | * The above copyright notice and this permission notice shall be included in
|
---|
15 | * all copies or substantial portions of the Software.
|
---|
16 | *
|
---|
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
23 | * THE SOFTWARE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <stdlib.h>
|
---|
27 | #ifndef VBOX
|
---|
28 | #include <stdint.h>
|
---|
29 | #else
|
---|
30 | # include <iprt/types.h>
|
---|
31 | #endif
|
---|
32 | #include "host-utils.h"
|
---|
33 |
|
---|
34 | //#define DEBUG_MULDIV
|
---|
35 |
|
---|
36 | /* Long integer helpers */
|
---|
37 | #if !defined(__x86_64__)
|
---|
38 | static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
---|
39 | {
|
---|
40 | *plow += a;
|
---|
41 | /* carry test */
|
---|
42 | if (*plow < a)
|
---|
43 | (*phigh)++;
|
---|
44 | *phigh += b;
|
---|
45 | }
|
---|
46 |
|
---|
47 | static void neg128 (uint64_t *plow, uint64_t *phigh)
|
---|
48 | {
|
---|
49 | *plow = ~*plow;
|
---|
50 | *phigh = ~*phigh;
|
---|
51 | add128(plow, phigh, 1, 0);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
---|
55 | {
|
---|
56 | uint32_t a0, a1, b0, b1;
|
---|
57 | uint64_t v;
|
---|
58 |
|
---|
59 | a0 = a;
|
---|
60 | a1 = a >> 32;
|
---|
61 |
|
---|
62 | b0 = b;
|
---|
63 | b1 = b >> 32;
|
---|
64 |
|
---|
65 | v = (uint64_t)a0 * (uint64_t)b0;
|
---|
66 | *plow = v;
|
---|
67 | *phigh = 0;
|
---|
68 |
|
---|
69 | v = (uint64_t)a0 * (uint64_t)b1;
|
---|
70 | add128(plow, phigh, v << 32, v >> 32);
|
---|
71 |
|
---|
72 | v = (uint64_t)a1 * (uint64_t)b0;
|
---|
73 | add128(plow, phigh, v << 32, v >> 32);
|
---|
74 |
|
---|
75 | v = (uint64_t)a1 * (uint64_t)b1;
|
---|
76 | *phigh += v;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Unsigned 64x64 -> 128 multiplication */
|
---|
80 | void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
---|
81 | {
|
---|
82 | mul64(plow, phigh, a, b);
|
---|
83 | #if defined(DEBUG_MULDIV)
|
---|
84 | printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
|
---|
85 | a, b, *phigh, *plow);
|
---|
86 | #endif
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* Signed 64x64 -> 128 multiplication */
|
---|
90 | void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
|
---|
91 | {
|
---|
92 | int sa, sb;
|
---|
93 |
|
---|
94 | sa = (a < 0);
|
---|
95 | if (sa)
|
---|
96 | a = -a;
|
---|
97 | sb = (b < 0);
|
---|
98 | if (sb)
|
---|
99 | b = -b;
|
---|
100 | mul64(plow, phigh, a, b);
|
---|
101 | if (sa ^ sb) {
|
---|
102 | neg128(plow, phigh);
|
---|
103 | }
|
---|
104 | #if defined(DEBUG_MULDIV)
|
---|
105 | printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
|
---|
106 | a, b, *phigh, *plow);
|
---|
107 | #endif
|
---|
108 | }
|
---|
109 | #endif /* !defined(__x86_64__) */
|
---|