GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
ValueConv.java
Go to the documentation of this file.
1package com.jogamp.common.util;
2
3/**
4 * Copyright 2012 JogAmp Community. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * The views and conclusions contained in the software and documentation are those of the
27 * authors and should not be interpreted as representing official policies, either expressed
28 * or implied, of JogAmp Community.
29 */
30
31/**
32 * Utility class providing simple signed and unsigned primitive value conversions
33 * for byte, short, int, float and double.
34 * <p>
35 * Non float to non float conversions are handled via float or double,
36 * depending on the value range.
37 * </p>
38 */
39public class ValueConv {
40 public static final byte float_to_byte(final float v, final boolean dSigned) {
41 // lossy
42 if( dSigned ) {
43 return (byte) ( v * ( v > 0 ? 127.0f : 128.0f ) );
44 } else {
45 return (byte) ( v * 255.0f );
46 }
47 }
48 public static final short float_to_short(final float v, final boolean dSigned) {
49 if( dSigned ) {
50 return (short) ( v * ( v > 0 ? 32767.0f : 32768.0f ) );
51 } else {
52 return (short) ( v * 65535.0f );
53 }
54 }
55 public static final int float_to_int(final float v, final boolean dSigned) {
56 // float significand 0x007fffff
57 // double significand 0x000fffffffffffffL
58 // int min = 0x80000000 = -2147483648
59 // int max = 0x7fffffff = +2147483647
60 if( dSigned ) {
61 return (int) ( v * ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
62 } else {
63 return (int) (long) ( v * 4294967295.0 );
64 }
65 }
66
67 public static final byte double_to_byte(final double v, final boolean dSigned) {
68 // lossy
69 if( dSigned ) {
70 return (byte) ( v * ( v > 0 ? 127.0 : 128.0 ) );
71 } else {
72 return (byte) ( v * 255.0 );
73 }
74 }
75 public static final short double_to_short(final double v, final boolean dSigned) {
76 // lossy
77 if( dSigned ) {
78 return (short) ( v * ( v > 0 ? 32767.0 : 32768.0 ) );
79 } else {
80 return (short) ( v * 65535.0 );
81 }
82 }
83 public static final int double_to_int(final double v, final boolean dSigned) {
84 // lossy
85 if( dSigned ) {
86 return (int) ( v * ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
87 } else {
88 return (int) (long) ( v * 4294967295.0 );
89 }
90 }
91
92 public static final float byte_to_float(final byte v, final boolean sSigned) {
93 if( sSigned ) {
94 return (v & 0xff) / ( v > 0 ? 127.0f : -128.0f ) ;
95 } else {
96 return (v & 0xff) / 255.0f ;
97 }
98 }
99 public static final double byte_to_double(final byte v, final boolean sSigned) {
100 if( sSigned ) {
101 return (v & 0xff) / ( v > 0 ? 127.0 : -128.0 ) ;
102 } else {
103 return (v & 0xff) / 255.0 ;
104 }
105 }
106 public static final float short_to_float(final short v, final boolean sSigned) {
107 if( sSigned ) {
108 return (v & 0xffff) / ( v > 0 ? 32767.0f : -32768.0f ) ;
109 } else {
110 return (v & 0xffff) / 65535.0f ;
111 }
112 }
113 public static final double short_to_double(final short v, final boolean sSigned) {
114 // lossy
115 if( sSigned ) {
116 return (v & 0xffff) / ( v > 0 ? 32767.0 : -32768.0 ) ;
117 } else {
118 return (v & 0xffff) / 65535.0 ;
119 }
120 }
121 public static final float int_to_float(final int v, final boolean sSigned) {
122 // lossy
123 // float significand 0x007fffff
124 // double significand 0x000fffffffffffffL
125 // int min = 0x80000000 = -2147483648
126 // int max = 0x7fffffff = +2147483647
127 if( sSigned ) {
128 return (float) ( v / ( v > 0 ? 2147483647.0 : 2147483648.0 ) );
129 } else {
130 return (float) ( (v & 0xffffffffL) / 4294967295.0 );
131 }
132 }
133 public static final double int_to_double(final int v, final boolean sSigned) {
134 if( sSigned ) {
135 return v / ( v > 0 ? 2147483647.0 : 2147483648.0 ) ;
136 } else {
137 return (v & 0xffffffffL) / 4294967295.0 ;
138 }
139 }
140
141 public static final short byte_to_short(final byte v, final boolean sSigned, final boolean dSigned) {
142 return float_to_short(byte_to_float(v, sSigned), dSigned);
143 }
144 public static final int byte_to_int(final byte v, final boolean sSigned, final boolean dSigned) {
145 return float_to_int(byte_to_float(v, sSigned), dSigned);
146 }
147
148 public static final byte short_to_byte(final short v, final boolean sSigned, final boolean dSigned) {
149 return float_to_byte(short_to_float(v, sSigned), dSigned);
150 }
151 public static final int short_to_int(final short v, final boolean sSigned, final boolean dSigned) {
152 return float_to_int(short_to_float(v, sSigned), dSigned);
153 }
154
155 public static final byte int_to_byte(final int v, final boolean sSigned, final boolean dSigned) {
156 return float_to_byte(int_to_float(v, sSigned), dSigned);
157 }
158 public static final short int_to_short(final int v, final boolean sSigned, final boolean dSigned) {
159 return float_to_short(int_to_float(v, sSigned), dSigned);
160 }
161}
Copyright 2012 JogAmp Community.
Definition: ValueConv.java:39
static final short double_to_short(final double v, final boolean dSigned)
Definition: ValueConv.java:75
static final double int_to_double(final int v, final boolean sSigned)
Definition: ValueConv.java:133
static final float short_to_float(final short v, final boolean sSigned)
Definition: ValueConv.java:106
static final int byte_to_int(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:144
static final int float_to_int(final float v, final boolean dSigned)
Definition: ValueConv.java:55
static final double short_to_double(final short v, final boolean sSigned)
Definition: ValueConv.java:113
static final byte short_to_byte(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:148
static final byte float_to_byte(final float v, final boolean dSigned)
Definition: ValueConv.java:40
static final short float_to_short(final float v, final boolean dSigned)
Definition: ValueConv.java:48
static final short byte_to_short(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:141
static final int double_to_int(final double v, final boolean dSigned)
Definition: ValueConv.java:83
static final short int_to_short(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:158
static final float byte_to_float(final byte v, final boolean sSigned)
Definition: ValueConv.java:92
static final byte int_to_byte(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:155
static final float int_to_float(final int v, final boolean sSigned)
Definition: ValueConv.java:121
static final byte double_to_byte(final double v, final boolean dSigned)
Definition: ValueConv.java:67
static final int short_to_int(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:151
static final double byte_to_double(final byte v, final boolean sSigned)
Definition: ValueConv.java:99