rsCpuRuntimeStubs.cpp revision 709a0978ae141198018ca9769f8d96292a8928e6
1/*
2 * Copyright (C) 2011-2012 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#include "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix4x4.h"
20#include "rsMatrix3x3.h"
21#include "rsMatrix2x2.h"
22#include "rsRuntime.h"
23
24#include "utils/Timers.h"
25#include "rsCpuCore.h"
26#include "rsCpuScript.h"
27
28#include <time.h>
29
30using namespace android;
31using namespace android::renderscript;
32
33typedef float float2 __attribute__((ext_vector_type(2)));
34typedef float float3 __attribute__((ext_vector_type(3)));
35typedef float float4 __attribute__((ext_vector_type(4)));
36typedef char char2 __attribute__((ext_vector_type(2)));
37typedef char char3 __attribute__((ext_vector_type(3)));
38typedef char char4 __attribute__((ext_vector_type(4)));
39typedef unsigned char uchar2 __attribute__((ext_vector_type(2)));
40typedef unsigned char uchar3 __attribute__((ext_vector_type(3)));
41typedef unsigned char uchar4 __attribute__((ext_vector_type(4)));
42typedef short short2 __attribute__((ext_vector_type(2)));
43typedef short short3 __attribute__((ext_vector_type(3)));
44typedef short short4 __attribute__((ext_vector_type(4)));
45typedef unsigned short ushort2 __attribute__((ext_vector_type(2)));
46typedef unsigned short ushort3 __attribute__((ext_vector_type(3)));
47typedef unsigned short ushort4 __attribute__((ext_vector_type(4)));
48typedef int32_t int2 __attribute__((ext_vector_type(2)));
49typedef int32_t int3 __attribute__((ext_vector_type(3)));
50typedef int32_t int4 __attribute__((ext_vector_type(4)));
51typedef uint32_t uint2 __attribute__((ext_vector_type(2)));
52typedef uint32_t uint3 __attribute__((ext_vector_type(3)));
53typedef uint32_t uint4 __attribute__((ext_vector_type(4)));
54typedef long long long2 __attribute__((ext_vector_type(2)));
55typedef long long long3 __attribute__((ext_vector_type(3)));
56typedef long long long4 __attribute__((ext_vector_type(4)));
57typedef unsigned long long ulong2 __attribute__((ext_vector_type(2)));
58typedef unsigned long long ulong3 __attribute__((ext_vector_type(3)));
59typedef unsigned long long ulong4 __attribute__((ext_vector_type(4)));
60
61
62//////////////////////////////////////////////////////////////////////////////
63// Message routines
64//////////////////////////////////////////////////////////////////////////////
65
66
67int SC_divsi3(int a, int b) {
68    return a / b;
69}
70
71int SC_modsi3(int a, int b) {
72    return a % b;
73}
74
75unsigned int SC_udivsi3(unsigned int a, unsigned int b) {
76    return a / b;
77}
78
79unsigned int SC_umodsi3(unsigned int a, unsigned int b) {
80    return a % b;
81}
82
83static void SC_debugF(const char *s, float f) {
84    ALOGD("%s %f, 0x%08x", s, f, *((int *) (&f)));
85}
86static void SC_debugFv2(const char *s, float f1, float f2) {
87    ALOGD("%s {%f, %f}", s, f1, f2);
88}
89static void SC_debugFv3(const char *s, float f1, float f2, float f3) {
90    ALOGD("%s {%f, %f, %f}", s, f1, f2, f3);
91}
92static void SC_debugFv4(const char *s, float f1, float f2, float f3, float f4) {
93    ALOGD("%s {%f, %f, %f, %f}", s, f1, f2, f3, f4);
94}
95static void SC_debugF2(const char *s, float2 f) {
96    ALOGD("%s {%f, %f}", s, f.x, f.y);
97}
98static void SC_debugF3(const char *s, float3 f) {
99    ALOGD("%s {%f, %f, %f}", s, f.x, f.y, f.z);
100}
101static void SC_debugF4(const char *s, float4 f) {
102    ALOGD("%s {%f, %f, %f, %f}", s, f.x, f.y, f.z, f.w);
103}
104static void SC_debugD(const char *s, double d) {
105    ALOGD("%s %f, 0x%08llx", s, d, *((long long *) (&d)));
106}
107static void SC_debugFM4v4(const char *s, const float *f) {
108    ALOGD("%s {%f, %f, %f, %f", s, f[0], f[4], f[8], f[12]);
109    ALOGD("%s  %f, %f, %f, %f", s, f[1], f[5], f[9], f[13]);
110    ALOGD("%s  %f, %f, %f, %f", s, f[2], f[6], f[10], f[14]);
111    ALOGD("%s  %f, %f, %f, %f}", s, f[3], f[7], f[11], f[15]);
112}
113static void SC_debugFM3v3(const char *s, const float *f) {
114    ALOGD("%s {%f, %f, %f", s, f[0], f[3], f[6]);
115    ALOGD("%s  %f, %f, %f", s, f[1], f[4], f[7]);
116    ALOGD("%s  %f, %f, %f}",s, f[2], f[5], f[8]);
117}
118static void SC_debugFM2v2(const char *s, const float *f) {
119    ALOGD("%s {%f, %f", s, f[0], f[2]);
120    ALOGD("%s  %f, %f}",s, f[1], f[3]);
121}
122static void SC_debugI8(const char *s, char c) {
123    ALOGD("%s %hhd  0x%hhx", s, c, (unsigned char)c);
124}
125static void SC_debugC2(const char *s, char2 c) {
126    ALOGD("%s {%hhd, %hhd}  0x%hhx 0x%hhx", s, c.x, c.y, (unsigned char)c.x, (unsigned char)c.y);
127}
128static void SC_debugC3(const char *s, char3 c) {
129    ALOGD("%s {%hhd, %hhd, %hhd}  0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, (unsigned char)c.x, (unsigned char)c.y, (unsigned char)c.z);
130}
131static void SC_debugC4(const char *s, char4 c) {
132    ALOGD("%s {%hhd, %hhd, %hhd, %hhd}  0x%hhx 0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.w, (unsigned char)c.x, (unsigned char)c.y, (unsigned char)c.z, (unsigned char)c.w);
133}
134static void SC_debugU8(const char *s, unsigned char c) {
135    ALOGD("%s %hhu  0x%hhx", s, c, c);
136}
137static void SC_debugUC2(const char *s, uchar2 c) {
138    ALOGD("%s {%hhu, %hhu}  0x%hhx 0x%hhx", s, c.x, c.y, c.x, c.y);
139}
140static void SC_debugUC3(const char *s, uchar3 c) {
141    ALOGD("%s {%hhu, %hhu, %hhu}  0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.x, c.y, c.z);
142}
143static void SC_debugUC4(const char *s, uchar4 c) {
144    ALOGD("%s {%hhu, %hhu, %hhu, %hhu}  0x%hhx 0x%hhx 0x%hhx 0x%hhx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
145}
146static void SC_debugI16(const char *s, short c) {
147    ALOGD("%s %hd  0x%hx", s, c, c);
148}
149static void SC_debugS2(const char *s, short2 c) {
150    ALOGD("%s {%hd, %hd}  0x%hx 0x%hx", s, c.x, c.y, c.x, c.y);
151}
152static void SC_debugS3(const char *s, short3 c) {
153    ALOGD("%s {%hd, %hd, %hd}  0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.x, c.y, c.z);
154}
155static void SC_debugS4(const char *s, short4 c) {
156    ALOGD("%s {%hd, %hd, %hd, %hd}  0x%hx 0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
157}
158static void SC_debugU16(const char *s, unsigned short c) {
159    ALOGD("%s %hu  0x%hx", s, c, c);
160}
161static void SC_debugUS2(const char *s, ushort2 c) {
162    ALOGD("%s {%hu, %hu}  0x%hx 0x%hx", s, c.x, c.y, c.x, c.y);
163}
164static void SC_debugUS3(const char *s, ushort3 c) {
165    ALOGD("%s {%hu, %hu, %hu}  0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.x, c.y, c.z);
166}
167static void SC_debugUS4(const char *s, ushort4 c) {
168    ALOGD("%s {%hu, %hu, %hu, %hu}  0x%hx 0x%hx 0x%hx 0x%hx", s, c.x, c.y, c.z, c.w, c.x, c.y, c.z, c.w);
169}
170static void SC_debugI32(const char *s, int32_t i) {
171    ALOGD("%s %d  0x%x", s, i, i);
172}
173static void SC_debugI2(const char *s, int2 i) {
174    ALOGD("%s {%d, %d}  0x%x 0x%x", s, i.x, i.y, i.x, i.y);
175}
176static void SC_debugI3(const char *s, int3 i) {
177    ALOGD("%s {%d, %d, %d}  0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.x, i.y, i.z);
178}
179static void SC_debugI4(const char *s, int4 i) {
180    ALOGD("%s {%d, %d, %d, %d}  0x%x 0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.w, i.x, i.y, i.z, i.w);
181}
182static void SC_debugU32(const char *s, uint32_t i) {
183    ALOGD("%s %u  0x%x", s, i, i);
184}
185static void SC_debugUI2(const char *s, uint2 i) {
186    ALOGD("%s {%u, %u}  0x%x 0x%x", s, i.x, i.y, i.x, i.y);
187}
188static void SC_debugUI3(const char *s, uint3 i) {
189    ALOGD("%s {%u, %u, %u}  0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.x, i.y, i.z);
190}
191static void SC_debugUI4(const char *s, uint4 i) {
192    ALOGD("%s {%u, %u, %u, %u}  0x%x 0x%x 0x%x 0x%x", s, i.x, i.y, i.z, i.w, i.x, i.y, i.z, i.w);
193}
194static void SC_debugLL64(const char *s, long long ll) {
195    ALOGD("%s %lld  0x%llx", s, ll, ll);
196}
197static void SC_debugL2(const char *s, long2 ll) {
198    ALOGD("%s {%lld, %lld}  0x%llx 0x%llx", s, ll.x, ll.y, ll.x, ll.y);
199}
200static void SC_debugL3(const char *s, long3 ll) {
201    ALOGD("%s {%lld, %lld, %lld}  0x%llx 0x%llx 0x%llx", s, ll.x, ll.y, ll.z, ll.x, ll.y, ll.z);
202}
203static void SC_debugL4(const char *s, long4 ll) {
204    ALOGD("%s {%lld, %lld, %lld, %lld}  0x%llx 0x%llx 0x%llx 0x%llx", s, ll.x, ll.y, ll.z, ll.w, ll.x, ll.y, ll.z, ll.w);
205}
206static void SC_debugULL64(const char *s, unsigned long long ll) {
207    ALOGD("%s %llu  0x%llx", s, ll, ll);
208}
209static void SC_debugUL2(const char *s, ulong2 ll) {
210    ALOGD("%s {%llu, %llu}  0x%llx 0x%llx", s, ll.x, ll.y, ll.x, ll.y);
211}
212static void SC_debugUL3(const char *s, ulong3 ll) {
213    ALOGD("%s {%llu, %llu, %llu}  0x%llx 0x%llx 0x%llx", s, ll.x, ll.y, ll.z, ll.x, ll.y, ll.z);
214}
215static void SC_debugUL4(const char *s, ulong4 ll) {
216    ALOGD("%s {%llu, %llu, %llu, %llu}  0x%llx 0x%llx 0x%llx 0x%llx", s, ll.x, ll.y, ll.z, ll.w, ll.x, ll.y, ll.z, ll.w);
217}
218static void SC_debugP(const char *s, const void *p) {
219    ALOGD("%s %p", s, p);
220}
221
222
223//////////////////////////////////////////////////////////////////////////////
224// Stub implementation
225//////////////////////////////////////////////////////////////////////////////
226
227// llvm name mangling ref
228//  <builtin-type> ::= v  # void
229//                 ::= b  # bool
230//                 ::= c  # char
231//                 ::= a  # signed char
232//                 ::= h  # unsigned char
233//                 ::= s  # short
234//                 ::= t  # unsigned short
235//                 ::= i  # int
236//                 ::= j  # unsigned int
237//                 ::= l  # long
238//                 ::= m  # unsigned long
239//                 ::= x  # long long, __int64
240//                 ::= y  # unsigned long long, __int64
241//                 ::= f  # float
242//                 ::= d  # double
243
244static RsdCpuReference::CpuSymbol gSyms[] = {
245    { "memset", (void *)&memset, true },
246    { "memcpy", (void *)&memcpy, true },
247
248    // Debug
249    { "_Z7rsDebugPKcf", (void *)&SC_debugF, true },
250    { "_Z7rsDebugPKcff", (void *)&SC_debugFv2, true },
251    { "_Z7rsDebugPKcfff", (void *)&SC_debugFv3, true },
252    { "_Z7rsDebugPKcffff", (void *)&SC_debugFv4, true },
253    { "_Z7rsDebugPKcDv2_f", (void *)&SC_debugF2, true },
254    { "_Z7rsDebugPKcDv3_f", (void *)&SC_debugF3, true },
255    { "_Z7rsDebugPKcDv4_f", (void *)&SC_debugF4, true },
256    { "_Z7rsDebugPKcd", (void *)&SC_debugD, true },
257    { "_Z7rsDebugPKcPK12rs_matrix4x4", (void *)&SC_debugFM4v4, true },
258    { "_Z7rsDebugPKcPK12rs_matrix3x3", (void *)&SC_debugFM3v3, true },
259    { "_Z7rsDebugPKcPK12rs_matrix2x2", (void *)&SC_debugFM2v2, true },
260    { "_Z7rsDebugPKcc", (void *)&SC_debugI8, true },
261    { "_Z7rsDebugPKcDv2_c", (void *)&SC_debugC2, true },
262    { "_Z7rsDebugPKcDv3_c", (void *)&SC_debugC3, true },
263    { "_Z7rsDebugPKcDv4_c", (void *)&SC_debugC4, true },
264    { "_Z7rsDebugPKch", (void *)&SC_debugU8, true },
265    { "_Z7rsDebugPKcDv2_h", (void *)&SC_debugUC2, true },
266    { "_Z7rsDebugPKcDv3_h", (void *)&SC_debugUC3, true },
267    { "_Z7rsDebugPKcDv4_h", (void *)&SC_debugUC4, true },
268    { "_Z7rsDebugPKcs", (void *)&SC_debugI16, true },
269    { "_Z7rsDebugPKcDv2_s", (void *)&SC_debugS2, true },
270    { "_Z7rsDebugPKcDv3_s", (void *)&SC_debugS3, true },
271    { "_Z7rsDebugPKcDv4_s", (void *)&SC_debugS4, true },
272    { "_Z7rsDebugPKct", (void *)&SC_debugU16, true },
273    { "_Z7rsDebugPKcDv2_t", (void *)&SC_debugUS2, true },
274    { "_Z7rsDebugPKcDv3_t", (void *)&SC_debugUS3, true },
275    { "_Z7rsDebugPKcDv4_t", (void *)&SC_debugUS4, true },
276    { "_Z7rsDebugPKci", (void *)&SC_debugI32, true },
277    { "_Z7rsDebugPKcDv2_i", (void *)&SC_debugI2, true },
278    { "_Z7rsDebugPKcDv3_i", (void *)&SC_debugI3, true },
279    { "_Z7rsDebugPKcDv4_i", (void *)&SC_debugI4, true },
280    { "_Z7rsDebugPKcj", (void *)&SC_debugU32, true },
281    { "_Z7rsDebugPKcDv2_j", (void *)&SC_debugUI2, true },
282    { "_Z7rsDebugPKcDv3_j", (void *)&SC_debugUI3, true },
283    { "_Z7rsDebugPKcDv4_j", (void *)&SC_debugUI4, true },
284    // Both "long" and "unsigned long" need to be redirected to their
285    // 64-bit counterparts, since we have hacked Slang to use 64-bit
286    // for "long" on Arm (to be similar to Java).
287    { "_Z7rsDebugPKcl", (void *)&SC_debugLL64, true },
288    { "_Z7rsDebugPKcDv2_l", (void *)&SC_debugL2, true },
289    { "_Z7rsDebugPKcDv3_l", (void *)&SC_debugL3, true },
290    { "_Z7rsDebugPKcDv4_l", (void *)&SC_debugL4, true },
291    { "_Z7rsDebugPKcm", (void *)&SC_debugULL64, true },
292    { "_Z7rsDebugPKcDv2_m", (void *)&SC_debugUL2, true },
293    { "_Z7rsDebugPKcDv3_m", (void *)&SC_debugUL3, true },
294    { "_Z7rsDebugPKcDv4_m", (void *)&SC_debugUL4, true },
295    { "_Z7rsDebugPKcx", (void *)&SC_debugLL64, true },
296    { "_Z7rsDebugPKcDv2_x", (void *)&SC_debugL2, true },
297    { "_Z7rsDebugPKcDv3_x", (void *)&SC_debugL3, true },
298    { "_Z7rsDebugPKcDv4_x", (void *)&SC_debugL4, true },
299    { "_Z7rsDebugPKcy", (void *)&SC_debugULL64, true },
300    { "_Z7rsDebugPKcDv2_y", (void *)&SC_debugUL2, true },
301    { "_Z7rsDebugPKcDv3_y", (void *)&SC_debugUL3, true },
302    { "_Z7rsDebugPKcDv4_y", (void *)&SC_debugUL4, true },
303    { "_Z7rsDebugPKcPKv", (void *)&SC_debugP, true },
304
305    { NULL, NULL, false }
306};
307
308
309void * RsdCpuScriptImpl::lookupRuntimeStub(void* pContext, char const* name) {
310    RsdCpuScriptImpl *s = (RsdCpuScriptImpl *)pContext;
311    const RsdCpuReference::CpuSymbol *syms = gSyms;
312    const RsdCpuReference::CpuSymbol *sym = NULL;
313
314    sym = s->mCtx->symLookup(name);
315    if (!sym) {
316        sym = s->lookupSymbolMath(name);
317    }
318    if (!sym) {
319        while (syms->fnPtr) {
320            if (!strcmp(syms->name, name)) {
321                sym = syms;
322            }
323            syms++;
324        }
325    }
326
327    if (sym) {
328        s->mIsThreadable &= sym->threadable;
329        return sym->fnPtr;
330    }
331    ALOGE("ScriptC sym lookup failed for %s", name);
332    return NULL;
333}
334
335
336