diff options
author | Brian Carlstrom <bdc@google.com> | 2013-06-08 00:15:07 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-06-08 00:15:07 +0000 |
commit | f47f0bdff8807ff419fb3553e510abaa36a3f98b (patch) | |
tree | 44a9616f85957f29ed1e4c489528a4edcf3b4b60 /luni | |
parent | 30abb0bd592702a4b8c26ee18565c3c7625220c8 (diff) | |
parent | 3443a5e4e7ea9fa3fdc3495a1f2c44bea97ac100 (diff) | |
download | libcore-f47f0bdff8807ff419fb3553e510abaa36a3f98b.zip libcore-f47f0bdff8807ff419fb3553e510abaa36a3f98b.tar.gz libcore-f47f0bdff8807ff419fb3553e510abaa36a3f98b.tar.bz2 |
Merge "Offer default implementations of various dalvik intrinsics in the library."
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/main/java/java/lang/Math.java | 24 | ||||
-rw-r--r-- | luni/src/main/native/Register.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/java_lang_Double.cpp | 60 | ||||
-rw-r--r-- | luni/src/main/native/java_lang_Float.cpp | 59 | ||||
-rw-r--r-- | luni/src/main/native/java_lang_Math.cpp | 15 | ||||
-rw-r--r-- | luni/src/main/native/sub.mk | 2 |
6 files changed, 156 insertions, 6 deletions
diff --git a/luni/src/main/java/java/lang/Math.java b/luni/src/main/java/java/lang/Math.java index f8d22ed..86df784 100644 --- a/luni/src/main/java/java/lang/Math.java +++ b/luni/src/main/java/java/lang/Math.java @@ -54,7 +54,9 @@ public final class Math { * <li>{@code abs(NaN) = NaN}</li> * </ul> */ - public static native double abs(double d); + public static double abs(double d) { + return Double.longBitsToDouble(Double.doubleToRawLongBits(d) & 0x7fffffffffffffffL); + } /** * Returns the absolute value of the argument. @@ -67,7 +69,9 @@ public final class Math { * <li>{@code abs(NaN) = NaN}</li> * </ul> */ - public static native float abs(float f); + public static float abs(float f) { + return Float.intBitsToFloat(Float.floatToRawIntBits(f) & 0x7fffffff); + } /** * Returns the absolute value of the argument. @@ -75,13 +79,17 @@ public final class Math { * If the argument is {@code Integer.MIN_VALUE}, {@code Integer.MIN_VALUE} * is returned. */ - public static native int abs(int i); + public static int abs(int i) { + return (i >= 0) ? i : -i; + } /** * Returns the absolute value of the argument. If the argument is {@code * Long.MIN_VALUE}, {@code Long.MIN_VALUE} is returned. */ - public static native long abs(long l); + public static long abs(long l) { + return (l >= 0) ? l : -l; + } /** * Returns the closest double approximation of the arc cosine of the @@ -495,7 +503,9 @@ public final class Math { * Returns the most positive (closest to positive infinity) of the two * arguments. */ - public static native int max(int i1, int i2); + public static int max(int i1, int i2) { + return i1 > i2 ? i1 : i2; + } /** * Returns the most positive (closest to positive infinity) of the two @@ -571,7 +581,9 @@ public final class Math { * Returns the most negative (closest to negative infinity) of the two * arguments. */ - public static native int min(int i1, int i2); + public static int min(int i1, int i2) { + return i1 < i2 ? i1 : i2; + } /** * Returns the most negative (closest to negative infinity) of the two diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp index 8418ef3..1357c2f 100644 --- a/luni/src/main/native/Register.cpp +++ b/luni/src/main/native/Register.cpp @@ -36,6 +36,8 @@ int JNI_OnLoad(JavaVM* vm, void*) { REGISTER(register_java_io_File); REGISTER(register_java_io_ObjectStreamClass); REGISTER(register_java_lang_Character); + REGISTER(register_java_lang_Double); + REGISTER(register_java_lang_Float); REGISTER(register_java_lang_Math); REGISTER(register_java_lang_ProcessManager); REGISTER(register_java_lang_RealToString); diff --git a/luni/src/main/native/java_lang_Double.cpp b/luni/src/main/native/java_lang_Double.cpp new file mode 100644 index 0000000..259be30 --- /dev/null +++ b/luni/src/main/native/java_lang_Double.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "Double" + +#include "JNIHelp.h" +#include "JniConstants.h" + +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> + +union Double { + uint64_t bits; + double d; +}; + +static const jlong NaN = 0x7ff8000000000000ULL; + +static jlong Double_doubleToLongBits(JNIEnv*, jclass, jdouble val) { + Double d; + d.d = val; + // For this method all values in the NaN range are normalized to the canonical NaN value. + return isnan(d.d) ? NaN : d.bits; +} + +static jlong Double_doubleToRawLongBits(JNIEnv*, jclass, jdouble val) { + Double d; + d.d = val; + return d.bits; +} + +static jdouble Double_longBitsToDouble(JNIEnv*, jclass, jlong val) { + Double d; + d.bits = val; + return d.d; +} + +static JNINativeMethod gMethods[] = { + NATIVE_METHOD(Double, doubleToLongBits, "(D)J"), + NATIVE_METHOD(Double, doubleToRawLongBits, "(D)J"), + NATIVE_METHOD(Double, longBitsToDouble, "(J)D"), +}; +int register_java_lang_Double(JNIEnv* env) { + return jniRegisterNativeMethods(env, "java/lang/Double", gMethods, NELEM(gMethods)); +} diff --git a/luni/src/main/native/java_lang_Float.cpp b/luni/src/main/native/java_lang_Float.cpp new file mode 100644 index 0000000..59544db --- /dev/null +++ b/luni/src/main/native/java_lang_Float.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "Float" + +#include "JNIHelp.h" +#include "JniConstants.h" + +#include <math.h> +#include <stdlib.h> +#include <stdio.h> + +union Float { + unsigned int bits; + float f; +}; + +static const jint NaN = 0x7fc00000; + +static jint Float_floatToIntBits(JNIEnv*, jclass, jfloat val) { + Float f; + f.f = val; + // For this method all values in the NaN range are normalized to the canonical NaN value. + return isnanf(f.f) ? NaN : f.bits; +} + +jint Float_floatToRawIntBits(JNIEnv*, jclass, jfloat val) { + Float f; + f.f = val; + return f.bits; +} + +jfloat Float_intBitsToFloat(JNIEnv*, jclass, jint val) { + Float f; + f.bits = val; + return f.f; +} + +static JNINativeMethod gMethods[] = { + NATIVE_METHOD(Float, floatToIntBits, "(F)I"), + NATIVE_METHOD(Float, floatToRawIntBits, "(F)I"), + NATIVE_METHOD(Float, intBitsToFloat, "(I)F"), +}; +int register_java_lang_Float(JNIEnv* env) { + return jniRegisterNativeMethods(env, "java/lang/Float", gMethods, NELEM(gMethods)); +} diff --git a/luni/src/main/native/java_lang_Math.cpp b/luni/src/main/native/java_lang_Math.cpp index 273820e..784b84d 100644 --- a/luni/src/main/native/java_lang_Math.cpp +++ b/luni/src/main/native/java_lang_Math.cpp @@ -23,6 +23,14 @@ #include <stdlib.h> #include <math.h> +static jdouble Math_sin(JNIEnv*, jclass, jdouble a) { + return sin(a); +} + +static jdouble Math_cos(JNIEnv*, jclass, jdouble a) { + return cos(a); +} + static jdouble Math_tan(JNIEnv*, jclass, jdouble a) { return tan(a); } @@ -91,6 +99,10 @@ static jdouble Math_cbrt(JNIEnv*, jclass, jdouble a) { return cbrt(a); } +static jdouble Math_sqrt(JNIEnv*, jclass, jdouble a) { + return sqrt(a); +} + static jdouble Math_expm1(JNIEnv*, jclass, jdouble a) { return expm1(a); } @@ -115,6 +127,7 @@ static JNINativeMethod gMethods[] = { NATIVE_METHOD(Math, atan2, "!(DD)D"), NATIVE_METHOD(Math, cbrt, "!(D)D"), NATIVE_METHOD(Math, ceil, "!(D)D"), + NATIVE_METHOD(Math, cos, "!(D)D"), NATIVE_METHOD(Math, cosh, "!(D)D"), NATIVE_METHOD(Math, exp, "!(D)D"), NATIVE_METHOD(Math, expm1, "!(D)D"), @@ -126,7 +139,9 @@ static JNINativeMethod gMethods[] = { NATIVE_METHOD(Math, nextafter, "!(DD)D"), NATIVE_METHOD(Math, pow, "!(DD)D"), NATIVE_METHOD(Math, rint, "!(D)D"), + NATIVE_METHOD(Math, sin, "!(D)D"), NATIVE_METHOD(Math, sinh, "!(D)D"), + NATIVE_METHOD(Math, sqrt, "!(D)D"), NATIVE_METHOD(Math, tan, "!(D)D"), NATIVE_METHOD(Math, tanh, "!(D)D"), }; diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk index 7d75804..59aebfe 100644 --- a/luni/src/main/native/sub.mk +++ b/luni/src/main/native/sub.mk @@ -15,6 +15,8 @@ LOCAL_SRC_FILES := \ java_io_File.cpp \ java_io_ObjectStreamClass.cpp \ java_lang_Character.cpp \ + java_lang_Double.cpp \ + java_lang_Float.cpp \ java_lang_Math.cpp \ java_lang_ProcessManager.cpp \ java_lang_RealToString.cpp \ |