Downcasting of Signed Types Not Working #23

Danielku15 opened this Issue Jan 25, 2013 · 0 comments


None yet
1 participant

If you try to downcast any integer to a lower byte signed integer the output is wrong.

trace("i16: " + (i16)(65535));

Leads to "Unknown expression type: undefined". It works as long the casted integer is in range of the int16.

A solution could be to write the unsigned version of the target integer into a ArrayBuffer using the according view, then read it again from the target type view.

var castBuffer = new ArrayBuffer(2);
var i16 = new Int16Array(castBuffer);
var u16 = new Uint16Array(castBuffer);
var i8 = new Int8Array(castBuffer);
var u8 = new Uint8Array(castBuffer);

exports.toInt16 = function (i) { u16[0] = i & 65535; return i16[0]; }
exports.toInt8 = function (i) { u8[0] = i & 255; return i8[0]; }

Downcasting float64 to float32 could be hard. I don't have a solution for this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment