* Updated to jitcache-arm-x86 branch d4f6023b26c5+d1b5b1c106ac
[cacao.git] / src / native / vm / cldc1.1 / java_lang_Double.cpp
1 /* src/native/vm/cldc1.1/java_lang_Double.c
2
3    Copyright (C) 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/jni.hpp"
31 #include "native/native.hpp"
32
33 #if defined(ENABLE_JNI_HEADERS)
34 # include "native/include/java_lang_Double.h"
35 #endif
36
37 #include "vm/vm.hpp"
38
39 #include "vm/jit/builtin.hpp"
40
41
42 // Native functions are exported as C functions.
43 extern "C" {
44
45 /*
46  * Class:     java/lang/Double
47  * Method:    doubleToLongBits
48  * Signature: (D)J
49  */
50 JNIEXPORT jlong JNICALL Java_java_lang_Double_doubleToLongBits(JNIEnv *env, jclass clazz, jdouble doubleValue)
51 {
52         jvalue val;
53         s8  e, f;
54         val.d = doubleValue;
55
56 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
57         /* On little endian ARM processors when using FPA, word order of
58            doubles is still big endian. So take that into account here. When
59            using VFP, word order of doubles follows byte order. */
60
61 #define SWAP_DOUBLE(a)    (((a) << 32) | (((a) >> 32) & 0x00000000ffffffff))
62
63         val.j = SWAP_DOUBLE(val.j);
64 #endif
65
66         e = val.j & 0x7ff0000000000000LL;
67         f = val.j & 0x000fffffffffffffLL;
68
69         if (e == DBL_POSINF && f != 0L)
70                 val.j = DBL_NAN;
71
72         return val.j;
73 }
74
75
76 /*
77  * Class:     java/lang/Double
78  * Method:    longBitsToDouble
79  * Signature: (J)D
80  */
81 JNIEXPORT jdouble JNICALL Java_java_lang_Double_longBitsToDouble(JNIEnv *env, jclass clazz, jlong longValue)
82 {
83         jvalue val;
84         val.j = longValue;
85
86 #if defined(__IEEE_BYTES_LITTLE_ENDIAN)
87         val.j = SWAP_DOUBLE(val.j);
88 #endif
89
90         return val.d;
91 }
92
93 } // extern "C"
94
95
96 /* native methods implemented by this file ************************************/
97  
98 static JNINativeMethod methods[] = {
99         { (char*) "doubleToLongBits", (char*) "(D)J", (void*) (uintptr_t) &Java_java_lang_Double_doubleToLongBits },
100         { (char*) "longBitsToDouble", (char*) "(J)D", (void*) (uintptr_t) &Java_java_lang_Double_longBitsToDouble },
101 };
102  
103  
104 /* _Jv_java_lang_Double_init ***************************************************
105  
106    Register native functions.
107  
108 *******************************************************************************/
109
110 void _Jv_java_lang_Double_init(void)
111 {
112         utf* u = utf_new_char("java/lang/Double");
113  
114         NativeMethods& nm = VM::get_current()->get_nativemethods();
115         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
116 }
117
118
119 /*
120  * These are local overrides for various environment variables in Emacs.
121  * Please do not remove this and leave it at the end of the file, where
122  * Emacs will automagically detect them.
123  * ---------------------------------------------------------------------
124  * Local variables:
125  * mode: c++
126  * indent-tabs-mode: t
127  * c-basic-offset: 4
128  * tab-width: 4
129  * End:
130  */