Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2005 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 #define LOG_TAG "Float"
     18 
     19 #include "JNIHelp.h"
     20 #include "JniConstants.h"
     21 
     22 #include <math.h>
     23 #include <stdlib.h>
     24 #include <stdio.h>
     25 
     26 union Float {
     27     unsigned int bits;
     28     float f;
     29 };
     30 
     31 static const jint NaN = 0x7fc00000;
     32 
     33 static jint Float_floatToIntBits(JNIEnv*, jclass, jfloat val) {
     34     Float f;
     35     f.f = val;
     36     //  For this method all values in the NaN range are normalized to the canonical NaN value.
     37     return isnanf(f.f) ? NaN : f.bits;
     38 }
     39 
     40 jint Float_floatToRawIntBits(JNIEnv*, jclass, jfloat val) {
     41     Float f;
     42     f.f = val;
     43     return f.bits;
     44 }
     45 
     46 jfloat Float_intBitsToFloat(JNIEnv*, jclass, jint val) {
     47     Float f;
     48     f.bits = val;
     49     return f.f;
     50 }
     51 
     52 static JNINativeMethod gMethods[] = {
     53     NATIVE_METHOD(Float, floatToIntBits, "(F)I"),
     54     NATIVE_METHOD(Float, floatToRawIntBits, "(F)I"),
     55     NATIVE_METHOD(Float, intBitsToFloat, "(I)F"),
     56 };
     57 int register_java_lang_Float(JNIEnv* env) {
     58     return jniRegisterNativeMethods(env, "java/lang/Float", gMethods, NELEM(gMethods));
     59 }
     60