For Gauche 0.9.7

Next: , Previous: , Up: Library modules - SRFIs   [Contents][Index]

### 11.30 `srfi-143` - Fixnums

Module: srfi-143

This module provides a set of fixnum-specific operations.

A fixnum is a small exact integer that can be handled very efficiently. In Gauche, fixnum is 62bit wide on 64bit platforms, and 30bit wide on 32bit platforms.

Note that these procedures are defined only to work on fixnums, but it is not enforced. If you pass non-fixnum arguments, or the result falls out of range of fixnums, what happens is up to the implementation. Consider these procedures as the way to tell your intentions to the compiler for potential optimizations.

In the current Gauche architecture, generic numeric operators are just as efficient, so most procedures provided in this module are aliases to corresponding operators. However, we might employ some optimizations in future versions.

The procedure `fixnum?` is built-in, and not explaned here. See Numerical predicates.

Variable: fx-width

[SRFI-143] {srfi-143} A variable bound to an exact positive integer w, where w is the greatest number such that exact integers between `2^(w-1) - 1` and `-2^(w-1)` are all fixnums. This value is the same as the built-in procedure `fixnum-width` returns (see Arithmetics).

In Gauche, it is usually `30` for 32bit platforms, and `62` for 64bit platforms.

Variable: fx-greatest
Variable: fx-least

[SRFI-143] {srfi-143} Variables bound to the greatest fixnum and the least fixnum. They are the same as the built-in procedures `greatest-fixnum` and `least-fixnum` return, respectively (see Arithmetics).

The following table shows the typical values on Gauche:

Platform`fx-greatest``fx-least`
32bit536,870,911-536,870,912
64bit2,305,843,009,213,693,951-2,305,843,009,213,693,952
Function: fx=? i …
Function: fx<? i …
Function: fx<=? i …
Function: fx>? i …
Function: fx>=? i …

[SRFI-143] {srfi-143} These are equivalent to built-in `=`, `<`, `<=`, `>` and `>=`, except that you should use these only for fixnums.

Function: fxzero? i
Function: fxpositive? i
Function: fxnegative? i
Function: fxodd? i
Function: fxeven? i

[SRFI-143] {srfi-143} These are equivalent to built-in `zero?`, `positive?`, `negative?`, `odd?` and `even?`, except that you should use these only for fixnums.

Function: fxmax i j …
Function: fxmin i j …

[SRFI-143] {srfi-143} These are equivalent to built-in `max` and `min`, except that you should use these only for fixnums.

Function: fx+ i j
Function: fx- i j
Function: fx* i j

[SRFI-143] {srfi-143} These are equivalent to built-in `+`, `-` and `*`, except that these take exactly two arguments, and you should use these only for fixnums and when the result fits within fixnum range.

Function: fxneg i

[SRFI-143] {srfi-143} This is equivalent to single-argument `-`, except that you should use this only for fixnums and when the result fits within fixnum range.

Function: fxquotient i j
Function: fxremainder i j
Function: fxabs i
Function: fxsquare i

[SRFI-143] {srfi-143} These are equivalent to built-in `quotient`, `remainder`, `abs` and `square`, except that you should use these only for fixnums and when the result fits within fixnum range.

Function: fxsqrt i

[SRFI-143] {srfi-143} This is equivalent to `exact-integer-sqrt` (not `sqrt`), except that you should use it only for fixnums. See Arithmetics.

Function: fx+/carry i j k
Function: fx-/carry i j k
Function: fx*/carry i j k

[SRFI-143] {srfi-143} These calculates `(+ i j k)`, `(- i j k)` and `(+ (* i j) k)`, respectively, then split the result to the remainder value `R` in the fixnum range, and spilled value `Q`, and return those values. That is, `(+ (* Q (expt 2 fx-width)) R)` is the result of above calculations. Both `Q` and `R` fits in the fixnum range, and `- 2^(w-1) <= R < 2^(w-1)`, where `w` is `fx-width`.

```(fx*/carry 1845917459 19475917581 4735374)
⇒ -942551854601421179 and 8

(+ (* 8 (expt 2 fx-width)) -942551854601421179)
⇒ 35950936292817682053

(+ (* 1845917459 19475917581) 4735374)
⇒ 35950936292817682053
```

These are primitives to implement extended-precision integer arithmetic on top of fixnum operations. In Gauche, however, you can just use built-in bignums. We provide these for the compatibility.

Function: fxnot i
Function: fxand i …
Function: fxior i …
Function: fxxor i …
Function: fxarithmetic-shift i count
Function: fxlength i
Function: fxbit-count i
Function: fxcopy-bit index i boolean
Function: fxbit-set? index i
Function: fxbit-field i start end
Function: fxfirst-set-bit i

[SRFI-143] {srfi-143} These are equivalent to `lognot`, `logand`, `logior`, `logxor`, `ash`, `integer-length`, `logcount`, `copy-bit`, `logbit?`, `bit-field`, and `twos-exponent-factor` respectively, except that you should use these only for fixnums. See Basic bitwise operations.

[SRFI-143] {srfi-143} These are equivalent to srfi-60’s `bitwise-if`, `rotate-bit-field` and `reverse-bit-field`, except that you should use these only for fixnums. See Integers as bits.