-/* Copyright (C) 1995,1996,1997,1998, 1999, 2000 Free Software Foundation, Inc.
-
+/* Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
+ *
* This program 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 2, or (at your option)
#include "unif.h"
#include "feature.h"
#include "ports.h"
+#include "root.h"
#include "smob.h"
+#include "strings.h"
#include "vectors.h"
#include "validate.h"
SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
(SCM n, SCM cnt),
- "Returns an integer equivalent to\n"
- "@code{(inexact->exact (floor (* @var{int} (expt 2 @var{count}))))}.@refill\n\n"
+ "The function ash performs an arithmetic shift left by CNT bits\n"
+ "(or shift right, if CNT is negative). 'Arithmetic' means, that\n"
+ "the function does not guarantee to keep the bit structure of N,\n"
+ "but rather guarantees that the result will always be rounded\n"
+ "towards minus infinity. Therefore, the results of ash and a\n"
+ "corresponding bitwise shift will differ if N is negative.\n\n"
+ "Formally, the function returns an integer equivalent to\n"
+ "@code{(inexact->exact (floor (* N (expt 2 CNT))))}.@refill\n\n"
"Example:\n"
"@lisp\n"
"(number->string (ash #b1 3) 2)\n"
"@end lisp")
#define FUNC_NAME s_scm_ash
{
- /* GJB:FIXME:: what is going on here? */
- SCM res = SCM_PACK (SCM_INUM (n));
- SCM_VALIDATE_INUM (2,cnt);
+ long bits_to_shift;
+
+#ifndef SCM_BIGDIG
+ SCM_VALIDATE_INUM (1, n)
+#endif
+ SCM_VALIDATE_INUM (2, cnt);
+
+ bits_to_shift = SCM_INUM (cnt);
#ifdef SCM_BIGDIG
- if (cnt < 0)
- {
- res = scm_integer_expt (SCM_MAKINUM (2), SCM_MAKINUM (-SCM_INUM (cnt)));
- if (SCM_NFALSEP (scm_negative_p (n)))
- return scm_sum (SCM_MAKINUM (-1L),
- scm_quotient (scm_sum (SCM_MAKINUM (1L), n), res));
- else
- return scm_quotient (n, res);
- }
- else
+ if (bits_to_shift < 0) {
+ /* Shift right by abs(cnt) bits. This is realized as a division by
+ div:=2^abs(cnt). However, to guarantee the floor rounding, negative
+ values require some special treatment.
+ */
+ SCM div = scm_integer_expt (SCM_MAKINUM (2), SCM_MAKINUM (-bits_to_shift));
+ if (SCM_FALSEP (scm_negative_p (n)))
+ return scm_quotient (n, div);
+ else
+ return scm_sum (SCM_MAKINUM (-1L),
+ scm_quotient (scm_sum (SCM_MAKINUM (1L), n), div));
+ } else
+ /* Shift left is done by multiplication with 2^CNT */
return scm_product (n, scm_integer_expt (SCM_MAKINUM (2), cnt));
#else
- SCM_VALIDATE_INUM (1,n)
- cnt = SCM_INUM (cnt);
- if (cnt < 0)
- return SCM_MAKINUM (SCM_SRS (res, -cnt));
- res = SCM_MAKINUM (res << cnt);
- if (SCM_INUM (res) >> cnt != SCM_INUM (n))
- scm_num_overflow (FUNC_NAME);
- return res;
+ if (bits_to_shift < 0)
+ /* Signed right shift (SCM_SRS does it right) by abs(cnt) bits. */
+ return SCM_MAKINUM (SCM_SRS (SCM_INUM (n), -bits_to_shift));
+ else {
+ /* Shift left, but make sure not to leave the range of inums */
+ SCM res = SCM_MAKINUM (SCM_INUM (n) << cnt);
+ if (SCM_INUM (res) >> cnt != SCM_INUM (n))
+ scm_num_overflow (FUNC_NAME);
+ return res;
+ }
#endif
}
#undef FUNC_NAME
{
intbig:
{
+ long i = SCM_INUM (x);
# ifndef SCM_DIGSTOOBIG
- long z = scm_pseudolong (SCM_INUM (x));
+ long z = scm_pseudolong (i);
return scm_addbig ((SCM_BIGDIG *) & z,
SCM_DIGSPERLONG,
- (x < 0) ? SCM_BIGSIGNFLAG : 0,
+ (i < 0) ? SCM_BIGSIGNFLAG : 0,
y, 0);
# else /* SCM_DIGSTOOBIG */
SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
- scm_longdigs (SCM_INUM (x), zdigs);
- return scm_addbig (zdigs, SCM_DIGSPERLONG, (x < 0) ? SCM_BIGSIGNFLAG : 0,
+ scm_longdigs (i, zdigs);
+ return scm_addbig (zdigs, SCM_DIGSPERLONG, (i < 0) ? SCM_BIGSIGNFLAG : 0,
y, 0);
# endif /* SCM_DIGSTOOBIG */
}
SCM_ASRTGO (SCM_NIMP (y), bady);
if (SCM_BIGP (y))
{
+ long i = SCM_INUM (x);
#ifndef SCM_DIGSTOOBIG
- long z = scm_pseudolong (SCM_INUM (x));
+ long z = scm_pseudolong (i);
return scm_addbig ((SCM_BIGDIG *) & z, SCM_DIGSPERLONG,
- (x < 0) ? SCM_BIGSIGNFLAG : 0,
+ (i < 0) ? SCM_BIGSIGNFLAG : 0,
y, SCM_BIGSIGNFLAG);
#else
SCM_BIGDIG zdigs[SCM_DIGSPERLONG];
- scm_longdigs (SCM_INUM (x), zdigs);
- return scm_addbig (zdigs, SCM_DIGSPERLONG, (x < 0) ? SCM_BIGSIGNFLAG : 0,
+ scm_longdigs (i, zdigs);
+ return scm_addbig (zdigs, SCM_DIGSPERLONG, (i < 0) ? SCM_BIGSIGNFLAG : 0,
y, SCM_BIGSIGNFLAG);
#endif
}
return scm_makdbl ((double) sl, 0.0);
#endif
}
- return SCM_MAKINUM (sl);
+ else
+ {
+ /* we know that sl fits into an inum */
+ return SCM_MAKINUM ((scm_bits_t) sl);
+ }
}
#endif
#endif /* DBL_DIG */
#include "numbers.x"
}
+
+/*
+ Local Variables:
+ c-file-style: "gnu"
+ End:
+*/