00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef MEMSET_N_H_INCLUDED
00025 #define MEMSET_N_H_INCLUDED
00026
00027 namespace Pentagram {
00028
00029 #if defined(__GNUC__) && defined(i386)
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 inline void memset_32_aligned(void *buf, uint32 val, uint32 dwords)
00041 {
00042 int u0, u1, u2;
00043 __asm__ __volatile__ ( \
00044 "cld\n\t" \
00045 "rep ; stosl\n\t" \
00046 : "=&D" (u0), "=&a" (u1), "=&c" (u2) \
00047 : "0" (buf), "1" (val), "2" (dwords) \
00048 : "memory" );
00049 }
00050
00051 #elif defined(_MSC_VER) && defined(_M_IX86) && (_M_IX86 >= 300)
00052
00053 #ifdef USE_MMX_ASM // Disabled by default, no obvious speed up
00054
00055
00056
00057
00058
00059
00060
00061
00062 inline void memset_32_aligned(void *buf, uint32 val, uint32 dwords)
00063 {
00064
00065 if ((uint32)(buf) & 4)
00066 {
00067 *(reinterpret_cast<uint32*>(buf)) = val;
00068 buf = (reinterpret_cast<uint32*>(buf))+1;
00069 dwords--;
00070 }
00071
00072 if (dwords > 1)
00073 {
00074 __asm {
00075 cld
00076 mov edi, buf
00077 mov ecx, dwords
00078 shr ecx, 1
00079 mov eax, val
00080 movd mm0, eax
00081 movd mm1, eax
00082 psllq mm1, 32
00083 por mm0, mm1
00084 align 16
00085 repeat:
00086 movq [edi], mm0
00087 add edi, 8
00088 loop repeat
00089 emms
00090 };
00091 }
00092
00093
00094 if (dwords & 1) *(reinterpret_cast<uint32*>(buf)) = val;
00095 }
00096
00097 #else // USE_MMX_ASM
00098
00099
00100
00101
00102
00103
00104
00105
00106 inline void memset_32_aligned(void *buf, uint32 val, uint32 dwords)
00107 {
00108 __asm {
00109 cld
00110 mov edi, buf
00111 mov eax, val
00112 mov ecx, dwords
00113 repne stosd
00114 };
00115 }
00116
00117 #endif // USE_MMX_ASM
00118
00119 #else
00120
00121
00122
00123
00124
00125
00126 inline void memset_32_aligned(void *buf, uint32 val, uint32 dwords)
00127 {
00128 do
00129 {
00130 *reinterpret_cast<uint32*>(buf) = val;
00131 buf = (reinterpret_cast<uint32*>(buf))+1;
00132 }
00133 while (--dwords);
00134 }
00135
00136 #endif
00137
00138
00139
00140
00141
00142
00143 inline void memset_32(void *buf, uint32 val, uint32 dwords)
00144 {
00145
00146 int align = 0;
00147 if (reinterpret_cast<uintptr>(buf) & 3)
00148 {
00149 align = 4;
00150 dwords--;
00151
00152
00153 if ((reinterpret_cast<uintptr>(buf) & 1))
00154 {
00155 *reinterpret_cast<uint8*>(buf) = static_cast<uint8>(val&0xFF);
00156 buf = (reinterpret_cast<uint8*>(buf))+1;
00157 val = ((val& 0xFF) << 24) || ((val& 0xFFFFFF00) >> 8);
00158 align --;
00159 }
00160
00161
00162 if ((reinterpret_cast<uintptr>(buf) & 2))
00163 {
00164 *reinterpret_cast<uint16*>(buf) = static_cast<uint16>(val&0xFFFF);
00165 buf = (reinterpret_cast<uint16*>(buf))+1;
00166 val = ((val& 0xFFFF) << 16) || ((val& 0xFFFF0000) >> 16);
00167 align-=2;
00168 }
00169 }
00170
00171
00172 memset_32_aligned(buf,val,dwords);
00173
00174
00175 if (align)
00176 {
00177
00178 if (align == 1)
00179 {
00180 *reinterpret_cast<uint8*>(buf) = static_cast<uint8>(val&0xFF);
00181 }
00182
00183 else
00184 {
00185 *reinterpret_cast<uint16*>(buf) = static_cast<uint16>(val&0xFFFF);
00186
00187
00188 if (align & 1) *(reinterpret_cast<uint8*>(buf)+2) = static_cast<uint8>((val>>16)&0xFF);
00189 }
00190 }
00191 }
00192
00193
00194
00195
00196
00197
00198 inline void memset_16(void *buf, sint32 val, uint32 words)
00199 {
00200
00201 if (words > 1) memset_32(buf,val|val<<16,words>>1);
00202
00203
00204 if (words & 1) *(reinterpret_cast<uint16*>(buf)) = static_cast<uint16>(val&0xFFFF);
00205 }
00206
00207 }
00208
00209 #endif //MEMSET_N_H_INCLUDED