1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/builtin/SIMD.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef builtin_SIMD_h 1.11 +#define builtin_SIMD_h 1.12 + 1.13 +#include "jsapi.h" 1.14 +#include "jsobj.h" 1.15 +#include "builtin/TypedObject.h" 1.16 +#include "vm/GlobalObject.h" 1.17 + 1.18 +/* 1.19 + * JS SIMD functions. 1.20 + * Spec matching polyfill: 1.21 + * https://github.com/johnmccutchan/ecmascript_simd/blob/master/src/ecmascript_simd.js 1.22 + */ 1.23 + 1.24 +#define FLOAT32X4_NULLARY_FUNCTION_LIST(V) \ 1.25 + V(zero, (FuncZero<Float32x4>), 0, 0, Zero) 1.26 + 1.27 +#define FLOAT32X4_UNARY_FUNCTION_LIST(V) \ 1.28 + V(abs, (Func<Float32x4, Abs<float, Float32x4>, Float32x4>), 1, 0, Abs) \ 1.29 + V(bitsToInt32x4, (FuncConvertBits<Float32x4, Int32x4>), 1, 0, BitsToInt32x4) \ 1.30 + V(neg, (Func<Float32x4, Neg<float, Float32x4>, Float32x4>), 1, 0, Neg) \ 1.31 + V(reciprocal, (Func<Float32x4, Rec<float, Float32x4>, Float32x4>), 1, 0, Reciprocal) \ 1.32 + V(reciprocalSqrt, (Func<Float32x4, RecSqrt<float, Float32x4>, Float32x4>), 1, 0, ReciprocalSqrt) \ 1.33 + V(splat, (FuncSplat<Float32x4>), 1, 0, Splat) \ 1.34 + V(sqrt, (Func<Float32x4, Sqrt<float, Float32x4>, Float32x4>), 1, 0, Sqrt) \ 1.35 + V(toInt32x4, (FuncConvert<Float32x4, Int32x4>), 1, 0, ToInt32x4) 1.36 + 1.37 +#define FLOAT32X4_BINARY_FUNCTION_LIST(V) \ 1.38 + V(add, (Func<Float32x4, Add<float, Float32x4>, Float32x4>), 2, 0, Add) \ 1.39 + V(div, (Func<Float32x4, Div<float, Float32x4>, Float32x4>), 2, 0, Div) \ 1.40 + V(equal, (Func<Float32x4, Equal<float, Int32x4>, Int32x4>), 2, 0, Equal) \ 1.41 + V(greaterThan, (Func<Float32x4, GreaterThan<float, Int32x4>, Int32x4>), 2, 0, GreaterThan) \ 1.42 + V(greaterThanOrEqual, (Func<Float32x4, GreaterThanOrEqual<float, Int32x4>, Int32x4>), 2, 0, GreaterThanOrEqual) \ 1.43 + V(lessThan, (Func<Float32x4, LessThan<float, Int32x4>, Int32x4>), 2, 0, LessThan) \ 1.44 + V(lessThanOrEqual, (Func<Float32x4, LessThanOrEqual<float, Int32x4>, Int32x4>), 2, 0, LessThanOrEqual) \ 1.45 + V(max, (Func<Float32x4, Maximum<float, Float32x4>, Float32x4>), 2, 0, Max) \ 1.46 + V(min, (Func<Float32x4, Minimum<float, Float32x4>, Float32x4>), 2, 0, Min) \ 1.47 + V(mul, (Func<Float32x4, Mul<float, Float32x4>, Float32x4>), 2, 0, Mul) \ 1.48 + V(notEqual, (Func<Float32x4, NotEqual<float, Int32x4>, Int32x4>), 2, 0, NotEqual) \ 1.49 + V(shuffle, (FuncShuffle<Float32x4, Shuffle<float, Float32x4>, Float32x4>), 2, 0, Shuffle) \ 1.50 + V(scale, (FuncWith<Float32x4, Scale<float, Float32x4>, Float32x4>), 2, 0, Scale) \ 1.51 + V(sub, (Func<Float32x4, Sub<float, Float32x4>, Float32x4>), 2, 0, Sub) \ 1.52 + V(withX, (FuncWith<Float32x4, WithX<float, Float32x4>, Float32x4>), 2, 0, WithX) \ 1.53 + V(withY, (FuncWith<Float32x4, WithY<float, Float32x4>, Float32x4>), 2, 0, WithY) \ 1.54 + V(withZ, (FuncWith<Float32x4, WithZ<float, Float32x4>, Float32x4>), 2, 0, WithZ) \ 1.55 + V(withW, (FuncWith<Float32x4, WithW<float, Float32x4>, Float32x4>), 2, 0, WithW) 1.56 + 1.57 +#define FLOAT32X4_TERNARY_FUNCTION_LIST(V) \ 1.58 + V(clamp, Float32x4Clamp, 3, 0, Clamp) \ 1.59 + V(shuffleMix, (FuncShuffle<Float32x4, Shuffle<float, Float32x4>, Float32x4>), 3, 0, ShuffleMix) 1.60 + 1.61 +#define FLOAT32X4_FUNCTION_LIST(V) \ 1.62 + FLOAT32X4_NULLARY_FUNCTION_LIST(V) \ 1.63 + FLOAT32X4_UNARY_FUNCTION_LIST(V) \ 1.64 + FLOAT32X4_BINARY_FUNCTION_LIST(V) \ 1.65 + FLOAT32X4_TERNARY_FUNCTION_LIST(V) 1.66 + 1.67 +#define INT32X4_NULLARY_FUNCTION_LIST(V) \ 1.68 + V(zero, (FuncZero<Int32x4>), 0, 0, Zero) 1.69 + 1.70 +#define INT32X4_UNARY_FUNCTION_LIST(V) \ 1.71 + V(bitsToFloat32x4, (FuncConvertBits<Int32x4, Float32x4>), 1, 0, BitsToFloat32x4) \ 1.72 + V(neg, (Func<Int32x4, Neg<int32_t, Int32x4>, Int32x4>), 1, 0, Neg) \ 1.73 + V(not, (Func<Int32x4, Not<int32_t, Int32x4>, Int32x4>), 1, 0, Not) \ 1.74 + V(splat, (FuncSplat<Int32x4>), 0, 0, Splat) \ 1.75 + V(toFloat32x4, (FuncConvert<Int32x4, Float32x4>), 1, 0, ToFloat32x4) 1.76 + 1.77 +#define INT32X4_BINARY_FUNCTION_LIST(V) \ 1.78 + V(add, (Func<Int32x4, Add<int32_t, Int32x4>, Int32x4>), 2, 0, Add) \ 1.79 + V(and, (Func<Int32x4, And<int32_t, Int32x4>, Int32x4>), 2, 0, And) \ 1.80 + V(mul, (Func<Int32x4, Mul<int32_t, Int32x4>, Int32x4>), 2, 0, Mul) \ 1.81 + V(or, (Func<Int32x4, Or<int32_t, Int32x4>, Int32x4>), 2, 0, Or) \ 1.82 + V(sub, (Func<Int32x4, Sub<int32_t, Int32x4>, Int32x4>), 2, 0, Sub) \ 1.83 + V(shuffle, (FuncShuffle<Int32x4, Shuffle<int32_t, Int32x4>, Int32x4>), 2, 0, Shuffle) \ 1.84 + V(withFlagX, (FuncWith<Int32x4, WithFlagX<int32_t, Int32x4>, Int32x4>), 2, 0, WithFlagX) \ 1.85 + V(withFlagY, (FuncWith<Int32x4, WithFlagY<int32_t, Int32x4>, Int32x4>), 2, 0, WithFlagY) \ 1.86 + V(withFlagZ, (FuncWith<Int32x4, WithFlagZ<int32_t, Int32x4>, Int32x4>), 2, 0, WithFlagZ) \ 1.87 + V(withFlagW, (FuncWith<Int32x4, WithFlagW<int32_t, Int32x4>, Int32x4>), 2, 0, WithFlagW) \ 1.88 + V(withX, (FuncWith<Int32x4, WithX<int32_t, Int32x4>, Int32x4>), 2, 0, WithX) \ 1.89 + V(withY, (FuncWith<Int32x4, WithY<int32_t, Int32x4>, Int32x4>), 2, 0, WithY) \ 1.90 + V(withZ, (FuncWith<Int32x4, WithZ<int32_t, Int32x4>, Int32x4>), 2, 0, WithZ) \ 1.91 + V(withW, (FuncWith<Int32x4, WithW<int32_t, Int32x4>, Int32x4>), 2, 0, WithW) \ 1.92 + V(xor, (Func<Int32x4, Xor<int32_t, Int32x4>, Int32x4>), 2, 0, Xor) 1.93 + 1.94 +#define INT32X4_TERNARY_FUNCTION_LIST(V) \ 1.95 + V(select, Int32x4Select, 3, 0, Select) \ 1.96 + V(shuffleMix, (FuncShuffle<Int32x4, Shuffle<int32_t, Int32x4>, Int32x4>), 3, 0, ShuffleMix) 1.97 + 1.98 +#define INT32X4_QUARTERNARY_FUNCTION_LIST(V) \ 1.99 + V(bool, Int32x4Bool, 4, 0, Bool) 1.100 + 1.101 +#define INT32X4_FUNCTION_LIST(V) \ 1.102 + INT32X4_NULLARY_FUNCTION_LIST(V) \ 1.103 + INT32X4_UNARY_FUNCTION_LIST(V) \ 1.104 + INT32X4_BINARY_FUNCTION_LIST(V) \ 1.105 + INT32X4_TERNARY_FUNCTION_LIST(V) \ 1.106 + INT32X4_QUARTERNARY_FUNCTION_LIST(V) 1.107 + 1.108 +namespace js { 1.109 + 1.110 +class SIMDObject : public JSObject 1.111 +{ 1.112 + public: 1.113 + static const Class class_; 1.114 + static JSObject* initClass(JSContext *cx, Handle<GlobalObject *> global); 1.115 + static bool toString(JSContext *cx, unsigned int argc, jsval *vp); 1.116 +}; 1.117 + 1.118 +// These classes exist for use with templates below. 1.119 + 1.120 +struct Float32x4 { 1.121 + typedef float Elem; 1.122 + static const int32_t lanes = 4; 1.123 + static const X4TypeDescr::Type type = X4TypeDescr::TYPE_FLOAT32; 1.124 + 1.125 + static TypeDescr &GetTypeDescr(GlobalObject &global) { 1.126 + return global.float32x4TypeDescr().as<TypeDescr>(); 1.127 + } 1.128 + static Elem toType(Elem a) { 1.129 + return a; 1.130 + } 1.131 + static bool toType(JSContext *cx, JS::HandleValue v, Elem *out) { 1.132 + *out = v.toNumber(); 1.133 + return true; 1.134 + } 1.135 + static void setReturn(CallArgs &args, Elem value) { 1.136 + args.rval().setDouble(JS::CanonicalizeNaN(value)); 1.137 + } 1.138 +}; 1.139 + 1.140 +struct Int32x4 { 1.141 + typedef int32_t Elem; 1.142 + static const int32_t lanes = 4; 1.143 + static const X4TypeDescr::Type type = X4TypeDescr::TYPE_INT32; 1.144 + 1.145 + static TypeDescr &GetTypeDescr(GlobalObject &global) { 1.146 + return global.int32x4TypeDescr().as<TypeDescr>(); 1.147 + } 1.148 + static Elem toType(Elem a) { 1.149 + return ToInt32(a); 1.150 + } 1.151 + static bool toType(JSContext *cx, JS::HandleValue v, Elem *out) { 1.152 + return ToInt32(cx, v, out); 1.153 + } 1.154 + static void setReturn(CallArgs &args, Elem value) { 1.155 + args.rval().setInt32(value); 1.156 + } 1.157 +}; 1.158 + 1.159 +template<typename V> 1.160 +JSObject *Create(JSContext *cx, typename V::Elem *data); 1.161 + 1.162 +#define DECLARE_SIMD_FLOAT32X4_FUNCTION(Name, Func, Operands, Flags, MIRId) \ 1.163 +extern bool \ 1.164 +simd_float32x4_##Name(JSContext *cx, unsigned argc, Value *vp); 1.165 +FLOAT32X4_FUNCTION_LIST(DECLARE_SIMD_FLOAT32X4_FUNCTION) 1.166 +#undef DECLARE_SIMD_FLOAT32X4_FUNCTION 1.167 + 1.168 +#define DECLARE_SIMD_INT32x4_FUNCTION(Name, Func, Operands, Flags, MIRId) \ 1.169 +extern bool \ 1.170 +simd_int32x4_##Name(JSContext *cx, unsigned argc, Value *vp); 1.171 +INT32X4_FUNCTION_LIST(DECLARE_SIMD_INT32x4_FUNCTION) 1.172 +#undef DECLARE_SIMD_INT32x4_FUNCTION 1.173 + 1.174 +} /* namespace js */ 1.175 + 1.176 +JSObject * 1.177 +js_InitSIMDClass(JSContext *cx, js::HandleObject obj); 1.178 + 1.179 +#endif /* builtin_SIMD_h */