root / host-utils.c @ 94cff60a
History | View | Annotate | Download (2.4 kB)
1 | 69d35728 | ths | /*
|
---|---|---|---|
2 | 69d35728 | ths | * Utility compute operations used by translated code.
|
3 | 69d35728 | ths | *
|
4 | 69d35728 | ths | * Copyright (c) 2007 Aurelien Jarno
|
5 | 69d35728 | ths | *
|
6 | 69d35728 | ths | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 69d35728 | ths | * of this software and associated documentation files (the "Software"), to deal
|
8 | 69d35728 | ths | * in the Software without restriction, including without limitation the rights
|
9 | 69d35728 | ths | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 69d35728 | ths | * copies of the Software, and to permit persons to whom the Software is
|
11 | 69d35728 | ths | * furnished to do so, subject to the following conditions:
|
12 | 69d35728 | ths | *
|
13 | 69d35728 | ths | * The above copyright notice and this permission notice shall be included in
|
14 | 69d35728 | ths | * all copies or substantial portions of the Software.
|
15 | 69d35728 | ths | *
|
16 | 69d35728 | ths | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 69d35728 | ths | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 69d35728 | ths | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 69d35728 | ths | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 69d35728 | ths | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 69d35728 | ths | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 69d35728 | ths | * THE SOFTWARE.
|
23 | 69d35728 | ths | */
|
24 | 69d35728 | ths | |
25 | 69d35728 | ths | #include "vl.h" |
26 | 69d35728 | ths | |
27 | 69d35728 | ths | /* Signed 64x64 -> 128 multiplication */
|
28 | 69d35728 | ths | |
29 | 69d35728 | ths | void muls64(int64_t *phigh, int64_t *plow, int64_t a, int64_t b)
|
30 | 69d35728 | ths | { |
31 | 69d35728 | ths | #if defined(__x86_64__)
|
32 | 69d35728 | ths | __asm__ ("imul %0\n\t"
|
33 | 69d35728 | ths | : "=d" (*phigh), "=a" (*plow) |
34 | 69d35728 | ths | : "a" (a), "0" (b) |
35 | 69d35728 | ths | ); |
36 | 69d35728 | ths | #else
|
37 | 69d35728 | ths | int64_t ph; |
38 | 69d35728 | ths | uint64_t pm1, pm2, pl; |
39 | 69d35728 | ths | |
40 | 69d35728 | ths | pl = (uint64_t)((uint32_t)a) * (uint64_t)((uint32_t)b); |
41 | 69d35728 | ths | pm1 = (a >> 32) * (uint32_t)b;
|
42 | 69d35728 | ths | pm2 = (uint32_t)a * (b >> 32);
|
43 | 69d35728 | ths | ph = (a >> 32) * (b >> 32); |
44 | 69d35728 | ths | |
45 | 69d35728 | ths | ph += (int64_t)pm1 >> 32;
|
46 | 69d35728 | ths | pm1 = (uint64_t)((uint32_t)pm1) + pm2 + (pl >> 32);
|
47 | 69d35728 | ths | |
48 | 69d35728 | ths | *phigh = ph + ((int64_t)pm1 >> 32);
|
49 | 69d35728 | ths | *plow = (pm1 << 32) + (uint32_t)pl;
|
50 | 69d35728 | ths | #endif
|
51 | 69d35728 | ths | } |
52 | 69d35728 | ths | |
53 | 69d35728 | ths | /* Unsigned 64x64 -> 128 multiplication */
|
54 | 69d35728 | ths | void mulu64(uint64_t *phigh, uint64_t *plow, uint64_t a, uint64_t b)
|
55 | 69d35728 | ths | { |
56 | 69d35728 | ths | #if defined(__x86_64__)
|
57 | 69d35728 | ths | __asm__ ("mul %0\n\t"
|
58 | 69d35728 | ths | : "=d" (*phigh), "=a" (*plow) |
59 | 69d35728 | ths | : "a" (a), "0" (b) |
60 | 69d35728 | ths | ); |
61 | 69d35728 | ths | #else
|
62 | 69d35728 | ths | uint64_t ph, pm1, pm2, pl; |
63 | 69d35728 | ths | |
64 | 69d35728 | ths | pl = (uint64_t)((uint32_t)a) * (uint64_t)((uint32_t)b); |
65 | 69d35728 | ths | pm1 = (a >> 32) * (uint32_t)b;
|
66 | 69d35728 | ths | pm2 = (uint32_t)a * (b >> 32);
|
67 | 69d35728 | ths | ph = (a >> 32) * (b >> 32); |
68 | 69d35728 | ths | |
69 | 69d35728 | ths | ph += pm1 >> 32;
|
70 | 69d35728 | ths | pm1 = (uint64_t)((uint32_t)pm1) + pm2 + (pl >> 32);
|
71 | 69d35728 | ths | |
72 | 69d35728 | ths | *phigh = ph + (pm1 >> 32);
|
73 | 69d35728 | ths | *plow = (pm1 << 32) + (uint32_t)pl;
|
74 | 69d35728 | ths | #endif
|
75 | 69d35728 | ths | } |