/rs/java/android/renderscript/Int4.java

https://github.com/aizuzi/platform_frameworks_base · Java · 514 lines · 256 code · 48 blank · 210 comment · 3 complexity · 191b7481f0212a883535c0eeca9c6305 MD5 · raw file

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