On this page:
16.1 Unsafe Numeric Operations
unsafe-fx+
unsafe-fx-
unsafe-fx*
unsafe-fxquotient
unsafe-fxremainder
unsafe-fxabs
unsafe-fxand
unsafe-fxior
unsafe-fxxor
unsafe-fxnot
unsafe-fxlshift
unsafe-fxrshift
unsafe-fx=
unsafe-fx<
unsafe-fx>
unsafe-fx<=
unsafe-fx>=
unsafe-fx->fl
unsafe-fl+
unsafe-fl-
unsafe-fl*
unsafe-fl/
unsafe-flabs
unsafe-fl=
unsafe-fl<
unsafe-fl>
unsafe-fl<=
unsafe-fl>=
16.2 Unsafe Data Extraction
unsafe-car
unsafe-cdr
unsafe-mcar
unsafe-mcdr
unsafe-set-mcar!
unsafe-set-mcdr!
unsafe-vector-length
unsafe-vector-ref
unsafe-vector-set!
unsafe-string-length
unsafe-string-ref
unsafe-string-set!
unsafe-bytes-length
unsafe-bytes-ref
unsafe-bytes-set!
unsafe-struct-ref
unsafe-struct-set!

16 Unsafe Operations

 (require scheme/unsafe/ops)

All fuctions and forms provided by scheme/base and scheme check their arguments to ensure that the arguments conform to contracts and other constraints. For example, vector-ref checks its arguments to ensure that the first argument is a vector, that the second argument is an exact integer, and that the second argument is between 0 and one less than the vector’s length, inclusive.

Functions provided by scheme/unsafe/ops are unsafe. They have certain constraints, but the constraints are not checked, which allows the system to generate and execute faster code. If arguments violate an unsafe function’s constraints, the function’s behavior and result is unpredictable, and the entire system can crash or become corrupted.

All of the exported bindings of scheme are protected in the sense of protect-out, so access to unsafe operations can be prevented by adjusting the code inspector (see Code Inspectors).

16.1 Unsafe Numeric Operations

(unsafe-fx+ a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fx- a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fx* a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxquotient a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxremainder a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxabs a)  fixnum?
  a : fixnum?
For fixnums: Like +, -, *, quotient, remainder, and abs, but constrained to consume fixnums and produce a fixnum result. The mathematical operation on a and b must be representable as a fixnum. In the case of unsafe-fxquotient and unsafe-fxremainder, b must not be 0.

(unsafe-fxand a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxior a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxxor a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxnot a)  fixnum?
  a : fixnum?
(unsafe-fxlshift a b)  fixnum?
  a : fixnum?
  b : fixnum?
(unsafe-fxrshift a b)  fixnum?
  a : fixnum?
  b : fixnum?
For fixnums: Like bitwise-and, bitwise-ior, bitwise-xor, bitwise-not, and arithmetic-shift, but constrained to consume fixnums; the result is always a fixnum. The unsafe-fxlshift and unsafe-fxrshift operations correspond to arithmetic-shift, but require non-negative arguments; unsafe-fxlshift is a positive (i.e., left) shift, and unsafe-fxrshift is a negative (i.e., right) shift, where the number of bits to shift must be less than the number of bits used to represent a fixnum, and the result is effectively bitwise-anded with the most negative fixnum.

(unsafe-fx= a b)  boolean?
  a : fixnum?
  b : fixnum?
(unsafe-fx< a b)  boolean?
  a : fixnum?
  b : fixnum?
(unsafe-fx> a b)  boolean?
  a : fixnum?
  b : fixnum?
(unsafe-fx<= a b)  boolean?
  a : fixnum?
  b : fixnum?
(unsafe-fx>= a b)  boolean?
  a : fixnum?
  b : fixnum?
For fixnums: Like =, <, >, <=, and >=, but constrained to consume fixnums.

(unsafe-fx->fl a)  inexact-real?
  a : fixnum?
Like exact->inexact, but constrained to consume fixnums.

(unsafe-fl+ a b)  inexact-real?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl- a b)  inexact-real?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl* a b)  inexact-real?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl/ a b)  inexact-real?
  a : inexact-real?
  b : inexact-real?
(unsafe-flabs a)  inexact-real?
  a : inexact-real?
For real inexact numbers: Like +, -, *, /, and abs, but constrained to consume real inexact numbers. The result is always a real inexact number.

(unsafe-fl= a b)  boolean?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl< a b)  boolean?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl> a b)  boolean?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl<= a b)  boolean?
  a : inexact-real?
  b : inexact-real?
(unsafe-fl>= a b)  boolean?
  a : inexact-real?
  b : inexact-real?
For real inexact numbers: Like =, <, >, <=, and >=, but constrained to consume real inexact numbers.

16.2 Unsafe Data Extraction

(unsafe-car p)  any/c
  p : pair?
(unsafe-cdr p)  any/c
  p : pair?
(unsafe-mcar p)  any/c
  p : mpair?
(unsafe-mcdr p)  any/c
  p : mpair?
(unsafe-set-mcar! p v)  void?
  p : mpair?
  v : any/c
(unsafe-set-mcdr! p v)  void?
  p : mpair?
  v : any/c
Unsafe variants of car, cdr, mcar, mcdr, set-mcar!, and set-mcdr!.

(unsafe-vector-length v)  fixnum?
  v : vector?
(unsafe-vector-ref v k)  any/c
  v : vector?
  k : fixnum?
(unsafe-vector-set! v k val)  void?
  v : vector?
  k : fixnum?
  val : any/c
Unsafe versions of vector-length, vector-ref, and vector-set!. A vector’s size can never be larger than a fixnum (so even vector-length always returns a fixnum).

(unsafe-string-length str)  fixnum?
  str : string?
(unsafe-string-ref str k)
  (and/c char? (lambda (ch) (<= 0 (char->integer ch) 255)))
  str : string?
  k : fixnum?
(unsafe-string-set! str k ch)  void?
  str : (and/c string? (not/c immutable?))
  k : fixnum?
  ch : char?
Unsafe versions of string-length, string-ref, and string-set!. The unsafe-string-ref procedure can be used only when the result will be a Latin-1 character. A string’s size can never be larger than a fixnum (so even string-length always returns a fixnum).

(unsafe-bytes-length bstr)  fixnum?
  bstr : bytes?
(unsafe-bytes-ref bstr k)  byte?
  bstr : bytes?
  k : fixnum?
(unsafe-bytes-set! bstr k b)  void?
  bstr : (and/c bytes? (not/c immutable?))
  k : fixnum?
  b : byte?
Unsafe versions of bytes-length, bytes-ref, and bytes-set!. A bytes’s size can never be larger than a fixnum (so even bytes-length always returns a fixnum).

(unsafe-struct-ref v k)  any/c
  v : any/c
  k : fixnum?
(unsafe-struct-set! v k val)  void?
  v : any/c
  k : fixnum?
  val : any/c
Unsafe field access and update for an instance of a structure type. The index k must be between 0 (inclusive) and the number of fields in the struture (exclusive). In the case of unsafe-struct-set!, the field must be mutable.