1 module CPUblit.composing.copy;
2 
3 import core.stdc..string : memcpy;
4 
5 /*
6  * CPUblit
7  * Copy composing functions.
8  * Author: Laszlo Szeremi
9  *
10  * The functions can be used on 8, 16, 24, and 32 bit datatypes. These cannot deal with alignments related to datatypes less than 8 bit.
11  * Only two operators + length are used, 3 and 4 operator ones currently are there for swapping with other 3 and 4 operator functions.
12  * These functions are using memcpy as their backend, so these should use whatever is the most efficient on your target.
13  */
14 
15 @nogc pure nothrow {
16 	/**
17 	 * 2 operator copy function.
18 	 */
19 	void copy(T)(T* src, T* dest, size_t length) {
20 		memcpy(dest, src, length * T.sizeof);
21 	}
22 	/**
23 	 * 3 operator copy function.
24 	 */
25 	void copy(T,M)(T* src, T* dest, size_t length, M* mask) {
26 		memcpy(dest, src, length * T.sizeof);
27 	}
28 	/**
29 	 * 3 operator copy function.
30 	 */
31 	void copy(T)(T* src, T* dest, T* dest0, size_t length) {
32 		memcpy(dest0, src, length * T.sizeof);
33 	}
34 	/**
35 	 * 4 operator copy function.
36 	 */
37 	void copy(T,M)(T* src, T* dest, T* dest0, size_t length, M* mask) {
38 		memcpy(dest0, src, length * T.sizeof);
39 	}
40 	/**
41 	 * 2 operator copy function with dummy master value.
42 	 */
43 	void copyMV(T)(T* src, T* dest, size_t length, ubyte value) {
44 		memcpy(dest, src, length * T.sizeof);
45 	}
46 	/**
47 	 * 3 operator copy function with dummy master value.
48 	 */
49 	void copyMV(T,M)(T* src, T* dest, size_t length, M* mask, ubyte value) {
50 		memcpy(dest, src, length * T.sizeof);
51 	}
52 	/**
53 	 * 3 operator copy function with dummy master value.
54 	 */
55 	void copyMV(T)(T* src, T* dest, T* dest0, size_t length, ubyte value) {
56 		memcpy(dest0, src, length * T.sizeof);
57 	}
58 	/**
59 	 * 4 operator copy function with dummy master value.
60 	 */
61 	void copyMV(T,M)(T* src, T* dest, T* dest0, size_t length, M* mask, ubyte value) {
62 		memcpy(dest0, src, length * T.sizeof);
63 	}
64 }
65 
66 unittest {
67 	void testfunc(T)(){
68 		{
69 			T[255] a, b;
70 			copy(a.ptr, b.ptr, 255);
71 			foreach (T val ; b) {
72 				assert(!val);
73 			}
74 			foreach (ref T val ; a) {
75 				val = T.max;
76 			}
77 			copy(a.ptr, b.ptr, 255);
78 			foreach (T val ; b) {
79 				assert(val == T.max);
80 			}
81 		}
82 		{
83 			T[255] a, b;
84 			copy(a.ptr, b.ptr, b.ptr, 255);
85 			foreach (T val ; b) {
86 				assert(!val);
87 			}
88 			foreach (ref T val ; a) {
89 				val = T.max;
90 			}
91 			copy(a.ptr, b.ptr, b.ptr, 255);
92 			foreach (T val ; b) {
93 				assert(val == T.max);
94 			}
95 		}
96 		/+{
97 			T[255] a, b;
98 			copy(a.ptr, b.ptr, 255, null);
99 			foreach (T val ; b) {
100 				assert(!val);
101 			}
102 			foreach (ref T val ; a) {
103 				val = T.max;
104 			}
105 			copy(a.ptr, b.ptr, 255, null);
106 			foreach (T val ; b) {
107 				assert(val == T.max);
108 			}
109 		}
110 		{
111 			T[255] a, b;
112 			copy(a.ptr, b.ptr, b.ptr, 255, null);
113 			foreach (T val ; b) {
114 				assert(!val);
115 			}
116 			foreach (ref T val ; a) {
117 				val = T.max;
118 			}
119 			copy(a.ptr, b.ptr, b.ptr, 255, null);
120 			foreach (T val ; b) {
121 				assert(val == T.max);
122 			}
123 		}+/
124 	}
125 	testfunc!ubyte();
126 	testfunc!ushort();
127 	testfunc!uint();
128 }