You're here: Snippet Directory » C/C++ (495)
Language:

MemSetN

Language: English
Programming Language: C
Published by: malcolmnixon [not registered]
Last Update: 5/15/2006
Views: 153

Description

A set of functions similar to memset that work on 16 bit and 32 bit size data as well as 8 bit. Contains optional X86 assembler implementation. Completely FREE!

Code

1 //////////////////////////////////////////////////////////////////////////////// 2 // 3 // Filename: MemSetN.h 4 // 5 // Version: 1.0 6 // 7 // Description: Header file for a set of functions similar to memset that 8 // work on sizes other than bytes. 9 // 10 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 11 // 12 // This code is supplied as is, without warranty. It may be used for any 13 // purpose commercial or private. 14 15 16 17 18 #ifndef __MEMSETN_H 19 #define __MEMSETN_H 20 21 // Include the header for the runtime memset function. 22 #include <memory.h> 23 24 25 26 27 // Check for assembler functions. If set then the functions are in 28 // MemSetN_X86.s, and we must prototype them for the C/C++ compiler. 29 #if defined(USEASM) 30 31 // Prototype for the assembler functions. 32 extern "C" void MemSet16_X86(void *dest, unsigned short val, unsigned int count); 33 extern "C" void MemSet32_X86(void *dest, unsigned long val, unsigned int count); 34 35 // Macros. 36 #define MemSet8(d, s, c) memset(d, s, c); 37 #define MemSet16(d, s, c) MemSet16_X86(d, s, c); 38 #define MemSet32(d, s, c) MemSet32_X86(d, s, c); 39 40 41 42 43 // If we have reached this test then we don't have assembler support. Check 44 // for a C++ compliant compiler and create the inline procedures. 45 #elif defined(__cplusplus) 46 47 // Function to set [count] number of words at [dest] to [src]. 48 inline void MemSet16_Cpp(unsigned short *dest, unsigned short src, unsigned int count) 49 { 50 while (count--) *dest++ = src; 51 } 52 53 // Function to set [count] number of dwords at [dest] to [src]. 54 inline void MemSet32_Cpp(unsigned long *dest, unsigned long src, unsigned int count) 55 { 56 while (count--) *dest++ = src; 57 } 58 59 // Macros to supply the type cast for the C++ functions. 60 #define MemSet8(d, s, c) memset(d, s, c); 61 #define MemSet16(d, s, c) MemSet16_Cpp((unsigned short*)d, s, c); 62 #define MemSet32(d, s, c) MemSet32_Cpp((unsigned long*)d, s, c); 63 64 65 66 67 // If the above #ifs failed then we must assume that this is a C compiler. The 68 // body of the functions are in MemSetN.c. 69 #else 70 71 // Prototypes for the C function in MemSetN.c 72 extern void MemSet16_C(unsigned short *dest, unsigned short src, unsigned int count); 73 extern void MemSet32_C(unsigned long *dest, unsigned long src, unsigned int count); 74 75 // Macros to supply the type cast for the C functions. 76 #define MemSet8(d, s, c) memset(d, s, c); 77 #define MemSet16(d, s, c) MemSet16_C((unsigned short*)d, s, c); 78 #define MemSet32(d, s, c) MemSet32_C((unsigned long*)d, s, c); 79 80 #endif //USEASM 81 82 #endif //__MEMSETN_H 83 // End of file. 84 85 //////////////////////////////////////////////////////////////////////////////// 86 // 87 // Filename: MemSetN.c 88 // 89 // Version: 1.0 90 // 91 // Description: C functions similar to memset which work on sizes other 92 // than bytes. 93 // 94 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 95 // 96 // This code is supplied as is, without warranty. It may be used for any 97 // purpose commercial or private. 98 99 100 // Function to set [count] number of words at [dest] to [src]. 101 void MemSet16_C(unsigned short *dest, unsigned short src, unsigned int count) 102 { 103 while (count--) *dest++ = src; 104 } 105 106 // Function to set [count] number of dwords at [dest] to [src]. 107 void MemSet32_C(unsigned long *dest, unsigned long src, unsigned int count) 108 { 109 while (count--) *dest++ = src; 110 } 111 112 // End of file. 113 114 //////////////////////////////////////////////////////////////////////////////// 115 // 116 // Filename: MemSetN_X86.s 117 // 118 // Version: 1.0 119 // 120 // Description: Assember functions similar to memset which work on sizes 121 // other than bytes. Use the following gcc options to compile: 122 // gcc -c -x assembler-with-cpp MemSetN_X86.s 123 // 124 // Author: Malcolm Nixon (MalcolmNixon@earthlink.net) 125 // 126 // This code is supplied as is, without warranty. It may be used for any 127 // purpose commercial or private. 128 129 130 131 /// Macros to get the C naming convention correct. 132 #ifdef __linux__ 133 #define C(label) label // ELF format 134 #else 135 #define C(label) _##label // COFF format 136 #endif 137 138 139 140 // Set segment and allignment 141 .text 142 .align 4 143 144 // Argument position macros. 145 #define argDest 12+0 146 #define argVal 12+4 147 #define argCount 12+8 148 149 150 151 ////////////////////////////////////////////////////////////////////////////// 152 // 153 // MemSet16 - Sets [Dest] to contain [Count] copies of the word [Val]. 154 // 155 // Note: This function is not optimal. It should set as many dwords as 156 // possible, then set the remaining word if count is odd. 157 // 158 ////////////////////////////////////////////////////////////////////////////// 159 .globl C(MemSet16_X86) 160 C(MemSet16_X86): 161 pushl %edi 162 pushl %ecx 163 164 movl argDest(%esp), %edi 165 movw argVal(%esp), %ax 166 movl argCount(%esp), %ecx 167 rep stosw 168 169 popl %edi 170 popl %ecx 171 ret 172 173 ////////////////////////////////////////////////////////////////////////////// 174 // 175 // MemSet32 - Sets [Dest] to contain [Count] copies of the dword [Val]. 176 // 177 ////////////////////////////////////////////////////////////////////////////// 178 .globl C(MemSet32_X86) 179 C(MemSet32_X86): 180 pushl %edi 181 pushl %ecx 182 183 movl argDest(%esp), %edi 184 movl argVal(%esp), %eax 185 movl argCount(%esp), %ecx 186 rep stosl 187 188 popl %edi 189 popl %ecx 190 ret 191 192 // End of file. 193

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS