Skip to content

Commit

Permalink
repair fx-/wraparound with 0 first argument, allow one argument (#…
Browse files Browse the repository at this point in the history
…899)

The `fx-/wraparound` function was treated like R6RS `fx-` by cp0,
which meant that it could reduce a 2-argument call with `0` as the
first argument to a 1-argument call, even though `fx-/wraparound` was
defined with only a 2-argument form. Making `fx-/wraparound` more like
R6RS `fx-` seems like the path of least resistance and least
likelihood of future bugs.
  • Loading branch information
mflatt authored Jan 3, 2025
1 parent bf70250 commit 003a1bf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
9 changes: 5 additions & 4 deletions csug/numeric.stex
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,11 @@ dividing \var{fixnum_1} by the product of the remaining arguments

%----------------------------------------------------------------------------
\entryheader
\formdef{fx+/wraparound}{\categoryprocedure}{(fx+/wraparound \var{fixnum} \dots)}
\formdef{fx-/wraparound}{\categoryprocedure}{(fx-/wraparound \var{fixnum} \dots)}
\formdef{fx*/wraparound}{\categoryprocedure}{(fx*/wraparound \var{fixnum} \dots)}
\formdef{fxsll/wraparound}{\categoryprocedure}{(fxsll/wraparound \var{fixnum} \dots)}
\formdef{fx+/wraparound}{\categoryprocedure}{(fx+/wraparound \var{fixnum} \var{fixnum})}
\formdef{fx-/wraparound}{\categoryprocedure}{(fx-/wraparound \var{fixnum} \var{fixnum})}
\formdef{fx-/wraparound}{\categoryprocedure}{(fx-/wraparound \var{fixnum})}
\formdef{fx*/wraparound}{\categoryprocedure}{(fx*/wraparound \var{fixnum} \var{fixnum})}
\formdef{fxsll/wraparound}{\categoryprocedure}{(fxsll/wraparound \var{fixnum} \var{fixnum})}
\returns the arithmetic result, wrapping on overflow
\listlibraries
\endentryheader
Expand Down
2 changes: 2 additions & 0 deletions mats/fx.ms
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,9 @@
(mat fx-/wraparound
(eqv? (fx-/wraparound 3 0) 3)
(eqv? (fx-/wraparound 3 1) 2)
(eqv? (fx-/wraparound 0 3) -3)
(eqv? (fx-/wraparound -3 4) -7)
(eqv? (fx-/wraparound 3) -3)
(error? (fx-/wraparound '(a . b) 0))
(error? (fx- (add1 (most-positive-fixnum)) 1))
(error? (fx- 1 (add1 (most-positive-fixnum))))
Expand Down
13 changes: 13 additions & 0 deletions release_notes/release_notes.stex
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ Online versions of both books can be found at
The type recovery pass has improved support for \scheme{abs} with a fixnum argument
and added support for \scheme{1+}, \scheme{1-}, and \scheme{-1+}.

\subsection{Single-argument \scheme{fx-/wraparound} (10.2.0)}

The \scheme{fx-/wraparound} function changed to accept a single
argument, which makes it more consistent with the R6RS \scheme{fx-}
function.

\subsection{Constrain signal delivery to the main thread (10.1.0)}

Signals are now always delivered to the main Scheme thread to avoid crashes when a signal
Expand Down Expand Up @@ -2769,6 +2775,13 @@ in fasl files does not generally make sense.
%-----------------------------------------------------------------------------
\section{Bug Fixes}\label{section:bugfixes}

\subsection{Fix \scheme{fx-/wraparound} with \scheme{0} first argument (10.2.0)}

When \scheme{fx-/wraparound} was called with \scheme{0} as its first
argument, the call could be rewritten to have a single argument, even though
\scheme{fx-/wraparound} required two arguments. As part of the repair,
\scheme{fx-/wraparound} changed to accept a single argument.

\subsection{Performance regression for \scheme{fxdiv-and-mod} at optimize-level 3 (10.2.0)}

At optimize-level 3, the source optimizer (cp0) could replace calls to
Expand Down
6 changes: 6 additions & 0 deletions s/cpprim.ss
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,7 @@
[(e) (%inline - (immediate 0) ,e)]
[(e1 e2) (%inline - ,e1 ,e2)])
(define-inline 3 fx-/wraparound
[(e) (%inline - (immediate 0) ,e)]
[(e1 e2) (%inline - ,e1 ,e2)])
(define-inline 3 fx1-
[(e) (%inline - ,e (immediate ,(fix 1)))])
Expand Down Expand Up @@ -1840,6 +1841,11 @@
[(e) (go src sexpr `(immediate ,(fix 0)) e)]
[(e1 e2) (go src sexpr e1 e2)])
(define-inline 2 fx-/wraparound
[(e)
(bind #t (e)
`(if ,(build-fixnums? (list e))
,(%inline - (immediate 0) ,e)
,(build-libcall #t src sexpr fx-/wraparound `(immediate 0) e)))]
[(e1 e2)
(bind #t (e1 e2)
`(if ,(build-fixnums? (list e1 e2))
Expand Down
7 changes: 5 additions & 2 deletions s/mathprims.ss
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,11 @@
(#2%fx+/wraparound x1 x2)))

(set-who! fx-/wraparound
(lambda (x1 x2)
(#2%fx-/wraparound x1 x2)))
(case-lambda
[(x)
(#2%fx-/wraparound x)]
[(x1 x2)
(#2%fx-/wraparound x1 x2)]))

(set! fx1-
(lambda (x)
Expand Down
2 changes: 1 addition & 1 deletion s/primdata.ss
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@
(fx+ [sig [(fixnum ...) -> (fixnum)]] [flags arith-op partial-folder]) ; not restricted to 2 arguments
(fx+/wraparound [sig [(fixnum fixnum) -> (fixnum)]] [flags arith-op partial-folder safeongoodargs])
(fx- [sig [(fixnum fixnum ...) -> (fixnum)]] [flags arith-op partial-folder]) ; not restricted to 1 or 2 arguments
(fx-/wraparound [sig [(fixnum fixnum) -> (fixnum)]] [flags arith-op partial-folder safeongoodargs])
(fx-/wraparound [sig [(fixnum) -> (fixnum)] [(fixnum fixnum) -> (fixnum)]] [flags arith-op partial-folder safeongoodargs])
(fx/ [sig [(fixnum fixnum ...) -> (fixnum)]] [flags arith-op partial-folder]) ; not restricted to 1 or 2 arguments
(fx1+ [sig [(fixnum) -> (fixnum)]] [flags arith-op cp02])
(fx1- [sig [(fixnum) -> (fixnum)]] [flags arith-op cp02])
Expand Down

0 comments on commit 003a1bf

Please sign in to comment.