1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19/**
20 * Vector version of the basic short type.
21 * Provides four short fields packed.
22 */
23public class Short4 {
24    public short x;
25    public short y;
26    public short z;
27    public short w;
28
29    public Short4() {
30    }
31
32    /** @hide */
33    public Short4(short i) {
34        this.x = this.y = this.z = this.w = i;
35    }
36
37    public Short4(short x, short y, short z, short w) {
38        this.x = x;
39        this.y = y;
40        this.z = z;
41        this.w = w;
42    }
43
44    /** @hide */
45    public Short4(Short4 source) {
46        this.x = source.x;
47        this.y = source.y;
48        this.z = source.z;
49        this.w = source.w;
50    }
51
52    /** @hide
53     * Vector add
54     *
55     * @param a
56     */
57    public void add(Short4 a) {
58        this.x += a.x;
59        this.y += a.y;
60        this.z += a.z;
61        this.w += a.w;
62    }
63
64    /** @hide
65     * Vector add
66     *
67     * @param a
68     * @param b
69     * @return
70     */
71    public static Short4 add(Short4 a, Short4 b) {
72        Short4 result = new Short4();
73        result.x = (short)(a.x + b.x);
74        result.y = (short)(a.y + b.y);
75        result.z = (short)(a.z + b.z);
76        result.w = (short)(a.w + b.w);
77
78        return result;
79    }
80
81    /** @hide
82     * Vector add
83     *
84     * @param value
85     */
86    public void add(short value) {
87        x += value;
88        y += value;
89        z += value;
90        w += value;
91    }
92
93    /** @hide
94     * Vector add
95     *
96     * @param a
97     * @param b
98     * @return
99     */
100    public static Short4 add(Short4 a, short b) {
101        Short4 result = new Short4();
102        result.x = (short)(a.x + b);
103        result.y = (short)(a.y + b);
104        result.z = (short)(a.z + b);
105        result.w = (short)(a.w + b);
106
107        return result;
108    }
109
110    /** @hide
111     * Vector subtraction
112     *
113     * @param a
114     */
115    public void sub(Short4 a) {
116        this.x -= a.x;
117        this.y -= a.y;
118        this.z -= a.z;
119        this.w -= a.w;
120    }
121
122    /** @hide
123     * Vector subtraction
124     *
125     * @param a
126     * @param b
127     * @return
128     */
129    public static Short4 sub(Short4 a, Short4 b) {
130        Short4 result = new Short4();
131        result.x = (short)(a.x - b.x);
132        result.y = (short)(a.y - b.y);
133        result.z = (short)(a.z - b.z);
134        result.w = (short)(a.w - b.w);
135
136        return result;
137    }
138
139    /** @hide
140     * Vector subtraction
141     *
142     * @param value
143     */
144    public void sub(short value) {
145        x -= value;
146        y -= value;
147        z -= value;
148        w -= value;
149    }
150
151    /** @hide
152     * Vector subtraction
153     *
154     * @param a
155     * @param b
156     * @return
157     */
158    public static Short4 sub(Short4 a, short b) {
159        Short4 result = new Short4();
160        result.x = (short)(a.x - b);
161        result.y = (short)(a.y - b);
162        result.z = (short)(a.z - b);
163        result.w = (short)(a.w - b);
164
165        return result;
166    }
167
168    /** @hide
169     * Vector multiplication
170     *
171     * @param a
172     */
173    public void mul(Short4 a) {
174        this.x *= a.x;
175        this.y *= a.y;
176        this.z *= a.z;
177        this.w *= a.w;
178    }
179
180    /** @hide
181     * Vector multiplication
182     *
183     * @param a
184     * @param b
185     * @return
186     */
187    public static Short4 mul(Short4 a, Short4 b) {
188        Short4 result = new Short4();
189        result.x = (short)(a.x * b.x);
190        result.y = (short)(a.y * b.y);
191        result.z = (short)(a.z * b.z);
192        result.w = (short)(a.w * b.w);
193
194        return result;
195    }
196
197    /** @hide
198     * Vector multiplication
199     *
200     * @param value
201     */
202    public void mul(short value) {
203        x *= value;
204        y *= value;
205        z *= value;
206        w *= value;
207    }
208
209    /** @hide
210     * Vector multiplication
211     *
212     * @param a
213     * @param b
214     * @return
215     */
216    public static Short4 mul(Short4 a, short b) {
217        Short4 result = new Short4();
218        result.x = (short)(a.x * b);
219        result.y = (short)(a.y * b);
220        result.z = (short)(a.z * b);
221        result.w = (short)(a.w * b);
222
223        return result;
224    }
225
226    /** @hide
227     * Vector division
228     *
229     * @param a
230     */
231    public void div(Short4 a) {
232        this.x /= a.x;
233        this.y /= a.y;
234        this.z /= a.z;
235        this.w /= a.w;
236    }
237
238    /** @hide
239     * Vector division
240     *
241     * @param a
242     * @param b
243     * @return
244     */
245    public static Short4 div(Short4 a, Short4 b) {
246        Short4 result = new Short4();
247        result.x = (short)(a.x / b.x);
248        result.y = (short)(a.y / b.y);
249        result.z = (short)(a.z / b.z);
250        result.w = (short)(a.w / b.w);
251
252        return result;
253    }
254
255    /** @hide
256     * Vector division
257     *
258     * @param value
259     */
260    public void div(short value) {
261        x /= value;
262        y /= value;
263        z /= value;
264        w /= value;
265    }
266
267    /** @hide
268     * Vector division
269     *
270     * @param a
271     * @param b
272     * @return
273     */
274    public static Short4 div(Short4 a, short b) {
275        Short4 result = new Short4();
276        result.x = (short)(a.x / b);
277        result.y = (short)(a.y / b);
278        result.z = (short)(a.z / b);
279        result.w = (short)(a.w / b);
280
281        return result;
282    }
283
284    /** @hide
285     * Vector Modulo
286     *
287     * @param a
288     */
289    public void mod(Short4 a) {
290        this.x %= a.x;
291        this.y %= a.y;
292        this.z %= a.z;
293        this.w %= a.w;
294    }
295
296    /** @hide
297     * Vector Modulo
298     *
299     * @param a
300     * @param b
301     * @return
302     */
303    public static Short4 mod(Short4 a, Short4 b) {
304        Short4 result = new Short4();
305        result.x = (short)(a.x % b.x);
306        result.y = (short)(a.y % b.y);
307        result.z = (short)(a.z % b.z);
308        result.w = (short)(a.w % b.w);
309
310        return result;
311    }
312
313    /** @hide
314     * Vector Modulo
315     *
316     * @param value
317     */
318    public void mod(short value) {
319        x %= value;
320        y %= value;
321        z %= value;
322        w %= value;
323    }
324
325    /** @hide
326     * Vector Modulo
327     *
328     * @param a
329     * @param b
330     * @return
331     */
332    public static Short4 mod(Short4 a, short b) {
333        Short4 result = new Short4();
334        result.x = (short)(a.x % b);
335        result.y = (short)(a.y % b);
336        result.z = (short)(a.z % b);
337        result.w = (short)(a.w % b);
338
339        return result;
340    }
341
342    /** @hide
343     * get vector length
344     *
345     * @return
346     */
347    public short length() {
348        return 4;
349    }
350
351    /** @hide
352     * set vector negate
353     */
354    public void negate() {
355        this.x = (short)(-x);
356        this.y = (short)(-y);
357        this.z = (short)(-z);
358        this.w = (short)(-w);
359    }
360
361    /** @hide
362     * Vector dot Product
363     *
364     * @param a
365     * @return
366     */
367    public short dotProduct(Short4 a) {
368        return (short)((x * a.x) + (y * a.y) + (z * a.z) + (w * a.w));
369    }
370
371    /** @hide
372     * Vector dot Product
373     *
374     * @param a
375     * @param b
376     * @return
377     */
378    public static short dotProduct(Short4 a, Short4 b) {
379        return (short)((b.x * a.x) + (b.y * a.y) + (b.z * a.z) + (b.w * a.w));
380    }
381
382    /** @hide
383     * Vector add Multiple
384     *
385     * @param a
386     * @param factor
387     */
388    public void addMultiple(Short4 a, short factor) {
389        x += a.x * factor;
390        y += a.y * factor;
391        z += a.z * factor;
392        w += a.w * factor;
393    }
394
395    /** @hide
396     * set vector value by Short4
397     *
398     * @param a
399     */
400    public void set(Short4 a) {
401        this.x = a.x;
402        this.y = a.y;
403        this.z = a.z;
404        this.w = a.w;
405    }
406
407    /** @hide
408     * set the vector field value by Short
409     *
410     * @param a
411     * @param b
412     * @param c
413     * @param d
414     */
415    public void setValues(short a, short b, short c, short d) {
416        this.x = a;
417        this.y = b;
418        this.z = c;
419        this.w = d;
420    }
421
422    /** @hide
423     * return the element sum of vector
424     *
425     * @return
426     */
427    public short elementSum() {
428        return (short)(x + y + z + w);
429    }
430
431    /** @hide
432     * get the vector field value by index
433     *
434     * @param i
435     * @return
436     */
437    public short get(int i) {
438        switch (i) {
439        case 0:
440            return (short)(x);
441        case 1:
442            return (short)(y);
443        case 2:
444            return (short)(z);
445        case 3:
446            return (short)(w);
447        default:
448            throw new IndexOutOfBoundsException("Index: i");
449        }
450    }
451
452    /** @hide
453     * set the vector field value by index
454     *
455     * @param i
456     * @param value
457     */
458    public void setAt(int i, short value) {
459        switch (i) {
460        case 0:
461            x = value;
462            return;
463        case 1:
464            y = value;
465            return;
466        case 2:
467            z = value;
468            return;
469        case 3:
470            w = value;
471            return;
472        default:
473            throw new IndexOutOfBoundsException("Index: i");
474        }
475    }
476
477    /** @hide
478     * add the vector field value by index
479     *
480     * @param i
481     * @param value
482     */
483    public void addAt(int i, short value) {
484        switch (i) {
485        case 0:
486            x += value;
487            return;
488        case 1:
489            y += value;
490            return;
491        case 2:
492            z += value;
493            return;
494        case 3:
495            w += value;
496            return;
497        default:
498            throw new IndexOutOfBoundsException("Index: i");
499        }
500    }
501
502    /** @hide
503     * copy the vector to short array
504     *
505     * @param data
506     * @param offset
507     */
508    public void copyTo(short[] data, int offset) {
509        data[offset] = (short)(x);
510        data[offset + 1] = (short)(y);
511        data[offset + 2] = (short)(z);
512        data[offset + 3] = (short)(w);
513    }
514}
515