blob: 62a914df020ab5e3f11ae6be2f452bdcfe47983e [file] [log] [blame]
Andy McFaddene9f54e62009-07-02 13:53:09 -07001/*
2 * Copyright (C) 2009 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 * Dalvik instruction fragments, useful when porting mterp.
19 *
20 * Compile this and examine the output to see what your compiler generates.
21 * This can give you a head start on some of the more complicated operations.
22 *
23 * Example:
24 * % gcc -c -O2 -save-temps -fverbose-asm porting-proto.c
25 * % less porting-proto.s
26 */
27#include <stdint.h>
28
29typedef int8_t s1;
30typedef uint8_t u1;
31typedef int16_t s2;
32typedef uint16_t u2;
33typedef int32_t s4;
34typedef uint32_t u4;
35typedef int64_t s8;
36typedef uint64_t u8;
37
38s4 iadd32(s4 x, s4 y) { return x + y; }
39s8 iadd64(s8 x, s8 y) { return x + y; }
40float fadd32(float x, float y) { return x + y; }
41double fadd64(double x, double y) { return x + y; }
42
43s4 isub32(s4 x, s4 y) { return x - y; }
44s8 isub64(s8 x, s8 y) { return x - y; }
45float fsub32(float x, float y) { return x - y; }
46double fsub64(double x, double y) { return x - y; }
47
48s4 irsub32lit8(s4 x) { return 25 - x; }
49
50s4 imul32(s4 x, s4 y) { return x * y; }
51s8 imul64(s8 x, s8 y) { return x * y; }
52float fmul32(float x, float y) { return x * y; }
53double fmul64(double x, double y) { return x * y; }
54
55s4 idiv32(s4 x, s4 y) { return x / y; }
56s8 idiv64(s8 x, s8 y) { return x / y; }
57float fdiv32(float x, float y) { return x / y; }
58double fdiv64(double x, double y) { return x / y; }
59
60s4 irem32(s4 x, s4 y) { return x % y; }
61s8 irem64(s8 x, s8 y) { return x % y; }
62
63s4 iand32(s4 x, s4 y) { return x & y; }
64s8 iand64(s8 x, s8 y) { return x & y; }
65
66s4 ior32(s4 x, s4 y) { return x | y; }
67s8 ior64(s8 x, s8 y) { return x | y; }
68
69s4 ixor32(s4 x, s4 y) { return x ^ y; }
70s8 ixor64(s8 x, s8 y) { return x ^ y; }
71
72s4 iasl32(s4 x, s4 count) { return x << (count & 0x1f); }
73s8 iasl64(s8 x, s4 count) { return x << (count & 0x3f); }
74
75s4 iasr32(s4 x, s4 count) { return x >> (count & 0x1f); }
76s8 iasr64(s8 x, s4 count) { return x >> (count & 0x3f); }
77
78s4 ilsr32(s4 x, s4 count) { return ((u4)x) >> (count & 0x1f); } // unsigned
79s8 ilsr64(s8 x, s4 count) { return ((u8)x) >> (count & 0x3f); } // unsigned
80
81s4 ineg32(s4 x) { return -x; }
82s8 ineg64(s8 x) { return -x; }
83float fneg32(float x) { return -x; }
84double fneg64(double x) { return -x; }
85
86s4 inot32(s4 x) { return x ^ -1; }
87s8 inot64(s8 x) { return x ^ -1LL; }
88
89s4 float2int(float x) { return (s4) x; }
90s8 float2long(float x) { return (s8) x; }
91double float2double(float x) { return (double) x; }
92s4 double2int(double x) { return (s4) x; }
93s8 double2long(double x) { return (s8) x; }
94float double2float(double x) { return (float) x; }
95
96s1 int2byte(s4 x) { return (s1) x; }
97s2 int2short(s4 x) { return (s2) x; }
98u2 int2char(s4 x) { return (u2) x; }
99s8 int2long(s4 x) { return (s8) x; }
100float int2float(s4 x) { return (float) x; }
101double int2double(s4 x) { return (double) x; }
102
103s4 long2int(s8 x) { return (s4) x; }
104float long2float(s8 x) { return (float) x; }
105double long2double(s8 x) { return (double) x; }
106
107int cmpl_float(float x, float y)
108{
109 int result;
110
111 if (x == y)
112 result = 0;
113 else if (x > y)
114 result = 1;
115 else /* (x < y) or NaN */
116 result = -1;
117 return result;
118}
119
120int cmpg_float(float x, float y)
121{
122 int result;
123
124 if (x == y)
125 result = 0;
126 else if (x < y)
127 result = -1;
128 else /* (x > y) or NaN */
129 result = 1;
130 return result;
131}
132
133int cmpl_double(double x, double y)
134{
135 int result;
136
137 if (x == y)
138 result = 0;
139 else if (x > y)
140 result = 1;
141 else /* (x < y) or NaN */
142 result = -1;
143 return result;
144}
145
146int cmpg_double(double x, double y)
147{
148 int result;
149
150 if (x == y)
151 result = 0;
152 else if (x < y)
153 result = -1;
154 else /* (x > y) or NaN */
155 result = 1;
156 return result;
157}
158
159int cmp_long(s8 x, s8 y)
160{
161 int result;
162
163 if (x == y)
164 result = 0;
165 else if (x < y)
166 result = -1;
167 else /* (x > y) */
168 result = 1;
169 return result;
170}
171
172/* instruction decoding fragments */
173u1 unsignedAA(u2 x) { return x >> 8; }
174s1 signedAA(u2 x) { return (s4)(x << 16) >> 24; }
175s2 signedBB(u2 x) { return (s2) x; }
176u1 unsignedA(u2 x) { return (x >> 8) & 0x0f; }
177u1 unsignedB(u2 x) { return x >> 12; }
178
179/* some handy immediate constants when working with float/double */
180u4 const_43e00000(u4 highword) { return 0x43e00000; }
181u4 const_c3e00000(u4 highword) { return 0xc3e00000; }
182u4 const_ffc00000(u4 highword) { return 0xffc00000; }
183u4 const_41dfffff(u4 highword) { return 0x41dfffff; }
184u4 const_c1e00000(u4 highword) { return 0xc1e00000; }
185
186/*
187 * Test for some gcc-defined symbols. If you're frequently switching
188 * between different cross-compiler architectures or CPU feature sets,
189 * this can help you keep track of which one you're compiling for.
190 */
191#ifdef __arm__
192# warning "found __arm__"
193#endif
194#ifdef __ARM_EABI__
195# warning "found __ARM_EABI__"
196#endif
197#ifdef __VFP_FP__
198# warning "found __VFP_FP__" /* VFP-format doubles used; may not have VFP */
199#endif
200#if defined(__VFP_FP__) && !defined(__SOFTFP__)
201# warning "VFP in use"
202#endif
203#ifdef __ARM_ARCH_5TE__
204# warning "found __ARM_ARCH_5TE__"
205#endif
206#ifdef __ARM_ARCH_7A__
207# warning "found __ARM_ARCH_7A__"
208#endif
209