Re: Double Array and Char Array that Take up the Same Space
According to KevinSimonson <kvnsmnsn@hotmail.com>:
In C I can write a <union> and make it so the same space in memory can
be treated both as a 64-bit <double> and a 4-element <short> array.
Actually it is not reliable in C either. Accessing the same space under
two distinct types implies a host of aliasing-related trouble,
especially when neither of the types is the ubiquitous "unsigned char".
Code which uses such tricks is prone to break when compiled with newer
compilers or with optimization flags. And, of course, it also has
portability issues, e.g. with endianness.
The "safe" way is to keep the space under one type (e.g. an array of
eight "unsigned char") and access it through dedicated macros or inline
functions. This would need only two such functions, one for reading and
one for writing. Then, if I have a specific system on which the decoding
and encoding functions turn out to be a performance bottleneck (it
happens much less often than usually expected), then I could replace
them with a union-based trick, or inline assembly, or whatever does the
job. This way I have only two functions to modify (and not the thousands
of lines of code which use those functions), and the whole thing is much
more portable and reliable.
In Java, you would do the same thing except for the last bit with inline
assembly, because that does not exist in Java (except if you resort to
JNI -- interface to native code -- but there is an overhead). This would
look like this (completely untested):
class ArrayOfShortsOrDoubles {
private short[] data;
ArrayOfShortsOrDoubles(int len)
{
data = new short[len];
}
int length()
{
return data.length;
}
short get(int x)
{
return data[x];
}
void set(int x, short value)
{
data[x] = value;
}
double getDouble(int x)
{
int hi = (data[x] << 16) | (data[x + 1] & 0xFFFF);
int lo = (data[x+ 2] << 16) | (data[x + 3] & 0xFFFF);
return Double.longBitsToDouble(
((long)hi << 32) | (lo & 0xFFFFFFFFL));
}
void setDouble(int x, double value)
{
long v = Double.doubleToLongBits(value);
data[0] = (short)(v >>> 48);
data[1] = (short)(v >>> 32);
data[2] = (short)(v >>> 16);
data[3] = (short)v;
}
}
Note that:
-- I am considering here the case where you want an array of such
short-or-double values.
-- I am using big-endian conventions in the splitting of 64-bit
IEEE754 "double" values into four 16-bit "short".
-- I am indexing by 16-bit units, whether I want doubles or shorts.
-- Double.doubleToLongBits() normalizes NaN values; you may want to
use Double.doubleToRawLongBits() instead.
--Thomas Pornin