00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef UMEMCMP_H
00031 #define UMEMCMP_H
00032
00033 #include "w_defines.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <assert.h>
00047
00048 #ifndef W_WORKAROUND_H
00049 #include <w_workaround.h>
00050 #endif
00051
00052
00053 inline int __umemcmp(const unsigned char* p, const unsigned char* q, int n)
00054 {
00055 int i;
00056 for (i = 0; (i < n) && (*p == *q); i++, p++, q++) ;
00057 return (i < n) ? *p - *q : 0;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #if defined(COMMON_USE_UMEMCMP) || defined(Sparc)
00069
00070 inline uint int_alignment_check(const void *i)
00071 {
00072 uint tmp = (ptrdiff_t) i & (sizeof(int)-1);
00073 w_assert9(tmp == (ptrdiff_t) i % sizeof(int));
00074 return tmp;
00075 }
00076 inline bool is_int_aligned(const void *i)
00077 {
00078 return int_alignment_check(i) == 0;
00079 }
00080
00081
00082
00083 inline int umemcmp_smart(const void* p_, const void* q_, int n)
00084 {
00085 const unsigned char* p = (const unsigned char*)p_;
00086 const unsigned char* q = (const unsigned char*)q_;
00087
00088
00089 if (n < (int)(2*sizeof(int)))
00090 return __umemcmp(p, q, n);
00091
00092
00093 if (int_alignment_check(p) == int_alignment_check(q)) {
00094 if (!is_int_aligned(p)) {
00095
00096 return __umemcmp(p, q, n);
00097 }
00098
00099
00100 uint i;
00101 for (i = 0; i < n/sizeof(int); i++) {
00102 if (((unsigned*)p)[i] != ((unsigned*)q)[i]) {
00103 return (((unsigned*)p)[i] > ((unsigned*)q)[i]) ? 1 : -1;
00104 }
00105 }
00106
00107 int j = i*sizeof(int);
00108 if (j) return __umemcmp(p+j, q+j, n-j);
00109 } else {
00110
00111 return __umemcmp(p, q, n);
00112 }
00113 return 0;
00114 }
00115
00116 inline int umemcmp_old(const void* p, const void* q, int n)
00117 {
00118 return __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00119 }
00120
00121 inline int umemcmp(const void* p, const void* q, int n)
00122 {
00123 #if W_DEBUG_LEVEL > 2
00124
00125 int t1 = umemcmp_smart(p, q, n);
00126 int t2 = __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00127 assert(t1 == t2 || (t1 < 0 && t2 < 0) || (t1 > 0 && t2 > 0));
00128 return t1;
00129 #else
00130 return umemcmp_smart(p, q, n);
00131 #endif
00132 }
00133
00134 #else
00135
00136 inline int umemcmp(const void* p, const void* q, int n)
00137 {
00138 #if W_DEBUG_LEVEL > 2
00139
00140 int t1 = memcmp(p, q, n);
00141 int t2 = __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00142 w_assert3(t1 == t2 || (t1 < 0 && t2 < 0) || (t1 > 0 && t2 > 0));
00143 return t1;
00144 #else
00145 return memcmp(p, q, n);
00146 #endif
00147 }
00148
00149 #endif
00150
00151
00152
00153 #endif