NAME ^

src/utils.c - Some utility functions

DESCRIPTION ^

Prototypes are in src/misc.h.

Opcode helper functions that don't really fit elsewhere.

Functions ^

INTVAL intval_mod(INTVAL i2, INTVAL i3)

NOTE: This "corrected mod" algorithm is based on the C code on page 70 of [1]. Assuming correct behavior of the built-in mod operator (%) with positive arguments, this algorithm implements a mathematically convenient version of mod, defined thus:

    x mod y = x - y * floor(x / y)
For more information on this definition of mod, see section 3.4 of [2], pages 81-85.

References:

[1] Donald E. Knuth, *MMIXware: A RISC Computer for the Third Millennium* Springer, 1999.

[2] Ronald L. Graham, Donald E. Knuth and Oren Patashnik, *Concrete Mathematics*, Second Edition. Addison-Wesley, 1994.

FLOATVAL floatval_mod(FLOATVAL n2, FLOATVAL n3)

Returns n2 mod n3.

Includes a workaround for buggy code generation in the lcc compiler.

Random Number Generator ^

Based on the rand48() family of functions.

static void next_rand(_rand_buf X)

Returns the next random number in X.

static FLOATVAL _erand48(_rand_buf buf)

Returns a double in the interval [0.0, 1.0).

static FLOATVAL _drand48(void)

Returns a double in the interval [0.0, 1.0).

static long _jrand48(_rand_buf buf)

Returns a long in the interval [-2^31, 2^31).

static long _nrand48(_rand_buf buf)

Returns a long in the interval [0, 2^31).

static long _lrand48(void)

Returns a long in the interval [0, 2^31).

static long _mrand48(void)

Returns a long in the interval [-2^31, 2^31).

static void _srand48(long seed)

Sets the high order 32 bits to the argument seed. The low order 16 bits are set to the arbitrary value 0x330e.

FLOATVAL Parrot_float_rand(INTVAL how_random)

Returns a FLOATVAL in the interval [0.0, 1.0).

how_random is ignored.

INTVAL Parrot_uint_rand(INTVAL how_random)

Returns an INTVAL in the interval [0, 2^31).

how_random is ignored.

INTVAL Parrot_int_rand(INTVAL how_random)

Returns an INTVAL in the interval [-2^31, 2^31).

how_random is ignored.

INTVAL Parrot_range_rand(INTVAL from, INTVAL to, INTVAL how_random)

Returns an INTVAL in the range [from, to].

how_random is ignored.

void Parrot_srand(INTVAL seed)

Seeds the random number generator with seed.

Array Functions ^

void *Parrot_make_la(Interp *interpreter, PMC *array)

Creates a C array of longs with one more element than the number of elements in *array. The elements are then copied from *array to the new array, and the last (extra) element is set to 0.

Used in src/nci.c.

void Parrot_destroy_la(long *array)

Use this to destroy an array created with Parrot_make_la().

void *Parrot_make_cpa(Interp *interpreter, PMC *array)

Creates a C array of char *s with one more element than the number of elements in *array. The elements are then copied from *array to the new array, and the last (extra) element is set to 0.

Currently unused.

void Parrot_destroy_cpa(char **array)

Use this to destroy an array created with Parrot_make_cpa().

PMC *tm_to_array(Parrot_Interp, struct tm *)

Helper to convert a struct tm * to an Array

HISTORY ^

Initial version by leo 2003.09.09.


parrot