1990-07-16 09:05:19 +00:00
|
|
|
/*
|
|
|
|
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Module: implementation of DIV and MOD
|
|
|
|
Author: Ceriel J.H. Jacobs
|
1994-06-24 14:02:31 +00:00
|
|
|
Version: $Id$
|
1990-07-16 09:05:19 +00:00
|
|
|
Reason: We cannot use DVI and RMI, because DVI rounds towards 0
|
2018-06-25 20:22:37 +00:00
|
|
|
and Modula-2 requires truncation
|
1990-07-16 09:05:19 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
#include "libm2.h"
|
1990-07-16 09:05:19 +00:00
|
|
|
#include <em_abs.h>
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
int dvi(int j, int i)
|
1990-07-16 09:05:19 +00:00
|
|
|
{
|
2018-06-25 20:22:37 +00:00
|
|
|
if (j == 0)
|
|
|
|
TRP(EIDIVZ);
|
|
|
|
if ((i < 0) != (j < 0))
|
|
|
|
{
|
|
|
|
if (i < 0)
|
|
|
|
i = -i;
|
|
|
|
else
|
|
|
|
j = -j;
|
|
|
|
return -((i + j - 1) / j);
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|
2018-06-25 20:22:37 +00:00
|
|
|
else
|
|
|
|
return i / j;
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
long dvil(long j, long i)
|
1990-07-16 09:05:19 +00:00
|
|
|
{
|
2018-06-25 20:22:37 +00:00
|
|
|
if (j == 0)
|
|
|
|
TRP(EIDIVZ);
|
|
|
|
if ((i < 0) != (j < 0))
|
|
|
|
{
|
|
|
|
if (i < 0)
|
|
|
|
i = -i;
|
|
|
|
else
|
|
|
|
j = -j;
|
|
|
|
return -((i + j - 1) / j);
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|
2018-06-25 20:22:37 +00:00
|
|
|
else
|
|
|
|
return i / j;
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
int rmi(int j, int i)
|
1990-07-16 09:05:19 +00:00
|
|
|
{
|
2017-10-28 23:55:06 +00:00
|
|
|
int m;
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
if (j == 0)
|
|
|
|
TRP(EIDIVZ);
|
|
|
|
if (i == 0)
|
|
|
|
return 0;
|
2017-10-28 23:55:06 +00:00
|
|
|
|
|
|
|
m = i % j;
|
|
|
|
if (m != 0 && (i < 0) != (j < 0))
|
|
|
|
m += j;
|
|
|
|
return m;
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
long rmil(long j, long i)
|
1990-07-16 09:05:19 +00:00
|
|
|
{
|
2017-10-28 23:55:06 +00:00
|
|
|
long m;
|
|
|
|
|
2018-06-25 20:22:37 +00:00
|
|
|
if (j == 0)
|
|
|
|
TRP(EIDIVZ);
|
|
|
|
if (i == 0)
|
|
|
|
return 0L;
|
2017-10-28 23:55:06 +00:00
|
|
|
|
|
|
|
m = i % j;
|
|
|
|
if (m != 0 && (i < 0) != (j < 0))
|
|
|
|
m += j;
|
|
|
|
return m;
|
1990-07-16 09:05:19 +00:00
|
|
|
}
|