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