Mercurial > projects > ldc
comparison druntime/src/compiler/ldc/arraycast.d @ 1458:e0b2d67cfe7c
Added druntime (this should be removed once it works).
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Tue, 02 Jun 2009 17:43:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1456:7b218ec1044f | 1458:e0b2d67cfe7c |
---|---|
1 /** | |
2 * Implementation of array cast support routines. | |
3 * | |
4 * Copyright: Copyright Digital Mars 2004 - 2009. | |
5 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>. | |
6 * Authors: Walter Bright, Sean Kelly | |
7 * | |
8 * Copyright Digital Mars 2004 - 2009. | |
9 * Distributed under the Boost Software License, Version 1.0. | |
10 * (See accompanying file LICENSE_1_0.txt or copy at | |
11 * http://www.boost.org/LICENSE_1_0.txt) | |
12 */ | |
13 module rt.arraycast; | |
14 | |
15 /****************************************** | |
16 * Runtime helper to convert dynamic array of one | |
17 * type to dynamic array of another. | |
18 * Adjusts the length of the array. | |
19 * Throws exception if new length is not aligned. | |
20 */ | |
21 | |
22 extern (C) | |
23 | |
24 void[] _d_arraycast(size_t tsize, size_t fsize, void[] a) | |
25 { | |
26 auto length = a.length; | |
27 | |
28 auto nbytes = length * fsize; | |
29 if (nbytes % tsize != 0) | |
30 { | |
31 throw new Exception("array cast misalignment"); | |
32 } | |
33 length = nbytes / tsize; | |
34 *cast(size_t *)&a = length; // jam new length | |
35 return a; | |
36 } | |
37 | |
38 unittest | |
39 { | |
40 byte[int.sizeof * 3] b; | |
41 int[] i; | |
42 short[] s; | |
43 | |
44 i = cast(int[])b; | |
45 assert(i.length == 3); | |
46 | |
47 s = cast(short[])b; | |
48 assert(s.length == 6); | |
49 | |
50 s = cast(short[])i; | |
51 assert(s.length == 6); | |
52 } | |
53 | |
54 /****************************************** | |
55 * Runtime helper to convert dynamic array of bits | |
56 * dynamic array of another. | |
57 * Adjusts the length of the array. | |
58 * Throws exception if new length is not aligned. | |
59 */ | |
60 | |
61 version (none) | |
62 { | |
63 extern (C) | |
64 | |
65 void[] _d_arraycast_frombit(uint tsize, void[] a) | |
66 { | |
67 uint length = a.length; | |
68 | |
69 if (length & 7) | |
70 { | |
71 throw new Exception("bit[] array cast misalignment"); | |
72 } | |
73 length /= 8 * tsize; | |
74 *cast(size_t *)&a = length; // jam new length | |
75 return a; | |
76 } | |
77 | |
78 unittest | |
79 { | |
80 version (D_Bits) | |
81 { | |
82 bit[int.sizeof * 3 * 8] b; | |
83 int[] i; | |
84 short[] s; | |
85 | |
86 i = cast(int[])b; | |
87 assert(i.length == 3); | |
88 | |
89 s = cast(short[])b; | |
90 assert(s.length == 6); | |
91 } | |
92 } | |
93 | |
94 } |