0N/A/*
2362N/A * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "jni.h"
0N/A#include "jvm.h"
0N/A#include "jni_util.h"
0N/A#include "jlong.h"
0N/A
0N/A#include "java_lang_Float.h"
0N/A#include "java_lang_Double.h"
0N/A#include "java_io_ObjectOutputStream.h"
0N/A
0N/A/*
0N/A * Class: java_io_ObjectOutputStream
0N/A * Method: floatsToBytes
0N/A * Signature: ([FI[BII)V
0N/A *
0N/A * Convert nfloats float values to their byte representations. Float values
0N/A * are read from array src starting at offset srcpos and written to array
0N/A * dst starting at offset dstpos.
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_io_ObjectOutputStream_floatsToBytes(JNIEnv *env,
0N/A jclass this,
0N/A jfloatArray src,
0N/A jint srcpos,
0N/A jbyteArray dst,
0N/A jint dstpos,
0N/A jint nfloats)
0N/A{
0N/A union {
0N/A int i;
0N/A float f;
0N/A } u;
0N/A jfloat *floats;
0N/A jbyte *bytes;
0N/A jsize srcend;
0N/A jint ival;
0N/A float fval;
0N/A
0N/A if (nfloats == 0)
0N/A return;
0N/A
0N/A /* fetch source array */
0N/A if (src == NULL) {
0N/A JNU_ThrowNullPointerException(env, NULL);
0N/A return;
0N/A }
0N/A floats = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
0N/A if (floats == NULL) /* exception thrown */
0N/A return;
0N/A
0N/A /* fetch dest array */
0N/A if (dst == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
0N/A JNU_ThrowNullPointerException(env, NULL);
0N/A return;
0N/A }
0N/A bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
0N/A if (bytes == NULL) { /* exception thrown */
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
0N/A return;
0N/A }
0N/A
0N/A /* do conversion */
0N/A srcend = srcpos + nfloats;
0N/A for ( ; srcpos < srcend; srcpos++) {
0N/A fval = (float) floats[srcpos];
0N/A if (JVM_IsNaN(fval)) { /* collapse NaNs */
0N/A ival = 0x7fc00000;
0N/A } else {
0N/A u.f = fval;
0N/A ival = (jint) u.i;
0N/A }
0N/A bytes[dstpos++] = (ival >> 24) & 0xFF;
0N/A bytes[dstpos++] = (ival >> 16) & 0xFF;
0N/A bytes[dstpos++] = (ival >> 8) & 0xFF;
0N/A bytes[dstpos++] = (ival >> 0) & 0xFF;
0N/A }
0N/A
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, floats, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
0N/A}
0N/A
0N/A/*
0N/A * Class: java_io_ObjectOutputStream
0N/A * Method: doublesToBytes
0N/A * Signature: ([DI[BII)V
0N/A *
0N/A * Convert ndoubles double values to their byte representations. Double
0N/A * values are read from array src starting at offset srcpos and written to
0N/A * array dst starting at offset dstpos.
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_io_ObjectOutputStream_doublesToBytes(JNIEnv *env,
0N/A jclass this,
0N/A jdoubleArray src,
0N/A jint srcpos,
0N/A jbyteArray dst,
0N/A jint dstpos,
0N/A jint ndoubles)
0N/A{
0N/A union {
0N/A jlong l;
0N/A double d;
0N/A } u;
0N/A jdouble *doubles;
0N/A jbyte *bytes;
0N/A jsize srcend;
0N/A jdouble dval;
0N/A jlong lval;
0N/A
0N/A if (ndoubles == 0)
0N/A return;
0N/A
0N/A /* fetch source array */
0N/A if (src == NULL) {
0N/A JNU_ThrowNullPointerException(env, NULL);
0N/A return;
0N/A }
0N/A doubles = (*env)->GetPrimitiveArrayCritical(env, src, NULL);
0N/A if (doubles == NULL) /* exception thrown */
0N/A return;
0N/A
0N/A /* fetch dest array */
0N/A if (dst == NULL) {
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
0N/A JNU_ThrowNullPointerException(env, NULL);
0N/A return;
0N/A }
0N/A bytes = (*env)->GetPrimitiveArrayCritical(env, dst, NULL);
0N/A if (bytes == NULL) { /* exception thrown */
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
0N/A return;
0N/A }
0N/A
0N/A /* do conversion */
0N/A srcend = srcpos + ndoubles;
0N/A for ( ; srcpos < srcend; srcpos++) {
0N/A dval = doubles[srcpos];
0N/A if (JVM_IsNaN((double) dval)) { /* collapse NaNs */
0N/A lval = jint_to_jlong(0x7ff80000);
0N/A lval = jlong_shl(lval, 32);
0N/A } else {
0N/A jdouble_to_jlong_bits(&dval);
0N/A u.d = (double) dval;
0N/A lval = u.l;
0N/A }
0N/A bytes[dstpos++] = (lval >> 56) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 48) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 40) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 32) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 24) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 16) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 8) & 0xFF;
0N/A bytes[dstpos++] = (lval >> 0) & 0xFF;
0N/A }
0N/A
0N/A (*env)->ReleasePrimitiveArrayCritical(env, src, doubles, JNI_ABORT);
0N/A (*env)->ReleasePrimitiveArrayCritical(env, dst, bytes, 0);
0N/A}