Bug 1146295: Factor out SIMD types initialization; r=h4writer
authorBenjamin Bouvier <benj@benj.me>
Mon, 23 Mar 2015 10:57:59 +0100
changeset 265108 bf1f3c86e45de07a73b1efe44b6554ae19cdb00e
parent 265107 8db04357fe91564a39b370e9ba8b3dce8544924e
child 265109 cf48b5413dbce95aff65fe58018efedfbf33fd4f
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersh4writer
bugs1146295
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1146295: Factor out SIMD types initialization; r=h4writer
js/src/builtin/SIMD.cpp
--- a/js/src/builtin/SIMD.cpp
+++ b/js/src/builtin/SIMD.cpp
@@ -326,28 +326,16 @@ CreateSimdClass(JSContext *cx, Handle<Gl
                                       T::TypedObjectMethods))
     {
         return nullptr;
     }
 
     return typeDescr;
 }
 
-const char*
-SimdTypeToMinimumLanesNumber(SimdTypeDescr &descr) {
-    switch (descr.type()) {
-      case SimdTypeDescr::TYPE_INT32:
-      case SimdTypeDescr::TYPE_FLOAT32:
-        return "3";
-      case SimdTypeDescr::TYPE_FLOAT64:
-        return "1";
-    }
-    MOZ_CRASH("Unexpected SIMD type description.");
-}
-
 bool
 SimdTypeDescr::call(JSContext *cx, unsigned argc, Value *vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
 
     Rooted<SimdTypeDescr*> descr(cx, &args.callee().as<SimdTypeDescr>());
     MOZ_ASSERT(size_t(static_cast<TypeDescr*>(descr)->size()) <= InlineTypedObject::MaximumSize,
                "inline storage is needed for using InternalHandle belows");
@@ -395,16 +383,37 @@ SimdTypeDescr::call(JSContext *cx, unsig
 ///////////////////////////////////////////////////////////////////////////
 // SIMD class
 
 const Class SIMDObject::class_ = {
     "SIMD",
     JSCLASS_HAS_CACHED_PROTO(JSProto_SIMD)
 };
 
+template <typename Defn>
+static JSObject *
+CreateAndBindSimdType(JSContext *cx, Handle<GlobalObject*> global, HandleObject SIMD,
+                      js::ImmutablePropertyNamePtr name, const JSFunctionSpec methodsSpec[])
+{
+    RootedObject typeObject(cx, CreateSimdClass<Defn>(cx, global, name));
+    if (!typeObject)
+        return nullptr;
+
+    // Define float32x4 functions and install as a property of the SIMD object.
+    RootedValue typeValue(cx, ObjectValue(*typeObject));
+    if (!JS_DefineFunctions(cx, typeObject, methodsSpec) ||
+        !DefineProperty(cx, SIMD, name, typeValue, nullptr, nullptr,
+                        JSPROP_READONLY | JSPROP_PERMANENT))
+    {
+        return nullptr;
+    }
+
+    return typeObject;
+}
+
 JSObject *
 SIMDObject::initClass(JSContext *cx, Handle<GlobalObject *> global)
 {
     // SIMD relies on having the TypedObject module initialized.
     // In particular, the self-hosted code for array() wants
     // to be able to call GetTypedObjectModule(). It is NOT necessary
     // to install the TypedObjectModule global, but at the moment
     // those two things are not separable.
@@ -416,76 +425,45 @@ SIMDObject::initClass(JSContext *cx, Han
     if (!objProto)
         return nullptr;
     RootedObject SIMD(cx, NewObjectWithGivenProto(cx, &SIMDObject::class_, objProto,
                                                   SingletonObject));
     if (!SIMD)
         return nullptr;
 
     // float32x4
-    RootedObject float32x4Object(cx);
-    float32x4Object = CreateSimdClass<Float32x4Defn>(cx, global,
-                                                     cx->names().float32x4);
-    if (!float32x4Object)
+    RootedObject f32x4(cx);
+    f32x4 = CreateAndBindSimdType<Float32x4Defn>(cx, global, SIMD, cx->names().float32x4,
+                                                 Float32x4Methods);
+    if (!f32x4)
         return nullptr;
-
-    // Define float32x4 functions and install as a property of the SIMD object.
-    RootedValue float32x4Value(cx, ObjectValue(*float32x4Object));
-    if (!JS_DefineFunctions(cx, float32x4Object, Float32x4Methods) ||
-        !DefineProperty(cx, SIMD, cx->names().float32x4,
-                        float32x4Value, nullptr, nullptr,
-                        JSPROP_READONLY | JSPROP_PERMANENT))
-    {
-        return nullptr;
-    }
+    global->setFloat32x4TypeDescr(*f32x4);
 
     // float64x2
-    RootedObject float64x2Object(cx);
-    float64x2Object = CreateSimdClass<Float64x2Defn>(cx, global,
-                                                     cx->names().float64x2);
-    if (!float64x2Object)
+    RootedObject f64x2(cx);
+    f64x2 = CreateAndBindSimdType<Float64x2Defn>(cx, global, SIMD, cx->names().float64x2,
+                                                 Float64x2Methods);
+    if (!f64x2)
         return nullptr;
-
-    // Define float64x2 functions and install as a property of the SIMD object.
-    RootedValue float64x2Value(cx, ObjectValue(*float64x2Object));
-    if (!JS_DefineFunctions(cx, float64x2Object, Float64x2Methods) ||
-        !DefineProperty(cx, SIMD, cx->names().float64x2,
-                        float64x2Value, nullptr, nullptr,
-                        JSPROP_READONLY | JSPROP_PERMANENT))
-    {
-        return nullptr;
-    }
+    global->setFloat64x2TypeDescr(*f64x2);
 
     // int32x4
-    RootedObject int32x4Object(cx);
-    int32x4Object = CreateSimdClass<Int32x4Defn>(cx, global,
-                                                 cx->names().int32x4);
-    if (!int32x4Object)
+    RootedObject i32x4(cx);
+    i32x4 = CreateAndBindSimdType<Int32x4Defn>(cx, global, SIMD, cx->names().int32x4,
+                                               Int32x4Methods);
+    if (!i32x4)
         return nullptr;
-
-    // Define int32x4 functions and install as a property of the SIMD object.
-    RootedValue int32x4Value(cx, ObjectValue(*int32x4Object));
-    if (!JS_DefineFunctions(cx, int32x4Object, Int32x4Methods) ||
-        !DefineProperty(cx, SIMD, cx->names().int32x4,
-                        int32x4Value, nullptr, nullptr,
-                        JSPROP_READONLY | JSPROP_PERMANENT))
-    {
-        return nullptr;
-    }
-
-    RootedValue SIMDValue(cx, ObjectValue(*SIMD));
+    global->setInt32x4TypeDescr(*i32x4);
 
     // Everything is set up, install SIMD on the global object.
+    RootedValue SIMDValue(cx, ObjectValue(*SIMD));
     if (!DefineProperty(cx, global, cx->names().SIMD, SIMDValue, nullptr, nullptr, 0))
         return nullptr;
 
     global->setConstructor(JSProto_SIMD, SIMDValue);
-    global->setFloat32x4TypeDescr(*float32x4Object);
-    global->setFloat64x2TypeDescr(*float64x2Object);
-    global->setInt32x4TypeDescr(*int32x4Object);
     return SIMD;
 }
 
 JSObject *
 js::InitSIMDClass(JSContext *cx, HandleObject obj)
 {
     MOZ_ASSERT(obj->is<GlobalObject>());
     Rooted<GlobalObject *> global(cx, &obj->as<GlobalObject>());