Andy McFadden | e9f54e6 | 2009-07-02 13:53:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | typedef int8_t s1; |
| 30 | typedef uint8_t u1; |
| 31 | typedef int16_t s2; |
| 32 | typedef uint16_t u2; |
| 33 | typedef int32_t s4; |
| 34 | typedef uint32_t u4; |
| 35 | typedef int64_t s8; |
| 36 | typedef uint64_t u8; |
| 37 | |
| 38 | s4 iadd32(s4 x, s4 y) { return x + y; } |
| 39 | s8 iadd64(s8 x, s8 y) { return x + y; } |
| 40 | float fadd32(float x, float y) { return x + y; } |
| 41 | double fadd64(double x, double y) { return x + y; } |
| 42 | |
| 43 | s4 isub32(s4 x, s4 y) { return x - y; } |
| 44 | s8 isub64(s8 x, s8 y) { return x - y; } |
| 45 | float fsub32(float x, float y) { return x - y; } |
| 46 | double fsub64(double x, double y) { return x - y; } |
| 47 | |
| 48 | s4 irsub32lit8(s4 x) { return 25 - x; } |
| 49 | |
| 50 | s4 imul32(s4 x, s4 y) { return x * y; } |
| 51 | s8 imul64(s8 x, s8 y) { return x * y; } |
| 52 | float fmul32(float x, float y) { return x * y; } |
| 53 | double fmul64(double x, double y) { return x * y; } |
| 54 | |
| 55 | s4 idiv32(s4 x, s4 y) { return x / y; } |
| 56 | s8 idiv64(s8 x, s8 y) { return x / y; } |
| 57 | float fdiv32(float x, float y) { return x / y; } |
| 58 | double fdiv64(double x, double y) { return x / y; } |
| 59 | |
| 60 | s4 irem32(s4 x, s4 y) { return x % y; } |
| 61 | s8 irem64(s8 x, s8 y) { return x % y; } |
| 62 | |
| 63 | s4 iand32(s4 x, s4 y) { return x & y; } |
| 64 | s8 iand64(s8 x, s8 y) { return x & y; } |
| 65 | |
| 66 | s4 ior32(s4 x, s4 y) { return x | y; } |
| 67 | s8 ior64(s8 x, s8 y) { return x | y; } |
| 68 | |
| 69 | s4 ixor32(s4 x, s4 y) { return x ^ y; } |
| 70 | s8 ixor64(s8 x, s8 y) { return x ^ y; } |
| 71 | |
| 72 | s4 iasl32(s4 x, s4 count) { return x << (count & 0x1f); } |
| 73 | s8 iasl64(s8 x, s4 count) { return x << (count & 0x3f); } |
| 74 | |
| 75 | s4 iasr32(s4 x, s4 count) { return x >> (count & 0x1f); } |
| 76 | s8 iasr64(s8 x, s4 count) { return x >> (count & 0x3f); } |
| 77 | |
| 78 | s4 ilsr32(s4 x, s4 count) { return ((u4)x) >> (count & 0x1f); } // unsigned |
| 79 | s8 ilsr64(s8 x, s4 count) { return ((u8)x) >> (count & 0x3f); } // unsigned |
| 80 | |
| 81 | s4 ineg32(s4 x) { return -x; } |
| 82 | s8 ineg64(s8 x) { return -x; } |
| 83 | float fneg32(float x) { return -x; } |
| 84 | double fneg64(double x) { return -x; } |
| 85 | |
| 86 | s4 inot32(s4 x) { return x ^ -1; } |
| 87 | s8 inot64(s8 x) { return x ^ -1LL; } |
| 88 | |
| 89 | s4 float2int(float x) { return (s4) x; } |
Andy McFadden | e9f54e6 | 2009-07-02 13:53:09 -0700 | [diff] [blame] | 90 | double float2double(float x) { return (double) x; } |
| 91 | s4 double2int(double x) { return (s4) x; } |
Andy McFadden | e9f54e6 | 2009-07-02 13:53:09 -0700 | [diff] [blame] | 92 | float double2float(double x) { return (float) x; } |
| 93 | |
Andy McFadden | d06170d | 2009-07-13 13:47:28 -0700 | [diff] [blame] | 94 | /* |
| 95 | * ARM lib doesn't clamp large values or NaN the way we want on these two. |
| 96 | * If the simple version isn't correct, use the long version. (You can use |
| 97 | * dalvik/tests/041-narrowing to verify.) |
| 98 | */ |
| 99 | s8 float2long(float x) { return (s8) x; } |
| 100 | s8 float2long_clamp(float x) |
| 101 | { |
| 102 | static const float kMaxLong = (float)0x7fffffffffffffffULL; |
| 103 | static const float kMinLong = (float)0x8000000000000000ULL; |
| 104 | |
| 105 | if (x >= kMaxLong) { |
| 106 | return 0x7fffffffffffffffULL; |
| 107 | } else if (x <= kMinLong) { |
| 108 | return 0x8000000000000000ULL; |
| 109 | } else if (x != x) { |
| 110 | return 0; |
| 111 | } else { |
| 112 | return (s8) x; |
| 113 | } |
| 114 | } |
| 115 | s8 double2long(double x) { return (s8) x; } |
| 116 | s8 double2long_clamp(double x) |
| 117 | { |
| 118 | static const double kMaxLong = (double)0x7fffffffffffffffULL; |
| 119 | static const double kMinLong = (double)0x8000000000000000ULL; |
| 120 | |
| 121 | if (x >= kMaxLong) { |
| 122 | return 0x7fffffffffffffffULL; |
| 123 | } else if (x <= kMinLong) { |
| 124 | return 0x8000000000000000ULL; |
| 125 | } else if (x != x) { |
| 126 | return 0; |
| 127 | } else { |
| 128 | return (s8) x; |
| 129 | } |
| 130 | } |
| 131 | |
Andy McFadden | e9f54e6 | 2009-07-02 13:53:09 -0700 | [diff] [blame] | 132 | s1 int2byte(s4 x) { return (s1) x; } |
| 133 | s2 int2short(s4 x) { return (s2) x; } |
| 134 | u2 int2char(s4 x) { return (u2) x; } |
| 135 | s8 int2long(s4 x) { return (s8) x; } |
| 136 | float int2float(s4 x) { return (float) x; } |
| 137 | double int2double(s4 x) { return (double) x; } |
| 138 | |
| 139 | s4 long2int(s8 x) { return (s4) x; } |
| 140 | float long2float(s8 x) { return (float) x; } |
| 141 | double long2double(s8 x) { return (double) x; } |
| 142 | |
| 143 | int cmpl_float(float x, float y) |
| 144 | { |
| 145 | int result; |
| 146 | |
| 147 | if (x == y) |
| 148 | result = 0; |
| 149 | else if (x > y) |
| 150 | result = 1; |
| 151 | else /* (x < y) or NaN */ |
| 152 | result = -1; |
| 153 | return result; |
| 154 | } |
| 155 | |
| 156 | int cmpg_float(float x, float y) |
| 157 | { |
| 158 | int result; |
| 159 | |
| 160 | if (x == y) |
| 161 | result = 0; |
| 162 | else if (x < y) |
| 163 | result = -1; |
| 164 | else /* (x > y) or NaN */ |
| 165 | result = 1; |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | int cmpl_double(double x, double y) |
| 170 | { |
| 171 | int result; |
| 172 | |
| 173 | if (x == y) |
| 174 | result = 0; |
| 175 | else if (x > y) |
| 176 | result = 1; |
| 177 | else /* (x < y) or NaN */ |
| 178 | result = -1; |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | int cmpg_double(double x, double y) |
| 183 | { |
| 184 | int result; |
| 185 | |
| 186 | if (x == y) |
| 187 | result = 0; |
| 188 | else if (x < y) |
| 189 | result = -1; |
| 190 | else /* (x > y) or NaN */ |
| 191 | result = 1; |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | int cmp_long(s8 x, s8 y) |
| 196 | { |
| 197 | int result; |
| 198 | |
| 199 | if (x == y) |
| 200 | result = 0; |
| 201 | else if (x < y) |
| 202 | result = -1; |
| 203 | else /* (x > y) */ |
| 204 | result = 1; |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | /* instruction decoding fragments */ |
| 209 | u1 unsignedAA(u2 x) { return x >> 8; } |
| 210 | s1 signedAA(u2 x) { return (s4)(x << 16) >> 24; } |
| 211 | s2 signedBB(u2 x) { return (s2) x; } |
| 212 | u1 unsignedA(u2 x) { return (x >> 8) & 0x0f; } |
| 213 | u1 unsignedB(u2 x) { return x >> 12; } |
| 214 | |
| 215 | /* some handy immediate constants when working with float/double */ |
| 216 | u4 const_43e00000(u4 highword) { return 0x43e00000; } |
| 217 | u4 const_c3e00000(u4 highword) { return 0xc3e00000; } |
| 218 | u4 const_ffc00000(u4 highword) { return 0xffc00000; } |
| 219 | u4 const_41dfffff(u4 highword) { return 0x41dfffff; } |
| 220 | u4 const_c1e00000(u4 highword) { return 0xc1e00000; } |
| 221 | |
| 222 | /* |
| 223 | * Test for some gcc-defined symbols. If you're frequently switching |
| 224 | * between different cross-compiler architectures or CPU feature sets, |
| 225 | * this can help you keep track of which one you're compiling for. |
| 226 | */ |
| 227 | #ifdef __arm__ |
| 228 | # warning "found __arm__" |
| 229 | #endif |
| 230 | #ifdef __ARM_EABI__ |
| 231 | # warning "found __ARM_EABI__" |
| 232 | #endif |
| 233 | #ifdef __VFP_FP__ |
| 234 | # warning "found __VFP_FP__" /* VFP-format doubles used; may not have VFP */ |
| 235 | #endif |
| 236 | #if defined(__VFP_FP__) && !defined(__SOFTFP__) |
| 237 | # warning "VFP in use" |
| 238 | #endif |
| 239 | #ifdef __ARM_ARCH_5TE__ |
| 240 | # warning "found __ARM_ARCH_5TE__" |
| 241 | #endif |
| 242 | #ifdef __ARM_ARCH_7A__ |
| 243 | # warning "found __ARM_ARCH_7A__" |
| 244 | #endif |