1 /*
2 * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "jni.h"
27 #include "jvm.h"
28 #include "jni_util.h"
29 #include "jlong.h"
30
31 #include "java_lang_Float.h"
32 #include "java_lang_Double.h"
33 #include "java_io_ObjectOutputStream.h"
34
35 /*
36 * Class: java_io_ObjectOutputStream
37 * Method: floatsToBytes
38 * Signature: ([FI[BII)V
39 *
40 * Convert nfloats float values to their byte representations. Float values
41 * are read from array src starting at offset srcpos and written to array
42 * dst starting at offset dstpos.
43 */
44 JNIEXPORT void JNICALL
45 Java_java_io_ObjectOutputStream_floatsToBytes(JNIEnv *env,
46 jclass this,
47 jfloatArray src,
48 jint srcpos,
49 jbyteArray dst,
71 floats = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
72 if (floats == NULL) /* exception thrown */
73 return;
74
75 /* fetch dest array */
76 if (dst == NULL) {
77 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
78 JNU_ThrowNullPointerException(env, NULL);
79 return;
80 }
81 bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
82 if (bytes == NULL) { /* exception thrown */
83 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
84 return;
85 }
86
87 /* do conversion */
88 srcend = srcpos + nfloats;
89 for ( ; srcpos < srcend; srcpos++) {
90 fval = (float) floats[srcpos];
91 if (JVM_IsNaN(fval)) { /* collapse NaNs */
92 ival = 0x7fc00000;
93 } else {
94 u.f = fval;
95 ival = (jint) u.i;
96 }
97 bytes[dstpos++] = (ival >> 24) & 0xFF;
98 bytes[dstpos++] = (ival >> 16) & 0xFF;
99 bytes[dstpos++] = (ival >> 8) & 0xFF;
100 bytes[dstpos++] = (ival >> 0) & 0xFF;
101 }
102
103 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
104 (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
105 }
106
107 /*
108 * Class: java_io_ObjectOutputStream
109 * Method: doublesToBytes
110 * Signature: ([DI[BII)V
111 *
143 doubles = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
144 if (doubles == NULL) /* exception thrown */
145 return;
146
147 /* fetch dest array */
148 if (dst == NULL) {
149 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
150 JNU_ThrowNullPointerException(env, NULL);
151 return;
152 }
153 bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
154 if (bytes == NULL) { /* exception thrown */
155 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
156 return;
157 }
158
159 /* do conversion */
160 srcend = srcpos + ndoubles;
161 for ( ; srcpos < srcend; srcpos++) {
162 dval = doubles[srcpos];
163 if (JVM_IsNaN((double) dval)) { /* collapse NaNs */
164 lval = jint_to_jlong(0x7ff80000);
165 lval = jlong_shl(lval, 32);
166 } else {
167 jdouble_to_jlong_bits(&dval);
168 u.d = (double) dval;
169 lval = u.l;
170 }
171 bytes[dstpos++] = (lval >> 56) & 0xFF;
172 bytes[dstpos++] = (lval >> 48) & 0xFF;
173 bytes[dstpos++] = (lval >> 40) & 0xFF;
174 bytes[dstpos++] = (lval >> 32) & 0xFF;
175 bytes[dstpos++] = (lval >> 24) & 0xFF;
176 bytes[dstpos++] = (lval >> 16) & 0xFF;
177 bytes[dstpos++] = (lval >> 8) & 0xFF;
178 bytes[dstpos++] = (lval >> 0) & 0xFF;
179 }
180
181 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
182 (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
183 }
|
1 /*
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "jni_util.h"
27 #include "jdk_util.h"
28
29 #include "java_lang_Float.h"
30 #include "java_lang_Double.h"
31 #include "java_io_ObjectOutputStream.h"
32
33 /*
34 * Class: java_io_ObjectOutputStream
35 * Method: floatsToBytes
36 * Signature: ([FI[BII)V
37 *
38 * Convert nfloats float values to their byte representations. Float values
39 * are read from array src starting at offset srcpos and written to array
40 * dst starting at offset dstpos.
41 */
42 JNIEXPORT void JNICALL
43 Java_java_io_ObjectOutputStream_floatsToBytes(JNIEnv *env,
44 jclass this,
45 jfloatArray src,
46 jint srcpos,
47 jbyteArray dst,
69 floats = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
70 if (floats == NULL) /* exception thrown */
71 return;
72
73 /* fetch dest array */
74 if (dst == NULL) {
75 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
76 JNU_ThrowNullPointerException(env, NULL);
77 return;
78 }
79 bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
80 if (bytes == NULL) { /* exception thrown */
81 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
82 return;
83 }
84
85 /* do conversion */
86 srcend = srcpos + nfloats;
87 for ( ; srcpos < srcend; srcpos++) {
88 fval = (float) floats[srcpos];
89 if (ISNANF(fval)) { /* collapse NaNs */
90 ival = 0x7fc00000;
91 } else {
92 u.f = fval;
93 ival = (jint) u.i;
94 }
95 bytes[dstpos++] = (ival >> 24) & 0xFF;
96 bytes[dstpos++] = (ival >> 16) & 0xFF;
97 bytes[dstpos++] = (ival >> 8) & 0xFF;
98 bytes[dstpos++] = (ival >> 0) & 0xFF;
99 }
100
101 (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
102 (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
103 }
104
105 /*
106 * Class: java_io_ObjectOutputStream
107 * Method: doublesToBytes
108 * Signature: ([DI[BII)V
109 *
141 doubles = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
142 if (doubles == NULL) /* exception thrown */
143 return;
144
145 /* fetch dest array */
146 if (dst == NULL) {
147 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
148 JNU_ThrowNullPointerException(env, NULL);
149 return;
150 }
151 bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
152 if (bytes == NULL) { /* exception thrown */
153 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
154 return;
155 }
156
157 /* do conversion */
158 srcend = srcpos + ndoubles;
159 for ( ; srcpos < srcend; srcpos++) {
160 dval = doubles[srcpos];
161 if (ISNAND((double) dval)) { /* collapse NaNs */
162 lval = jint_to_jlong(0x7ff80000);
163 lval = jlong_shl(lval, 32);
164 } else {
165 jdouble_to_jlong_bits(&dval);
166 u.d = (double) dval;
167 lval = u.l;
168 }
169 bytes[dstpos++] = (lval >> 56) & 0xFF;
170 bytes[dstpos++] = (lval >> 48) & 0xFF;
171 bytes[dstpos++] = (lval >> 40) & 0xFF;
172 bytes[dstpos++] = (lval >> 32) & 0xFF;
173 bytes[dstpos++] = (lval >> 24) & 0xFF;
174 bytes[dstpos++] = (lval >> 16) & 0xFF;
175 bytes[dstpos++] = (lval >> 8) & 0xFF;
176 bytes[dstpos++] = (lval >> 0) & 0xFF;
177 }
178
179 (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
180 (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
181 }
|