1 /* 2 * Copyright (C) 2014 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 18 package android.renderscript.cts; 19 20 import android.renderscript.*; 21 22 public class VLoadTest extends RSBaseCompute { 23 24 private ScriptC_vload script; 25 private ScriptC_vload_relaxed scriptRelaxed; 26 Allocation walkAlloc; 27 Allocation inAlloc; 28 Allocation outAlloc; 29 private static java.util.Random random = new java.util.Random(); 30 31 final int w = 253; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 random.setSeed(10); 37 script = new ScriptC_vload(mRS); 38 scriptRelaxed = new ScriptC_vload_relaxed(mRS); 39 } 40 41 @Override 42 protected void tearDown() throws Exception { 43 if (walkAlloc != null) { 44 walkAlloc.destroy(); 45 } 46 if (inAlloc != null) { 47 inAlloc.destroy(); 48 } 49 if (outAlloc != null) { 50 outAlloc.destroy(); 51 } 52 script.destroy(); 53 scriptRelaxed.destroy(); 54 super.tearDown(); 55 } 56 57 protected void createWalk() { 58 int tmp[] = new int[w]; 59 boolean b[] = new boolean[w]; 60 int toCopy = w; 61 int i = 0; 62 63 while (toCopy > 0) { 64 int x = random.nextInt(w); 65 66 //android.util.Log.v("rs", "x " + x + ", y " + y + ", toCopy " + toCopy); 67 while ((x < w) && b[x]) { 68 x++; 69 if (x >= w) { 70 x = 0; 71 } 72 } 73 74 int maxsize = 1; 75 b[x] = true; 76 if ((x+1 < w) && !b[x+1]) { 77 maxsize ++; 78 b[x+1] = true; 79 if ((x+2 < w) && !b[x+2]) { 80 maxsize ++; 81 b[x+2] = true; 82 if ((x+3 < w) && !b[x+3]) { 83 maxsize ++; 84 b[x+3] = true; 85 } 86 } 87 } 88 89 toCopy -= maxsize; 90 tmp[i] = x | (maxsize << 16); 91 android.util.Log.v("rs", "x " + x + ", vec " + maxsize); 92 i++; 93 } 94 95 walkAlloc = Allocation.createSized(mRS, Element.I32(mRS), i); 96 walkAlloc.copy1DRangeFrom(0, i, tmp); 97 } 98 99 private void testSetup(Type t) { 100 createWalk(); 101 102 inAlloc = Allocation.createTyped(mRS, t); 103 outAlloc = Allocation.createTyped(mRS, t); 104 script.set_gAllocIn(inAlloc); 105 script.set_gAllocOut(outAlloc); 106 scriptRelaxed.set_gAllocIn(inAlloc); 107 scriptRelaxed.set_gAllocOut(outAlloc); 108 } 109 110 private void verify(byte[] a1, byte[] a2, String s) { 111 outAlloc.copyTo(a2); 112 for (int i=0; i < w; i++) { 113 if (a1[i] != a2[i]) { 114 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 115 } 116 a2[i] = 0; 117 } 118 outAlloc.copyFrom(a2); 119 } 120 121 private void verify(short[] a1, short[] a2, String s) { 122 outAlloc.copyTo(a2); 123 for (int i=0; i < w; i++) { 124 if (a1[i] != a2[i]) { 125 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 126 } 127 a2[i] = 0; 128 } 129 outAlloc.copyFrom(a2); 130 } 131 132 private void verify(int[] a1, int[] a2, String s) { 133 outAlloc.copyTo(a2); 134 for (int i=0; i < w; i++) { 135 if (a1[i] != a2[i]) { 136 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 137 } 138 a2[i] = 0; 139 } 140 outAlloc.copyFrom(a2); 141 } 142 143 private void verify(long[] a1, long[] a2, String s) { 144 outAlloc.copyTo(a2); 145 for (int i=0; i < w; i++) { 146 if (a1[i] != a2[i]) { 147 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 148 } 149 a2[i] = 0; 150 } 151 outAlloc.copyFrom(a2); 152 } 153 154 private void verify(float[] a1, float[] a2, String s) { 155 outAlloc.copyTo(a2); 156 for (int i=0; i < w; i++) { 157 if (a1[i] != a2[i]) { 158 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 159 } 160 a2[i] = 0; 161 } 162 outAlloc.copyFrom(a2); 163 } 164 165 private void verify(double[] a1, double[] a2, String s) { 166 outAlloc.copyTo(a2); 167 for (int i=0; i < w; i++) { 168 if (a1[i] != a2[i]) { 169 throw new RSRuntimeException(s + a1[i] + ", " + a2[i] + ", at " + i); 170 } 171 a2[i] = 0; 172 } 173 outAlloc.copyFrom(a2); 174 } 175 176 private byte[] randomByteArray(int len) { 177 byte t[] = new byte[len]; 178 random.nextBytes(t); 179 inAlloc.copyFrom(t); 180 return t; 181 } 182 183 private short[] randomShortArray(int len) { 184 short t[] = new short[len]; 185 for (int i = 0; i < t.length; i++) { 186 t[i] = (short)(random.nextInt() & 0xffff); 187 } 188 inAlloc.copyFrom(t); 189 return t; 190 } 191 192 private int[] randomIntArray(int len) { 193 int t[] = new int[len]; 194 for (int i = 0; i < t.length; i++) { 195 t[i] = random.nextInt(); 196 } 197 inAlloc.copyFrom(t); 198 return t; 199 } 200 201 private long[] randomLongArray(int len) { 202 long t[] = new long[len]; 203 for (int i = 0; i < t.length; i++) { 204 t[i] = random.nextLong(); 205 } 206 inAlloc.copyFrom(t); 207 return t; 208 } 209 210 public void testVload_char() { 211 testSetup(Type.createX(mRS, Element.I8(mRS), w)); 212 byte tmp[] = randomByteArray(w); 213 byte tmp2[] = new byte[w]; 214 script.forEach_copy2d_char(walkAlloc); 215 verify(tmp, tmp2, "Data mismatch char: "); 216 } 217 218 public void testVload_uchar() { 219 testSetup(Type.createX(mRS, Element.U8(mRS), w)); 220 byte tmp[] = randomByteArray(w); 221 byte tmp2[] = new byte[w]; 222 script.forEach_copy2d_uchar(walkAlloc); 223 verify(tmp, tmp2, "Data mismatch uchar: "); 224 } 225 226 public void testVload_char_relaxed() { 227 testSetup(Type.createX(mRS, Element.I8(mRS), w)); 228 byte tmp[] = randomByteArray(w); 229 byte tmp2[] = new byte[w]; 230 scriptRelaxed.forEach_copy2d_char(walkAlloc); 231 verify(tmp, tmp2, "Data mismatch relaxed char: "); 232 } 233 234 public void testVload_uchar_relaxed() { 235 testSetup(Type.createX(mRS, Element.U8(mRS), w)); 236 byte tmp[] = randomByteArray(w); 237 byte tmp2[] = new byte[w]; 238 scriptRelaxed.forEach_copy2d_uchar(walkAlloc); 239 verify(tmp, tmp2, "Data mismatch relaxed uchar: "); 240 } 241 242 public void testVload_short() { 243 testSetup(Type.createX(mRS, Element.I16(mRS), w)); 244 short tmp[] = randomShortArray(w); 245 short tmp2[] = new short[w]; 246 script.forEach_copy2d_short(walkAlloc); 247 verify(tmp, tmp2, "Data mismatch short: "); 248 } 249 250 public void testVload_ushort() { 251 testSetup(Type.createX(mRS, Element.U16(mRS), w)); 252 short tmp[] = randomShortArray(w); 253 short tmp2[] = new short[w]; 254 script.forEach_copy2d_ushort(walkAlloc); 255 verify(tmp, tmp2, "Data mismatch ushort: "); 256 } 257 258 public void testVload_short_relaxed() { 259 testSetup(Type.createX(mRS, Element.I16(mRS), w)); 260 short tmp[] = randomShortArray(w); 261 short tmp2[] = new short[w]; 262 scriptRelaxed.forEach_copy2d_short(walkAlloc); 263 verify(tmp, tmp2, "Data mismatch relaxed short: "); 264 } 265 266 public void testVload_ushort_relaxed() { 267 testSetup(Type.createX(mRS, Element.U16(mRS), w)); 268 short tmp[] = randomShortArray(w); 269 short tmp2[] = new short[w]; 270 scriptRelaxed.forEach_copy2d_ushort(walkAlloc); 271 verify(tmp, tmp2, "Data mismatch ushort: "); 272 } 273 274 public void testVload_int() { 275 testSetup(Type.createX(mRS, Element.I32(mRS), w)); 276 int tmp[] = randomIntArray(w); 277 int tmp2[] = new int[w]; 278 script.forEach_copy2d_int(walkAlloc); 279 verify(tmp, tmp2, "Data mismatch int: "); 280 } 281 282 public void testVload_uint() { 283 testSetup(Type.createX(mRS, Element.U32(mRS), w)); 284 int tmp[] = randomIntArray(w); 285 int tmp2[] = new int[w]; 286 script.forEach_copy2d_uint(walkAlloc); 287 verify(tmp, tmp2, "Data mismatch uint: "); 288 } 289 290 public void testVload_int_relaxed() { 291 testSetup(Type.createX(mRS, Element.I32(mRS), w)); 292 int tmp[] = randomIntArray(w); 293 int tmp2[] = new int[w]; 294 scriptRelaxed.forEach_copy2d_int(walkAlloc); 295 verify(tmp, tmp2, "Data mismatch relaxed int: "); 296 } 297 298 public void testVload_uint_relaxed() { 299 testSetup(Type.createX(mRS, Element.U32(mRS), w)); 300 int tmp[] = randomIntArray(w); 301 int tmp2[] = new int[w]; 302 scriptRelaxed.forEach_copy2d_uint(walkAlloc); 303 verify(tmp, tmp2, "Data mismatch uint: "); 304 } 305 306 public void testVload_long() { 307 testSetup(Type.createX(mRS, Element.I64(mRS), w)); 308 long tmp[] = randomLongArray(w); 309 long tmp2[] = new long[w]; 310 script.forEach_copy2d_long(walkAlloc); 311 verify(tmp, tmp2, "Data mismatch long: "); 312 } 313 314 public void testVload_ulong() { 315 testSetup(Type.createX(mRS, Element.U64(mRS), w)); 316 long tmp[] = randomLongArray(w); 317 long tmp2[] = new long[w]; 318 script.forEach_copy2d_ulong(walkAlloc); 319 verify(tmp, tmp2, "Data mismatch ulong: "); 320 } 321 public void testVload_long_relaxed() { 322 testSetup(Type.createX(mRS, Element.I64(mRS), w)); 323 long tmp[] = randomLongArray(w); 324 long tmp2[] = new long[w]; 325 scriptRelaxed.forEach_copy2d_long(walkAlloc); 326 verify(tmp, tmp2, "Data mismatch relaxed long: "); 327 } 328 public void testVload_ulong_relaxed() { 329 testSetup(Type.createX(mRS, Element.U64(mRS), w)); 330 long tmp[] = randomLongArray(w); 331 long tmp2[] = new long[w]; 332 scriptRelaxed.forEach_copy2d_ulong(walkAlloc); 333 verify(tmp, tmp2, "Data mismatch ulong: "); 334 } 335 336 public void testVload_float() { 337 testSetup(Type.createX(mRS, Element.F32(mRS), w)); 338 float tmp[] = new float[w]; 339 float tmp2[] = new float[w]; 340 for (int i=0; i < w; i++) { 341 tmp[i] = random.nextFloat(); 342 } 343 inAlloc.copyFrom(tmp); 344 script.forEach_copy2d_float(walkAlloc); 345 verify(tmp, tmp2, "Data mismatch float: "); 346 } 347 348 public void testVload_float_relaxed() { 349 testSetup(Type.createX(mRS, Element.F32(mRS), w)); 350 float tmp[] = new float[w]; 351 float tmp2[] = new float[w]; 352 for (int i=0; i < w; i++) { 353 tmp[i] = random.nextFloat(); 354 } 355 inAlloc.copyFrom(tmp); 356 scriptRelaxed.forEach_copy2d_float(walkAlloc); 357 verify(tmp, tmp2, "Data mismatch relaxed float: "); 358 } 359 360 public void testVload_double() { 361 testSetup(Type.createX(mRS, Element.F64(mRS), w)); 362 double tmp[] = new double[w]; 363 double tmp2[] = new double[w]; 364 for (int i=0; i < w; i++) { 365 tmp[i] = random.nextDouble(); 366 } 367 inAlloc.copyFrom(tmp); 368 script.forEach_copy2d_double(walkAlloc); 369 verify(tmp, tmp2, "Data mismatch double: "); 370 } 371 372 public void testVload_double_relaxed() { 373 testSetup(Type.createX(mRS, Element.F64(mRS), w)); 374 double tmp[] = new double[w]; 375 double tmp2[] = new double[w]; 376 for (int i=0; i < w; i++) { 377 tmp[i] = random.nextDouble(); 378 } 379 inAlloc.copyFrom(tmp); 380 scriptRelaxed.forEach_copy2d_double(walkAlloc); 381 verify(tmp, tmp2, "Data mismatch relaxed double: "); 382 } 383 384 } 385