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