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