blob: 6e4041fcea38a45f337e77be9212934fa8b1f04d [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 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070024static jlong doubleToLongBits(JNIEnv* env __attribute__ ((unused)), jclass clazz __attribute__ ((unused)), jdouble val)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080025{
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 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070042static jlong doubleToRawLongBits(JNIEnv* env __attribute__ ((unused)), jclass clazz __attribute__ ((unused)), jdouble val)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080043{
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 */
Brian Carlstrom44e0e562010-05-06 23:44:16 -070054static jdouble longBitsToDouble(JNIEnv* env __attribute__ ((unused)), jclass clazz __attribute__ ((unused)), jlong val)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080055{
56 Double d;
57
58 d.bits = val;
59
60 return d.d;
61}
62
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080063static JNINativeMethod gMethods[] = {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080064 { "doubleToLongBits", "(D)J", doubleToLongBits },
65 { "doubleToRawLongBits", "(D)J", doubleToRawLongBits },
66 { "longBitsToDouble", "(J)D", longBitsToDouble },
67};
Elliott Hughesc08f9fb2010-04-16 17:44:12 -070068int register_java_lang_Double(JNIEnv* env) {
69 return jniRegisterNativeMethods(env, "java/lang/Double", gMethods, NELEM(gMethods));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080070}