Statistics
| Branch: | Revision:

root / host-utils.c @ c3d420ea

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 facd2857 Blue Swirl
#include <stdlib.h>
27 facd2857 Blue Swirl
#include <stdint.h>
28 084ed5cc j_mayer
#include "host-utils.h"
29 69d35728 ths
30 5592a750 ths
//#define DEBUG_MULDIV
31 5592a750 ths
32 e494ead5 ths
/* Long integer helpers */
33 7a51ad82 j_mayer
#if !defined(__x86_64__)
34 e494ead5 ths
static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
35 e494ead5 ths
{
36 e494ead5 ths
    *plow += a;
37 e494ead5 ths
    /* carry test */
38 e494ead5 ths
    if (*plow < a)
39 e494ead5 ths
        (*phigh)++;
40 e494ead5 ths
    *phigh += b;
41 e494ead5 ths
}
42 69d35728 ths
43 e494ead5 ths
static void neg128 (uint64_t *plow, uint64_t *phigh)
44 69d35728 ths
{
45 e494ead5 ths
    *plow = ~*plow;
46 e494ead5 ths
    *phigh = ~*phigh;
47 e494ead5 ths
    add128(plow, phigh, 1, 0);
48 e494ead5 ths
}
49 69d35728 ths
50 e494ead5 ths
static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
51 e494ead5 ths
{
52 e494ead5 ths
    uint32_t a0, a1, b0, b1;
53 e494ead5 ths
    uint64_t v;
54 69d35728 ths
55 e494ead5 ths
    a0 = a;
56 e494ead5 ths
    a1 = a >> 32;
57 69d35728 ths
58 e494ead5 ths
    b0 = b;
59 e494ead5 ths
    b1 = b >> 32;
60 e494ead5 ths
61 e494ead5 ths
    v = (uint64_t)a0 * (uint64_t)b0;
62 e494ead5 ths
    *plow = v;
63 e494ead5 ths
    *phigh = 0;
64 e494ead5 ths
65 e494ead5 ths
    v = (uint64_t)a0 * (uint64_t)b1;
66 e494ead5 ths
    add128(plow, phigh, v << 32, v >> 32);
67 e494ead5 ths
68 e494ead5 ths
    v = (uint64_t)a1 * (uint64_t)b0;
69 e494ead5 ths
    add128(plow, phigh, v << 32, v >> 32);
70 e494ead5 ths
71 e494ead5 ths
    v = (uint64_t)a1 * (uint64_t)b1;
72 e494ead5 ths
    *phigh += v;
73 69d35728 ths
}
74 69d35728 ths
75 69d35728 ths
/* Unsigned 64x64 -> 128 multiplication */
76 e494ead5 ths
void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
77 69d35728 ths
{
78 e494ead5 ths
    mul64(plow, phigh, a, b);
79 e494ead5 ths
#if defined(DEBUG_MULDIV)
80 e494ead5 ths
    printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
81 e494ead5 ths
           a, b, *phigh, *plow);
82 e494ead5 ths
#endif
83 e494ead5 ths
}
84 69d35728 ths
85 e494ead5 ths
/* Signed 64x64 -> 128 multiplication */
86 e494ead5 ths
void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
87 e494ead5 ths
{
88 e494ead5 ths
    int sa, sb;
89 69d35728 ths
90 e494ead5 ths
    sa = (a < 0);
91 e494ead5 ths
    if (sa)
92 e494ead5 ths
        a = -a;
93 e494ead5 ths
    sb = (b < 0);
94 e494ead5 ths
    if (sb)
95 e494ead5 ths
        b = -b;
96 e494ead5 ths
    mul64(plow, phigh, a, b);
97 e494ead5 ths
    if (sa ^ sb) {
98 e494ead5 ths
        neg128(plow, phigh);
99 e494ead5 ths
    }
100 e494ead5 ths
#if defined(DEBUG_MULDIV)
101 e494ead5 ths
    printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
102 e494ead5 ths
           a, b, *phigh, *plow);
103 69d35728 ths
#endif
104 69d35728 ths
}
105 7a51ad82 j_mayer
#endif /* !defined(__x86_64__) */