~lovesyao/+junk/transgs

« back to all changes in this revision

Viewing changes to nazo/mem.d

  • Committer: Nazo
  • Date: 2008-10-18 08:26:14 UTC
  • Revision ID: lovesyao@hotmail.com-20081018082614-22qgtg2gsotz5r2i
initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * メモリ関係のテンプレート
 
3
 *
 
4
 * コードが汚いのは気にしない。コンパイラは信じるな。mmx/sseなんてシラネ、128bitってなに?
 
5
 *
 
6
 * License: Public Domain
 
7
 */
 
8
module nazo.mem;
 
9
import std.stdio;
 
10
 
 
11
void memcpy(int T)(const(void)* to, const(void)* from){
 
12
  static if(T==0)return;
 
13
  else{
 
14
    static if(T%2==1){
 
15
      *cast(byte*)to=*cast(byte*)from;
 
16
      static if(T > 1){to++;from++;}
 
17
    }
 
18
    static if(T%4>=2){
 
19
      *cast(short*)to=*cast(short*)from;
 
20
      static if(T > 3){to+=2;from+=2;}
 
21
    }
 
22
    static if(T%8>=4){
 
23
      *cast(int*)to=*cast(int*)from;
 
24
      static if(T > 7){to+=4;from+=4;}
 
25
    }
 
26
    static if(T < 8){
 
27
    }else static if(T == 8){
 
28
      *cast(long*)to=*cast(long*)from;
 
29
    }else{
 
30
      for(int i=0;i<(T/8-1);i++){
 
31
        *cast(long*)to=*cast(long*)from;
 
32
        to+=8;from+=8;
 
33
      }
 
34
      *cast(long*)to=*cast(long*)from;
 
35
    }
 
36
  }
 
37
}
 
38
 
 
39
void memzero(int T)(void* dst){
 
40
  static if(T==0)return;
 
41
  else{
 
42
    static if(T%2==1){
 
43
      *cast(ubyte*)dst=0;
 
44
      static if(T > 1){dst++;}
 
45
    }
 
46
    static if(T%4>=2){
 
47
      *cast(ushort*)dst=0;
 
48
      static if(T > 3){dst+=2;}
 
49
    }
 
50
    static if(T%8>=4){
 
51
      *cast(uint*)dst=0;
 
52
      static if(T > 7){dst+=4;}
 
53
    }
 
54
    static if(T < 8){
 
55
    }else static if(T == 8){
 
56
      *cast(ulong*)dst=0;
 
57
    }else{
 
58
      for(int i=0;i<(T/8-1);i++){
 
59
        *cast(ulong*)dst=0;
 
60
        dst+=8;
 
61
      }
 
62
      *cast(ulong*)dst=0;
 
63
    }
 
64
  }
 
65
}
 
66
 
 
67
void memset_ubyte(int T, ubyte U)(void* dst){
 
68
  static if(T==0)return;
 
69
  else{
 
70
    static if(T%2==1){
 
71
      *cast(ubyte*)dst=toMultiArr_ubyte!(ubyte,U);
 
72
      static if(T > 1){dst++;}
 
73
    }
 
74
    static if(T%4>=2){
 
75
      *cast(ushort*)dst=toMultiArr_ubyte!(ushort,U);
 
76
      static if(T > 3){dst+=2;}
 
77
    }
 
78
    static if(T%8>=4){
 
79
      *cast(uint*)dst=toMultiArr_ubyte!(uint,U);
 
80
      static if(T > 7){dst+=4;}
 
81
    }
 
82
    static if(T < 8){
 
83
    }else static if(T == 8){
 
84
      *cast(ulong*)dst=toMultiArr_ubyte!(ulong,U);
 
85
    }else{
 
86
      for(int i=0;i<(T/8-1);i++){
 
87
        *cast(ulong*)dst=toMultiArr_ubyte!(ulong,U);
 
88
        dst+=8;
 
89
      }
 
90
      *cast(ulong*)dst=toMultiArr_ubyte!(ulong,U);
 
91
    }
 
92
  }
 
93
}
 
94
 
 
95
void memset_ushort(int T, ushort U)(void* dst){
 
96
  static assert(T%2 == 0);
 
97
  static if(T==0)return;
 
98
  else{
 
99
    static if(T%4>=2){
 
100
      *cast(ushort*)dst=toMultiArr_ushort!(ushort,U);
 
101
      static if(T > 3){dst+=2;}
 
102
    }
 
103
    static if(T%8>=4){
 
104
      *cast(uint*)dst=toMultiArr_ushort!(uint,U);
 
105
      static if(T > 7){dst+=4;}
 
106
    }
 
107
    static if(T < 8){
 
108
    }else static if(T == 8){
 
109
      *cast(ulong*)dst=toMultiArr_ushort!(ulong,U);
 
110
    }else{
 
111
      for(int i=0;i<(T/8-1);i++){
 
112
        *cast(ulong*)dst=toMultiArr_ushort!(ulong,U);
 
113
        dst+=8;
 
114
      }
 
115
      *cast(ulong*)dst=toMultiArr_ushort!(ulong,U);
 
116
    }
 
117
  }
 
118
}
 
119
 
 
120
void memset_uint(int T, uint U)(void* dst){
 
121
  static assert(T%4 == 0);
 
122
  static if(T==0)return;
 
123
  else{
 
124
    static if(T%8>=4){
 
125
      *cast(uint*)dst=toMultiArr_uint!(uint,U);
 
126
      static if(T > 7){dst+=4;}
 
127
    }
 
128
    static if(T < 8){
 
129
    }else static if(T == 8){
 
130
      *cast(ulong*)dst=toMultiArr_uint!(ulong,U);
 
131
    }else{
 
132
      for(int i=0;i<(T/8-1);i++){
 
133
        *cast(ulong*)dst=toMultiArr_uint!(ulong,U);
 
134
        dst+=8;
 
135
      }
 
136
      *cast(ulong*)dst=toMultiArr_uint!(ulong,U);
 
137
    }
 
138
  }
 
139
}
 
140
 
 
141
void memset_ulong(int T, ulong U)(void* dst){
 
142
  static assert(T%8 == 0);
 
143
  static if(T==0)return;
 
144
  else{
 
145
    static if(T < 8){
 
146
    }else static if(T == 8){
 
147
      *cast(ulong*)dst=toMultiArr_ulong!(ulong,U);
 
148
    }else{
 
149
      for(int i=0;i<(T/8-1);i++){
 
150
        *cast(ulong*)dst=toMultiArr_ulong!(ulong,U);
 
151
        dst+=8;
 
152
      }
 
153
      *cast(ulong*)dst=toMultiArr_ulong!(ulong,U);
 
154
    }
 
155
  }
 
156
}
 
157
 
 
158
template toMultiArr_ubyte(T,ubyte U){
 
159
  static if(is(T==ubyte)){
 
160
    const T toMultiArr_ubyte = U;
 
161
  }else static if(is(T==ushort)){
 
162
    const T toMultiArr_ubyte = cast(ushort)U<<8 | U;
 
163
  }else static if(is(T==uint)){
 
164
    const T toMultiArr_ubyte = cast(uint)U<<24 | cast(uint)U<<16 | cast(ushort)U<<8 | U;
 
165
  }else static if(is(T==ulong)){
 
166
    const T toMultiArr_ubyte = cast(ulong)U<<56 | cast(ulong)U<<48 | cast(ulong)U<<40 | cast(ulong)U<<32 | cast(uint)U<<24 | cast(uint)U<<16 | cast(ushort)U<<8 | U;
 
167
  }
 
168
}
 
169
 
 
170
template toMultiArr_ushort(T,ushort U){
 
171
  static if(is(T==ushort)){
 
172
    const T toMultiArr_ushort = U;
 
173
  }else static if(is(T==uint)){
 
174
    const T toMultiArr_ushort = cast(uint)U<<16 | U;
 
175
  }else static if(is(T==ulong)){
 
176
    const T toMultiArr_ushort = cast(ulong)U<<48 | cast(ulong)U<<32 | cast(uint)U<<16 | U;
 
177
  }
 
178
}
 
179
 
 
180
template toMultiArr_uint(T,uint U){
 
181
  static if(is(T==uint)){
 
182
    const T toMultiArr_uint = U;
 
183
  }else static if(is(T==ulong)){
 
184
    const T toMultiArr_uint = cast(ulong)U<<32 | U;
 
185
  }
 
186
}
 
187
 
 
188
template toMultiArr_ulong(T,ulong U){
 
189
  static if(is(T==ulong)){
 
190
    const T toMultiArr_ulong = U;
 
191
  }
 
192
}
 
193
 
 
194
unittest{
 
195
  string a="abcdefghijklmnopqrstuvwxyz".dup;
 
196
  memcpy!(15)(a.ptr,a.ptr+1);
 
197
  assert(a == "bcdefghijklmnoppqrstuvwxyz");
 
198
  ubyte[100] b=void;
 
199
  memzero!(b.length*b[0].sizeof)(b.ptr);
 
200
  foreach(ubyte i;b){
 
201
    if(i!=0)assert(0);
 
202
  }
 
203
  ubyte[100] c=void;
 
204
  memset_ubyte!(c.length*c[0].sizeof,100)(c.ptr);
 
205
  foreach(ubyte i;c){
 
206
    if(i!=100)assert(0);
 
207
  }
 
208
  ushort[100] d=void;
 
209
  memset_ushort!(d.length*d[0].sizeof,100)(d.ptr);
 
210
  foreach(ushort i;d){
 
211
    if(i!=100)assert(0);
 
212
  }
 
213
  uint[100] e=void;
 
214
  memset_uint!(e.length*e[0].sizeof,100)(e.ptr);
 
215
  foreach(uint i;e){
 
216
    if(i!=100)assert(0);
 
217
  }
 
218
  ulong[100] f=void;
 
219
  memset_ulong!(f.length*f[0].sizeof,100)(f.ptr);
 
220
  foreach(ulong i;f){
 
221
    if(i!=100)assert(0);
 
222
  }
 
223
}