4.3.4 Fixnums (original) (raw)

4.3.4 Fixnums🔗

The racket/fixnum library provides operations likefx+ that consume and produce only fixnums. The operations in this library are meant to be safe versions of unsafe operations likeunsafe-fx+. These safe operations are generally no faster than using generic primitives like +.

The expected use of the racket/fixnum library is for code where the require of racket/fixnum is replaced with

to drop in unsafe versions of the library. Alternately, when encountering crashes with code that uses unsafe fixnum operations, use the racket/fixnum library to help debug the problems.

4.3.4.1 Fixnum Arithmetic🔗

Changed in version 7.0.0.13 of package base: Allow zero or more arguments for fx+ and fx*and one or more arguments for fx-.

Like bitwise-and, bitwise-ior,bitwise-xor, bitwise-not, andarithmetic-shift, but constrained to consume fixnums; the result is always a fixnum. The unsafe-fxlshift andunsafe-fxrshift operations correspond toarithmetic-shift, but require non-negative arguments;unsafe-fxlshift is a positive (i.e., left) shift, andunsafe-fxrshift is a negative (i.e., right) shift, where the number of bits to shift must be no more than the number of bits used to represent a fixnum. Theexn:fail:contract:non-fixnum-result exception is raised if the arithmetic result would not be a fixnum.

Changed in version 7.0.0.13 of package base: Allow any number of arguments for fxand, fxior, and fxxor.

Counts the number of bits in the two’s complement representation ofa. Depending on the platform, the fxpopcount32 andfxpopcount16 operations can be faster when the result is known to be no more than 32 or 16, respectively.

Added in version 8.5.0.7 of package base.

Like fx+, fx-, fx*, and fxlshift, but a fixnum result is produced for any allowed arguments (i.e., for any fixnum argument, except that the secondfxlshift/wraparound argument must be between 0 and the number of bits in a fixnum, inclusive). The result is produced by simply discarding bits that do not fit in a fixnum representation. The result is negative if the highest of the retained bits is set—even, for example, if the value was produced by adding two positive fixnums.

Added in version 7.9.0.6 of package base.

Shifts the bits in a to the right by b, filling in with zeros. With the sign bit treated as just another bit, a logical right-shift of a negative-signed fixnum can produce a large positive fixnum. For example, (fxrshift/logical -1 1) produces (most-positive-fixnum), illustrating that logical right-shift results are platform-dependent.

Examples:

> (fxrshift/logical 128 2)
32
> (fxrshift/logical 255 4)
15
> (= (fxrshift/logical -1 1) (most-positive-fixnum))
#t

Added in version 8.8.0.5 of package base.

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

The fx->fl function is the same as exact->inexact or->fl constrained to a fixnum argument.

The fl->fx function is the same as truncate followed by inexact->exact or fl->exact-integer constrained to returning a fixnum. If the truncated flonum does not fit into a fixnum, the exn:fail:contract exception is raised.

Changed in version 7.7.0.8 of package base: Changed fl->fx to truncate.

Returns #t if v is a fixnum and is represented by fixnum by every Racket implementation, #fotherwise.

Added in version 7.3.0.11 of package base.

4.3.4.2 Fixnum Vectors🔗

A fxvector is like a vector, but it holds onlyfixnums. The only advantage of a fxvector over avector is that a shared version can be created with functions like shared-fxvector.

Two fxvectors are equal? if they have the same length, and if the values in corresponding slots of the fxvectors areequal?.

A printed fxvector starts with #fx(, optionally with a number between the #fx and(. See Reading Vectors for information on reading fxvectors and Printing Vectors for information on printing fxvectors.

Returns #t if v is a fxvector, #f otherwise.

Creates a fxvector containing the given fixnums.

Example:

> (fxvector 2 3 4 5)
(fxvector 2 3 4 5)

Creates a fxvector with size elements, where every slot in the fxvector is filled with x.

Example:

> (make-fxvector 4 3)
(fxvector 3 3 3 3)

Returns the length of vec (i.e., the number of slots in thefxvector).

Returns the fixnum in slot pos ofvec. The first slot is position 0, and the last slot is one less than (fxvector-length vec).

Sets the fixnum in slot pos of vec. The first slot is position 0, and the last slot is one less than(fxvector-length vec).

Creates a fresh fxvector of size (- end start), with all of the elements of vec from start (inclusive) toend (exclusive).

Returns a sequence equivalent to vec when no optional arguments are supplied.

The optional arguments start, stop, andstep are as in in-vector.

An in-fxvector application can provide better performance for fxvector iteration when it appears directly in a for clause.

(for/fxvector maybe-length (for-clause ...) body ...)
(for*/fxvector maybe-length (for-clause ...) body ...) maybe-length = | #:length length-expr #:length length-expr #:fill fill-expr length-expr : exact-nonnegative-integer? fill-expr : fixnum?

Example:

> (shared-fxvector 2 3 4 5)
(fxvector 2 3 4 5)

Creates a fxvector with size elements, where every slot in the fxvector is filled with x. For communication among places, the new fxvector is allocated in the shared memory space.

Example:

> (make-shared-fxvector 4 3)
(fxvector 3 3 3 3)
4.3.4.3 Fixnum Range🔗

Returns the largest-magnitude positive and negative fixnums. The values of (most-positive-fixnum) and(most-negative-fixnum) depend on the platform and virtual machine, but all fixnums are in the range(most-negative-fixnum) to (most-positive-fixnum)inclusive, and all exact integers in that range are fixnums.

Added in version 8.1.0.7 of package base.