comparison druntime/src/compiler/dmd/arraycast.d @ 759:d3eb054172f9

Added copy of druntime from DMD 2.020 modified for LDC.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:52:37 +0100
parents
children
comparison
equal deleted inserted replaced
758:f04dde6e882c 759:d3eb054172f9
1 /*
2 * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com
3 * Written by Walter Bright
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, in both source and binary form, subject to the following
12 * restrictions:
13 *
14 * o The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * o Altered source versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
20 * o This notice may not be removed or altered from any source
21 * distribution.
22 */
23
24 /*
25 * Modified by Sean Kelly for use with the D Runtime Project
26 */
27
28 module rt.arraycast;
29
30 /******************************************
31 * Runtime helper to convert dynamic array of one
32 * type to dynamic array of another.
33 * Adjusts the length of the array.
34 * Throws exception if new length is not aligned.
35 */
36
37 extern (C)
38
39 void[] _d_arraycast(size_t tsize, size_t fsize, void[] a)
40 {
41 auto length = a.length;
42
43 auto nbytes = length * fsize;
44 if (nbytes % tsize != 0)
45 {
46 throw new Exception("array cast misalignment");
47 }
48 length = nbytes / tsize;
49 *cast(size_t *)&a = length; // jam new length
50 return a;
51 }
52
53 unittest
54 {
55 byte[int.sizeof * 3] b;
56 int[] i;
57 short[] s;
58
59 i = cast(int[])b;
60 assert(i.length == 3);
61
62 s = cast(short[])b;
63 assert(s.length == 6);
64
65 s = cast(short[])i;
66 assert(s.length == 6);
67 }
68
69 /******************************************
70 * Runtime helper to convert dynamic array of bits
71 * dynamic array of another.
72 * Adjusts the length of the array.
73 * Throws exception if new length is not aligned.
74 */
75
76 version (none)
77 {
78 extern (C)
79
80 void[] _d_arraycast_frombit(uint tsize, void[] a)
81 {
82 uint length = a.length;
83
84 if (length & 7)
85 {
86 throw new Exception("bit[] array cast misalignment");
87 }
88 length /= 8 * tsize;
89 *cast(size_t *)&a = length; // jam new length
90 return a;
91 }
92
93 unittest
94 {
95 version (D_Bits)
96 {
97 bit[int.sizeof * 3 * 8] b;
98 int[] i;
99 short[] s;
100
101 i = cast(int[])b;
102 assert(i.length == 3);
103
104 s = cast(short[])b;
105 assert(s.length == 6);
106 }
107 }
108
109 }