2019-06-13 23:57:43 +01:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
|
|
|
* GNU Mes --- Maxwell Equations of Software
|
|
|
|
* Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
* Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
|
|
|
|
*
|
|
|
|
* This file is part of GNU Mes.
|
|
|
|
*
|
|
|
|
* GNU Mes 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.
|
|
|
|
*
|
|
|
|
* GNU Mes 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mes/lib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <limits.h>
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
#include <signal.h>
|
2019-06-13 23:57:43 +01:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
long quot;
|
|
|
|
long rem;
|
|
|
|
} ldiv_t;
|
|
|
|
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
int __raise(int);
|
|
|
|
|
|
|
|
void
|
|
|
|
__mesabi_div0 (void)
|
2019-06-13 23:57:43 +01:00
|
|
|
{
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
if (__raise(SIGFPE) < 0) { /* could not raise SIGFPE */
|
|
|
|
/* Fail in any way possible */
|
|
|
|
unsigned char* x = (unsigned char*) 0;
|
|
|
|
*x = 2;
|
|
|
|
}
|
2019-06-13 23:57:43 +01:00
|
|
|
}
|
|
|
|
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
/* Compare gcc: __udivmoddi4 */
|
|
|
|
unsigned long
|
|
|
|
__mesabi_uldiv (unsigned long a, unsigned long b, unsigned long* remainder)
|
2019-06-13 23:57:43 +01:00
|
|
|
{
|
|
|
|
unsigned long tmp;
|
|
|
|
if (!remainder)
|
|
|
|
remainder = &tmp;
|
|
|
|
*remainder = 0;
|
|
|
|
if (b == 1)
|
|
|
|
return a;
|
|
|
|
else if (b == 0)
|
|
|
|
__mesabi_div0();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned long x;
|
|
|
|
for (x = 0; a >= b; a -= b)
|
|
|
|
++x;
|
|
|
|
*remainder = a;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 22:02:19 +01:00
|
|
|
/* Note: Rounds towards zero.
|
|
|
|
Maintainer: Be careful to satisfy quot * b + rem == a.
|
|
|
|
That means that rem can be negative. */
|
2020-06-10 12:16:08 +01:00
|
|
|
void
|
|
|
|
__mesabi_ldiv(long a, long b, ldiv_t* result)
|
2019-06-13 23:57:43 +01:00
|
|
|
{
|
2019-07-01 21:14:11 +01:00
|
|
|
int negate_result = (a < 0) ^ (b < 0);
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
if (b == LONG_MIN)
|
|
|
|
__mesabi_div0();
|
2019-07-01 21:14:11 +01:00
|
|
|
if (a != LONG_MIN)
|
|
|
|
{
|
2019-07-01 22:02:19 +01:00
|
|
|
int negative_a = (a < 0);
|
|
|
|
if (negative_a)
|
2019-07-01 21:14:11 +01:00
|
|
|
a = -a;
|
|
|
|
if (b < 0)
|
|
|
|
b = -b;
|
2020-06-10 12:16:08 +01:00
|
|
|
result->quot = __mesabi_uldiv(a, b, &result->rem);
|
2019-07-01 21:14:11 +01:00
|
|
|
if (negate_result)
|
2020-06-10 12:16:08 +01:00
|
|
|
result->quot = -result->quot;
|
2019-07-01 22:02:19 +01:00
|
|
|
if (negative_a)
|
2020-06-10 12:16:08 +01:00
|
|
|
result->rem = -result->rem;
|
2019-07-01 21:14:11 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-10 12:16:08 +01:00
|
|
|
result->rem = 0;
|
2019-07-01 21:14:11 +01:00
|
|
|
if (b < 0)
|
|
|
|
b = -b;
|
|
|
|
if (b == 1)
|
|
|
|
{
|
2020-06-10 12:16:08 +01:00
|
|
|
result->quot = a;
|
|
|
|
/* Since result->quot is already negative, don't negate it again. */
|
2019-07-01 21:14:11 +01:00
|
|
|
negate_result = !negate_result;
|
|
|
|
}
|
|
|
|
else if (b == 0)
|
|
|
|
__mesabi_div0();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long x;
|
|
|
|
for (x = 0; a <= -b; a += b)
|
|
|
|
++x;
|
2020-06-10 12:16:08 +01:00
|
|
|
result->rem = a; /* negative */
|
|
|
|
result->quot = x;
|
2019-07-01 21:14:11 +01:00
|
|
|
}
|
|
|
|
if (negate_result)
|
2020-06-10 12:16:08 +01:00
|
|
|
result->quot = -result->quot;
|
2019-07-01 21:14:11 +01:00
|
|
|
}
|
2019-06-13 23:57:43 +01:00
|
|
|
}
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 22:47:49 +01:00
|
|
|
|
2020-06-10 12:16:08 +01:00
|
|
|
long
|
|
|
|
__mesabi_imod (long a, long b)
|
|
|
|
{
|
|
|
|
ldiv_t result;
|
|
|
|
__mesabi_ldiv(a, b, &result);
|
|
|
|
return result.rem;
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:08:57 +01:00
|
|
|
int
|
|
|
|
__mesabi_idiv (int a, int b)
|
2020-06-10 12:16:08 +01:00
|
|
|
{
|
|
|
|
ldiv_t result;
|
|
|
|
__mesabi_ldiv(a, b, &result);
|
|
|
|
return result.quot;
|
|
|
|
}
|