Bug 743878 - Move uint8_clamped and associated machinery into ObjectImpl.h, so that the new object representation can use it as well. r=sfink

This commit is contained in:
Jeff Walden 2012-04-09 15:14:59 -07:00
parent 8c91faa124
commit 0457887cdb
7 changed files with 97 additions and 91 deletions

View File

@ -946,8 +946,8 @@ TypedArray::dataOffset()
/* Helper clamped uint8_t type */
int32_t JS_FASTCALL
js_TypedArray_uint8_clamp_double(const double x)
uint32_t JS_FASTCALL
js::ClampDoubleToUint8(const double x)
{
// Not < so that NaN coerces to 0
if (!(x >= 0))
@ -979,77 +979,6 @@ js_TypedArray_uint8_clamp_double(const double x)
return y;
}
struct uint8_clamped {
uint8_t val;
uint8_clamped() { }
uint8_clamped(const uint8_clamped& other) : val(other.val) { }
// invoke our assignment helpers for constructor conversion
uint8_clamped(uint8_t x) { *this = x; }
uint8_clamped(uint16_t x) { *this = x; }
uint8_clamped(uint32_t x) { *this = x; }
uint8_clamped(int8_t x) { *this = x; }
uint8_clamped(int16_t x) { *this = x; }
uint8_clamped(int32_t x) { *this = x; }
uint8_clamped(double x) { *this = x; }
inline uint8_clamped& operator= (const uint8_clamped& x) {
val = x.val;
return *this;
}
inline uint8_clamped& operator= (uint8_t x) {
val = x;
return *this;
}
inline uint8_clamped& operator= (uint16_t x) {
val = (x > 255) ? 255 : uint8_t(x);
return *this;
}
inline uint8_clamped& operator= (uint32_t x) {
val = (x > 255) ? 255 : uint8_t(x);
return *this;
}
inline uint8_clamped& operator= (int8_t x) {
val = (x >= 0) ? uint8_t(x) : 0;
return *this;
}
inline uint8_clamped& operator= (int16_t x) {
val = (x >= 0)
? ((x < 255)
? uint8_t(x)
: 255)
: 0;
return *this;
}
inline uint8_clamped& operator= (int32_t x) {
val = (x >= 0)
? ((x < 255)
? uint8_t(x)
: 255)
: 0;
return *this;
}
inline uint8_clamped& operator= (const double x) {
val = uint8_t(js_TypedArray_uint8_clamp_double(x));
return *this;
}
inline operator uint8_t() const {
return val;
}
};
/* Make sure the compiler isn't doing some funky stuff */
JS_STATIC_ASSERT(sizeof(uint8_clamped) == 1);
template<typename NativeType> static inline const int TypeIDOfType();
template<> inline const int TypeIDOfType<int8_t>() { return TypedArray::TYPE_INT8; }
template<> inline const int TypeIDOfType<uint8_t>() { return TypedArray::TYPE_UINT8; }
@ -1061,15 +990,6 @@ template<> inline const int TypeIDOfType<float>() { return TypedArray::TYPE_FLOA
template<> inline const int TypeIDOfType<double>() { return TypedArray::TYPE_FLOAT64; }
template<> inline const int TypeIDOfType<uint8_clamped>() { return TypedArray::TYPE_UINT8_CLAMPED; }
template<typename NativeType> static inline const bool TypeIsUnsigned() { return false; }
template<> inline const bool TypeIsUnsigned<uint8_t>() { return true; }
template<> inline const bool TypeIsUnsigned<uint16_t>() { return true; }
template<> inline const bool TypeIsUnsigned<uint32_t>() { return true; }
template<typename NativeType> static inline const bool TypeIsFloatingPoint() { return false; }
template<> inline const bool TypeIsFloatingPoint<float>() { return true; }
template<> inline const bool TypeIsFloatingPoint<double>() { return true; }
template<typename NativeType> static inline const bool ElementTypeMayBeDouble() { return false; }
template<> inline const bool ElementTypeMayBeDouble<uint32_t>() { return true; }
template<> inline const bool ElementTypeMayBeDouble<float>() { return true; }

View File

@ -331,9 +331,6 @@ JS_FRIEND_API(JSObject *)
js_CreateTypedArrayWithBuffer(JSContext *cx, int atype, JSObject *bufArg,
int byteoffset, int length);
extern int32_t JS_FASTCALL
js_TypedArray_uint8_clamp_double(const double x);
JS_FRIEND_API(JSBool)
JS_IsArrayBufferObject(JSObject *obj);

View File

@ -1135,7 +1135,7 @@ static const JSC::MacroAssembler::RegisterID JSParamReg_Argc = JSC::MIPSRegiste
done.linkTo(label(), this);
}
// Inline version of js_TypedArray_uint8_clamp_double.
// Inline version of js::ClampDoubleToUint8.
void clampDoubleToUint8(FPRegisterID fpReg, FPRegisterID fpTemp, RegisterID reg)
{
JS_ASSERT(fpTemp != Registers::FPConversionTemp);
@ -1164,7 +1164,7 @@ static const JSC::MacroAssembler::RegisterID JSParamReg_Argc = JSC::MIPSRegiste
Jump done3 = branchDouble(Assembler::DoubleNotEqual, fpTemp, Registers::FPConversionTemp);
// It was a tie. Mask out the ones bit to get an even value.
// See js_TypedArray_uint8_clamp_double for the reasoning behind this.
// See js::ClampDoubleToUint8 for the reasoning behind this.
and32(Imm32(~1), reg);
done1.linkTo(label(), this);

View File

@ -1261,7 +1261,7 @@ mjit::Compiler::convertForTypedArray(int atype, ValueRemat *vr, bool *allocated)
i32 = ClampIntForUint8Array(i32);
} else {
i32 = (atype == TypedArray::TYPE_UINT8_CLAMPED)
? js_TypedArray_uint8_clamp_double(v.toDouble())
? ClampDoubleToUint8(v.toDouble())
: js_DoubleToECMAInt32(v.toDouble());
}
*vr = ValueRemat::FromConstant(Int32Value(i32));

View File

@ -1722,7 +1722,7 @@ stubs::ConvertToTypedInt(JSContext *cx, Value *vp)
if (vp->isDouble()) {
if (Clamped)
return js_TypedArray_uint8_clamp_double(vp->toDouble());
return ClampDoubleToUint8(vp->toDouble());
return js_DoubleToECMAInt32(vp->toDouble());
}

View File

@ -128,7 +128,7 @@ ConstantFoldForIntArray(JSContext *cx, JSObject *tarray, ValueRemat *vr)
int32_t i32 = 0;
if (v.isDouble()) {
i32 = (TypedArray::getType(tarray) == js::TypedArray::TYPE_UINT8_CLAMPED)
? js_TypedArray_uint8_clamp_double(v.toDouble())
? ClampDoubleToUint8(v.toDouble())
: js_DoubleToECMAInt32(v.toDouble());
} else if (v.isInt32()) {
i32 = v.toInt32();

View File

@ -166,6 +166,95 @@ class SparseElementsHeader : public ElementsHeader
void operator=(const SparseElementsHeader &other) MOZ_DELETE;
};
extern uint32_t JS_FASTCALL
ClampDoubleToUint8(const double x);
struct uint8_clamped {
uint8_t val;
uint8_clamped() { }
uint8_clamped(const uint8_clamped& other) : val(other.val) { }
// invoke our assignment helpers for constructor conversion
uint8_clamped(uint8_t x) { *this = x; }
uint8_clamped(uint16_t x) { *this = x; }
uint8_clamped(uint32_t x) { *this = x; }
uint8_clamped(int8_t x) { *this = x; }
uint8_clamped(int16_t x) { *this = x; }
uint8_clamped(int32_t x) { *this = x; }
uint8_clamped(double x) { *this = x; }
uint8_clamped& operator=(const uint8_clamped& x) {
val = x.val;
return *this;
}
uint8_clamped& operator=(uint8_t x) {
val = x;
return *this;
}
uint8_clamped& operator=(uint16_t x) {
val = (x > 255) ? 255 : uint8_t(x);
return *this;
}
uint8_clamped& operator=(uint32_t x) {
val = (x > 255) ? 255 : uint8_t(x);
return *this;
}
uint8_clamped& operator=(int8_t x) {
val = (x >= 0) ? uint8_t(x) : 0;
return *this;
}
uint8_clamped& operator=(int16_t x) {
val = (x >= 0)
? ((x < 255)
? uint8_t(x)
: 255)
: 0;
return *this;
}
uint8_clamped& operator=(int32_t x) {
val = (x >= 0)
? ((x < 255)
? uint8_t(x)
: 255)
: 0;
return *this;
}
uint8_clamped& operator=(const double x) {
val = uint8_t(ClampDoubleToUint8(x));
return *this;
}
operator uint8_t() const {
return val;
}
void staticAsserts() {
MOZ_STATIC_ASSERT(sizeof(uint8_clamped) == 1,
"uint8_clamped must be layout-compatible with uint8_t");
}
};
/* Note that we can't use std::numeric_limits here due to uint8_clamped. */
template<typename T> static inline const bool TypeIsFloatingPoint() { return false; }
template<> inline const bool TypeIsFloatingPoint<float>() { return true; }
template<> inline const bool TypeIsFloatingPoint<double>() { return true; }
template<typename T> static inline const bool TypeIsUnsigned() { return false; }
template<> inline const bool TypeIsUnsigned<uint8_t>() { return true; }
template<> inline const bool TypeIsUnsigned<uint16_t>() { return true; }
template<> inline const bool TypeIsUnsigned<uint32_t>() { return true; }
template<typename T> static inline const bool TypeIsUint8Clamped() { return false; }
template<> inline const bool TypeIsUint8Clamped<uint8_clamped>() { return true; }
template <typename T>
class TypedElementsHeader : public ElementsHeader
{
@ -248,7 +337,7 @@ class Float64ElementsHeader : public TypedElementsHeader<double>
void operator=(const Float64ElementsHeader &other) MOZ_DELETE;
};
class Uint8ClampedElementsHeader : public TypedElementsHeader<uint8_t>
class Uint8ClampedElementsHeader : public TypedElementsHeader<uint8_clamped>
{
private:
inline bool isUint8Clamped() const MOZ_DELETE;