vaseboot/VasEBoot-core/lib/division.c

75 lines
1.8 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2015 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/misc.h>
#include <VasEBoot/dl.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static VasEBoot_uint64_t
abs64(VasEBoot_int64_t a)
{
return a > 0 ? a : -a;
}
VasEBoot_int64_t
VasEBoot_divmod64s (VasEBoot_int64_t n,
VasEBoot_int64_t d,
VasEBoot_int64_t *ro)
{
VasEBoot_uint64_t ru;
VasEBoot_int64_t q, r;
q = VasEBoot_divmod64 (abs64(n), abs64(d), &ru);
r = ru;
/* Now: |n| = |d| * q + r */
if (n < 0)
{
/* -|n| = |d| * (-q) + (-r) */
q = -q;
r = -r;
}
/* Now: n = |d| * q + r */
if (d < 0)
{
/* n = (-|d|) * (-q) + r */
q = -q;
}
/* Now: n = d * q + r */
if (ro)
*ro = r;
return q;
}
VasEBoot_uint32_t
VasEBoot_divmod32 (VasEBoot_uint32_t n, VasEBoot_uint32_t d, VasEBoot_uint32_t *ro)
{
VasEBoot_uint64_t q, r;
q = VasEBoot_divmod64 (n, d, &r);
*ro = r;
return q;
}
VasEBoot_int32_t
VasEBoot_divmod32s (VasEBoot_int32_t n, VasEBoot_int32_t d, VasEBoot_int32_t *ro)
{
VasEBoot_int64_t q, r;
q = VasEBoot_divmod64s (n, d, &r);
*ro = r;
return q;
}