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