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 #if defined(Sparc)
00067
00068 inline uint int_alignment_check(const void *i)
00069 {
00070 uint tmp = (ptrdiff_t) i & (sizeof(int)-1);
00071 w_assert9(tmp == (ptrdiff_t) i % sizeof(int));
00072 return tmp;
00073 }
00074 inline bool is_int_aligned(const void *i)
00075 {
00076 return int_alignment_check(i) == 0;
00077 }
00078
00079
00080
00081 inline int umemcmp_smart(const void* p_, const void* q_, int n)
00082 {
00083 const unsigned char* p = (const unsigned char*)p_;
00084 const unsigned char* q = (const unsigned char*)q_;
00085
00086
00087 if (n < (int)(2*sizeof(int)))
00088 return __umemcmp(p, q, n);
00089
00090
00091 if (int_alignment_check(p) == int_alignment_check(q)) {
00092 if (!is_int_aligned(p)) {
00093
00094 return __umemcmp(p, q, n);
00095 }
00096
00097
00098 uint i;
00099 for (i = 0; i < n/sizeof(int); i++) {
00100 if (((unsigned*)p)[i] != ((unsigned*)q)[i]) {
00101 return (((unsigned*)p)[i] > ((unsigned*)q)[i]) ? 1 : -1;
00102 }
00103 }
00104
00105 int j = i*sizeof(int);
00106 if (j) return __umemcmp(p+j, q+j, n-j);
00107 } else {
00108
00109 return __umemcmp(p, q, n);
00110 }
00111 return 0;
00112 }
00113
00114 inline int umemcmp_old(const void* p, const void* q, int n)
00115 {
00116 return __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00117 }
00118
00119 inline int umemcmp(const void* p, const void* q, int n)
00120 {
00121 #if W_DEBUG_LEVEL > 2
00122
00123 int t1 = umemcmp_smart(p, q, n);
00124 int t2 = __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00125 assert(t1 == t2 || (t1 < 0 && t2 < 0) || (t1 > 0 && t2 > 0));
00126 return t1;
00127 #else
00128 return umemcmp_smart(p, q, n);
00129 #endif
00130 }
00131
00132 #else
00133
00134 inline int umemcmp(const void* p, const void* q, int n)
00135 {
00136 #if W_DEBUG_LEVEL > 2
00137
00138 int t1 = memcmp(p, q, n);
00139 int t2 = __umemcmp((unsigned char*)p, (unsigned char*)q, n);
00140 w_assert3(t1 == t2 || (t1 < 0 && t2 < 0) || (t1 > 0 && t2 > 0));
00141 return t1;
00142 #else
00143 return memcmp(p, q, n);
00144 #endif
00145 }
00146
00147 #endif
00148
00149
00150
00151 #endif