1 module rt.arrayInit;
2 private import ldc.intrinsics;
3 
4 extern(C):
5 
6 int memcmp(void*,void*,size_t);
7 size_t strlen(char*);
8 
9 version(LLVM64)
10 alias llvm_memcpy_i64 llvm_memcpy;
11 else
12 alias llvm_memcpy_i32 llvm_memcpy;
13 
14 // per-element array init routines
15 
16 void _d_array_init_i1(bool* a, size_t n, bool v)
17 {
18     auto p = a;
19     auto end = a+n;
20     while (p !is end)
21         *p++ = v;
22 }
23 
24 void _d_array_init_i8(ubyte* a, size_t n, ubyte v)
25 {
26     auto p = a;
27     auto end = a+n;
28     while (p !is end)
29         *p++ = v;
30 }
31 
32 void _d_array_init_i16(ushort* a, size_t n, ushort v)
33 {
34     auto p = a;
35     auto end = a+n;
36     while (p !is end)
37         *p++ = v;
38 }
39 
40 void _d_array_init_i32(uint* a, size_t n, uint v)
41 {
42     auto p = a;
43     auto end = a+n;
44     while (p !is end)
45         *p++ = v;
46 }
47 
48 void _d_array_init_i64(ulong* a, size_t n, ulong v)
49 {
50     auto p = a;
51     auto end = a+n;
52     while (p !is end)
53         *p++ = v;
54 }
55 
56 void _d_array_init_float(float* a, size_t n, float v)
57 {
58     auto p = a;
59     auto end = a+n;
60     while (p !is end)
61         *p++ = v;
62 }
63 
64 void _d_array_init_double(double* a, size_t n, double v)
65 {
66     auto p = a;
67     auto end = a+n;
68     while (p !is end)
69         *p++ = v;
70 }
71 
72 void _d_array_init_real(real* a, size_t n, real v)
73 {
74     auto p = a;
75     auto end = a+n;
76     while (p !is end)
77         *p++ = v;
78 }
79 
80 void _d_array_init_cfloat(cfloat* a, size_t n, cfloat v)
81 {
82     auto p = a;
83     auto end = a+n;
84     while (p !is end)
85         *p++ = v;
86 }
87 
88 void _d_array_init_cdouble(cdouble* a, size_t n, cdouble v)
89 {
90     auto p = a;
91     auto end = a+n;
92     while (p !is end)
93         *p++ = v;
94 }
95 
96 void _d_array_init_creal(creal* a, size_t n, creal v)
97 {
98     auto p = a;
99     auto end = a+n;
100     while (p !is end)
101         *p++ = v;
102 }
103 
104 void _d_array_init_pointer(void** a, size_t n, void* v)
105 {
106     auto p = a;
107     auto end = a+n;
108     while (p !is end)
109         *p++ = v;
110 }
111 
112 void _d_array_init_mem(void* a, size_t na, void* v, size_t nv)
113 {
114     auto p = a;
115     auto end = a + na*nv;
116     while (p !is end) {
117         version(LDC_LLVMPre28)
118             llvm_memcpy(p,v,nv,0);
119         else
120             llvm_memcpy(p,v,nv,1, false);
121         p += nv;
122     }
123 }
124 
125 /*
126 void _d_array_init(TypeInfo ti, void* a)
127 {
128     auto initializer = ti.next.init();
129     auto isize = initializer.length;
130     auto q = initializer.ptr;
131 
132     if (isize == 1)
133         memset(p, *cast(ubyte*)q, size);
134     else if (isize == int.sizeof)
135     {
136         int init = *cast(int*)q;
137         size /= int.sizeof;
138         for (size_t u = 0; u < size; u++)
139         {
140             (cast(int*)p)[u] = init;
141         }
142     }
143     else
144     {
145         for (size_t u = 0; u < size; u += isize)
146         {
147             memcpy(p + u, q, isize);
148         }
149     }
150 }*/
151 
152 // for array cast
153 size_t _d_array_cast_len(size_t len, size_t elemsz, size_t newelemsz)
154 {
155     if (newelemsz == 1) {
156         return len*elemsz;
157     }
158     else if ((len*elemsz) % newelemsz) {
159         throw new Exception("Bad array cast");
160     }
161     return (len*elemsz)/newelemsz;
162 }
163 
164 // slice copy when assertions are enabled
165 void _d_array_slice_copy(void* dst, size_t dstlen, void* src, size_t srclen)
166 {
167     if (dstlen != srclen)
168         throw new Exception("lengths don't match for array copy");
169     else if (dst+dstlen <= src || src+srclen <= dst)
170     {
171         version(LDC_LLVMPre28)
172             llvm_memcpy(dst, src, dstlen, 0);
173         else
174             llvm_memcpy(dst, src, dstlen, 1, false);
175     }
176     else
177         throw new Exception("overlapping array copy");
178 }