IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Bits.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2013 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Runtime.InteropServices;
26using System.Security;
27using System.Security.Permissions;
28
30{
31
32 [SecurityCritical]
33 static class Bits
34 {
35
36 public static void copyFromShortArray(object src, long srcPos, long dstAddr, long length)
37 {
38#if !FIRST_PASS
39 short[] shortArray = src as short[];
40 if (shortArray != null)
41 {
42 int index = ((int)srcPos) >> 1;
43 while (length > 0)
44 {
45 short v = global::java.lang.Short.reverseBytes(shortArray[index++]);
46 Marshal.WriteInt16((IntPtr)dstAddr, v);
47 dstAddr += 2;
48 length -= 2;
49 }
50 }
51 else
52 {
53 char[] charArray = (char[])src;
54 int index = ((int)srcPos) >> 1;
55 while (length > 0)
56 {
57 short v = global::java.lang.Short.reverseBytes((short)charArray[index++]);
58 Marshal.WriteInt16((IntPtr)dstAddr, v);
59 dstAddr += 2;
60 length -= 2;
61 }
62 }
63#endif
64 }
65
66 public static void copyToShortArray(long srcAddr, object dst, long dstPos, long length)
67 {
68#if !FIRST_PASS
69 short[] shortArray = dst as short[];
70 if (shortArray != null)
71 {
72 int index = ((int)dstPos) >> 1;
73 while (length > 0)
74 {
75 short v = Marshal.ReadInt16((IntPtr)srcAddr);
76 shortArray[index++] = global::java.lang.Short.reverseBytes(v);
77 srcAddr += 2;
78 length -= 2;
79 }
80 }
81 else
82 {
83 char[] charArray = (char[])dst;
84 int index = ((int)dstPos) >> 1;
85 while (length > 0)
86 {
87 short v = Marshal.ReadInt16((IntPtr)srcAddr);
88 charArray[index++] = (char)global::java.lang.Short.reverseBytes(v);
89 srcAddr += 2;
90 length -= 2;
91 }
92 }
93#endif
94 }
95
96 public static void copyFromIntArray(object src, long srcPos, long dstAddr, long length)
97 {
98#if !FIRST_PASS
99 int[] intArray = src as int[];
100 if (intArray != null)
101 {
102 int index = ((int)srcPos) >> 2;
103 while (length > 0)
104 {
105 int v = global::java.lang.Integer.reverseBytes(intArray[index++]);
106 Marshal.WriteInt32((IntPtr)dstAddr, v);
107 dstAddr += 4;
108 length -= 4;
109 }
110 }
111 else
112 {
113 float[] floatArray = (float[])src;
114 int index = ((int)srcPos) >> 2;
115 while (length > 0)
116 {
117 int v = global::java.lang.Integer.reverseBytes(global::java.lang.Float.floatToRawIntBits(floatArray[index++]));
118 Marshal.WriteInt32((IntPtr)dstAddr, v);
119 dstAddr += 4;
120 length -= 4;
121 }
122 }
123#endif
124 }
125
126 public static void copyToIntArray(long srcAddr, object dst, long dstPos, long length)
127 {
128#if !FIRST_PASS
129 int[] intArray = dst as int[];
130 if (intArray != null)
131 {
132 int index = ((int)dstPos) >> 2;
133 while (length > 0)
134 {
135 int v = Marshal.ReadInt32((IntPtr)srcAddr);
136 intArray[index++] = global::java.lang.Integer.reverseBytes(v);
137 srcAddr += 4;
138 length -= 4;
139 }
140 }
141 else
142 {
143 float[] floatArray = (float[])dst;
144 int index = ((int)dstPos) >> 2;
145 while (length > 0)
146 {
147 int v = Marshal.ReadInt32((IntPtr)srcAddr);
148 floatArray[index++] = global::java.lang.Float.intBitsToFloat(global::java.lang.Integer.reverseBytes(v));
149 srcAddr += 4;
150 length -= 4;
151 }
152 }
153#endif
154 }
155
156 public static void copyFromLongArray(object src, long srcPos, long dstAddr, long length)
157 {
158#if !FIRST_PASS
159 long[] longArray = src as long[];
160 if (longArray != null)
161 {
162 int index = ((int)srcPos) >> 3;
163 while (length > 0)
164 {
165 long v = global::java.lang.Long.reverseBytes(longArray[index++]);
166 Marshal.WriteInt64((IntPtr)dstAddr, v);
167 dstAddr += 8;
168 length -= 8;
169 }
170 }
171 else
172 {
173 double[] doubleArray = (double[])src;
174 int index = ((int)srcPos) >> 3;
175 while (length > 0)
176 {
177 long v = global::java.lang.Long.reverseBytes(BitConverter.DoubleToInt64Bits(doubleArray[index++]));
178 Marshal.WriteInt64((IntPtr)dstAddr, v);
179 dstAddr += 8;
180 length -= 8;
181 }
182 }
183#endif
184 }
185
186 public static void copyToLongArray(long srcAddr, object dst, long dstPos, long length)
187 {
188#if !FIRST_PASS
189 long[] longArray = dst as long[];
190 if (longArray != null)
191 {
192 int index = ((int)dstPos) >> 3;
193 while (length > 0)
194 {
195 long v = Marshal.ReadInt64((IntPtr)srcAddr);
196 longArray[index++] = global::java.lang.Long.reverseBytes(v);
197 srcAddr += 8;
198 length -= 8;
199 }
200 }
201 else
202 {
203 double[] doubleArray = (double[])dst;
204 int index = ((int)dstPos) >> 3;
205 while (length > 0)
206 {
207 long v = Marshal.ReadInt64((IntPtr)srcAddr);
208 doubleArray[index++] = BitConverter.Int64BitsToDouble(global::java.lang.Long.reverseBytes(v));
209 srcAddr += 8;
210 length -= 8;
211 }
212 }
213#endif
214 }
215
216 }
217
218}
static void copyToLongArray(long srcAddr, object dst, long dstPos, long length)
Definition Bits.cs:186
static void copyFromShortArray(object src, long srcPos, long dstAddr, long length)
Definition Bits.cs:36
static void copyToShortArray(long srcAddr, object dst, long dstPos, long length)
Definition Bits.cs:66
static void copyFromIntArray(object src, long srcPos, long dstAddr, long length)
Definition Bits.cs:96
static void copyToIntArray(long srcAddr, object dst, long dstPos, long length)
Definition Bits.cs:126
static void copyFromLongArray(object src, long srcPos, long dstAddr, long length)
Definition Bits.cs:156