2007-05-16 19:59:40 +08:00
|
|
|
/*
|
|
|
|
* Utility compute operations used by translated code.
|
|
|
|
*
|
2007-10-26 07:00:03 +08:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
2007-05-16 19:59:40 +08:00
|
|
|
* Copyright (c) 2007 Aurelien Jarno
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vl.h"
|
|
|
|
|
2007-10-27 06:35:02 +08:00
|
|
|
//#define DEBUG_MULDIV
|
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
/* Long integer helpers */
|
|
|
|
static void add128 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
*plow += a;
|
|
|
|
/* carry test */
|
|
|
|
if (*plow < a)
|
|
|
|
(*phigh)++;
|
|
|
|
*phigh += b;
|
|
|
|
}
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
static void neg128 (uint64_t *plow, uint64_t *phigh)
|
2007-05-16 19:59:40 +08:00
|
|
|
{
|
2007-10-26 07:00:03 +08:00
|
|
|
*plow = ~*plow;
|
|
|
|
*phigh = ~*phigh;
|
|
|
|
add128(plow, phigh, 1, 0);
|
|
|
|
}
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
static void mul64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
uint32_t a0, a1, b0, b1;
|
|
|
|
uint64_t v;
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
a0 = a;
|
|
|
|
a1 = a >> 32;
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
b0 = b;
|
|
|
|
b1 = b >> 32;
|
|
|
|
|
|
|
|
v = (uint64_t)a0 * (uint64_t)b0;
|
|
|
|
*plow = v;
|
|
|
|
*phigh = 0;
|
|
|
|
|
|
|
|
v = (uint64_t)a0 * (uint64_t)b1;
|
|
|
|
add128(plow, phigh, v << 32, v >> 32);
|
|
|
|
|
|
|
|
v = (uint64_t)a1 * (uint64_t)b0;
|
|
|
|
add128(plow, phigh, v << 32, v >> 32);
|
|
|
|
|
|
|
|
v = (uint64_t)a1 * (uint64_t)b1;
|
|
|
|
*phigh += v;
|
2007-05-16 19:59:40 +08:00
|
|
|
}
|
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
|
2007-05-16 19:59:40 +08:00
|
|
|
/* Unsigned 64x64 -> 128 multiplication */
|
2007-10-26 07:00:03 +08:00
|
|
|
void mulu64 (uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
|
2007-05-16 19:59:40 +08:00
|
|
|
{
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
__asm__ ("mul %0\n\t"
|
|
|
|
: "=d" (*phigh), "=a" (*plow)
|
2007-10-26 07:00:03 +08:00
|
|
|
: "a" (a), "0" (b));
|
2007-05-16 19:59:40 +08:00
|
|
|
#else
|
2007-10-26 07:00:03 +08:00
|
|
|
mul64(plow, phigh, a, b);
|
|
|
|
#endif
|
|
|
|
#if defined(DEBUG_MULDIV)
|
|
|
|
printf("mulu64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
|
|
|
|
a, b, *phigh, *plow);
|
|
|
|
#endif
|
|
|
|
}
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
/* Signed 64x64 -> 128 multiplication */
|
|
|
|
void muls64 (uint64_t *plow, uint64_t *phigh, int64_t a, int64_t b)
|
|
|
|
{
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
__asm__ ("imul %0\n\t"
|
|
|
|
: "=d" (*phigh), "=a" (*plow)
|
|
|
|
: "a" (a), "0" (b));
|
|
|
|
#else
|
|
|
|
int sa, sb;
|
2007-05-16 19:59:40 +08:00
|
|
|
|
2007-10-26 07:00:03 +08:00
|
|
|
sa = (a < 0);
|
|
|
|
if (sa)
|
|
|
|
a = -a;
|
|
|
|
sb = (b < 0);
|
|
|
|
if (sb)
|
|
|
|
b = -b;
|
|
|
|
mul64(plow, phigh, a, b);
|
|
|
|
if (sa ^ sb) {
|
|
|
|
neg128(plow, phigh);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(DEBUG_MULDIV)
|
|
|
|
printf("muls64: 0x%016llx * 0x%016llx = 0x%016llx%016llx\n",
|
|
|
|
a, b, *phigh, *plow);
|
2007-05-16 19:59:40 +08:00
|
|
|
#endif
|
|
|
|
}
|