Quoth (e-mail address removed) (ivo welch):
hi perl gurus: I would like to read about 1GB of floats, not doubles.
Does perl have the facility to designate internal storage of an array
to be in float (4-byte), not double (8-byte) representation?
Otherwise, I am running out of memory...
No. NVs (Perl's representation of floating-point) are always NVs.
You could perhaps achieve this by writing a little XS or Inline class to
tie an array to a C float[]; something like (completely untested):
Tie/FloatArray.xs:
/* standard XS stuff */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
typedef struct floatarray {
UV size;
float *data;
} *Tie_FloatArray;
void extend_floatarray(Tie_FloatArray this, UV size)
{
this->size = size;
Renewc(this->data, this->size ? this->size : 1, float, float *);
}
/* note that the line below has a literal tab in the middle */
TYPEMAP
Tie_FloatArray T_PTROBJ_SPECIAL
INPUT
T_PTROBJ_SPECIAL
if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
IV tmp = SvIV((SV*)SvRV($arg));
$var = ($type) tmp;
}
else
croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
OUTPUT
T_PTROBJ_SPECIAL
sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\", (void*)$var);
MODULE = Tie::FloatArray PACKAGE = Tie::FloatArray
Tie_FloatArray
TIEARRAY (char *class)
CODE:
Newc(434, RETVAL, 1, struct floatarray, Tie_FloatArray);
RETVAL->size = 0;
Newc(434, RETVAL->data, 1, float, float *);
OUTPUT:
RETVAL
SV *
FETCH (Tie_FloatArray this, UV index)
CODE:
if (index < this->size) {
RETVAL = NEWSV(434, 0);
sv_setnv(RETVAL, this->data[index]);
}
else {
RETVAL = &PL_sv_undef;
}
OUTPUT:
RETVAL
float
STORE (Tie_FloatArray this, UV index, float value)
CODE:
if (index > this->size) {
croak("index %"UVf" > size %"UVf" of Tie::FloatArray",
index, this->size);
}
RETVAL = this->data[index] = value;
OUTPUT:
RETVAL
UV
FETCHSIZE (Tie_FloatArray this)
CODE:
RETVAL = this->size;
OUTPUT:
RETVAL
void
STORESIZE (Tie_FloatArray this, UV size)
CODE:
extend_floatarray(this, size);
void
EXTEND (Tie_FloatArray this, UV size)
CODE:
extend_floatarray(this, size);
void
DESTROY (Tie_FloatArray this)
CODE:
Safefree(this->data);
Safefree(this);
Note that this array *only* supports float values: no undefs.
Ben