blob: fd4b7f14af5ef893386421179f05f218aa8a2757 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001//
2// java_lang_Double.c
3// Android
4//
5// Copyright 2005 The Android Open Source Project
6//
7#include "JNIHelp.h"
8
9#include <math.h>
10#include <stdlib.h>
11#include <stdio.h>
12#include <stdint.h>
13
14typedef union {
15 uint64_t bits;
16 double d;
17} Double;
18
19#define NaN (0x7ff8000000000000ULL)
20
21/*
22 * public static native long doubleToLongBits(double value)
23 */
24static jlong doubleToLongBits(JNIEnv* env, jclass clazz, jdouble val)
25{
26 Double d;
27
28 d.d = val;
29
30 // For this method all values in the NaN range are
31 // normalized to the canonical NaN value.
32
33 if (isnan(d.d))
34 d.bits = NaN;
35
36 return d.bits;
37}
38
39/*
40 * public static native long doubleToRawLongBits(double value)
41 */
42static jlong doubleToRawLongBits(JNIEnv* env, jclass clazz, jdouble val)
43{
44 Double d;
45
46 d.d = val;
47
48 return d.bits;
49}
50
51/*
52 * public static native double longBitsToDouble(long bits)
53 */
54static jdouble longBitsToDouble(JNIEnv* env, jclass clazz, jlong val)
55{
56 Double d;
57
58 d.bits = val;
59
60 return d.d;
61}
62
63/*
64 * JNI registration
65 */
66static JNINativeMethod gMethods[] = {
67 /* name, signature, funcPtr */
68 { "doubleToLongBits", "(D)J", doubleToLongBits },
69 { "doubleToRawLongBits", "(D)J", doubleToRawLongBits },
70 { "longBitsToDouble", "(J)D", longBitsToDouble },
71};
72int register_java_lang_Double(JNIEnv* env)
73{
74 return jniRegisterNativeMethods(env, "java/lang/Double",
75 gMethods, NELEM(gMethods));
76}
77