os/ossrv/stdcpp/tsrc/Stdcpp_test/stdcxx/testengine/src/valcmp.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/stdcpp/tsrc/Stdcpp_test/stdcxx/testengine/src/valcmp.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,928 @@
     1.4 +
     1.5 +
     1.6 +/************************************************************************
     1.7 + *
     1.8 + * valcmp.cpp - definitions of the rw_valcmp() family of helper functions
     1.9 + *
    1.10 + * $Id: valcmp.cpp 332687 2005-11-12 00:47:57Z sebor $
    1.11 + *
    1.12 + ************************************************************************
    1.13 + *
    1.14 + * Copyright (c) 1994-2005 Quovadx,  Inc., acting through its  Rogue Wave
    1.15 + * Software division. Licensed under the Apache License, Version 2.0 (the
    1.16 + * "License");  you may  not use this file except  in compliance with the
    1.17 + * License.    You    may   obtain   a   copy   of    the   License    at
    1.18 + * http://www.apache.org/licenses/LICENSE-2.0.    Unless   required    by
    1.19 + * applicable law  or agreed to  in writing,  software  distributed under
    1.20 + * the License is distributed on an "AS IS" BASIS,  WITHOUT WARRANTIES OR
    1.21 + * CONDITIONS OF  ANY KIND, either  express or implied.  See  the License
    1.22 + * for the specific language governing permissions  and limitations under
    1.23 + * the License.
    1.24 + * 
    1.25 + **************************************************************************/
    1.26 +
    1.27 +// expand _TEST_EXPORT macros
    1.28 +#define _RWSTD_TEST_SRC
    1.29 +
    1.30 +#include <cfloat>
    1.31 +#include <valcmp.h>
    1.32 +
    1.33 +#include <stdio.h>    // for fprintf, stderr
    1.34 +#include <stdlib.h>   // for abort
    1.35 +#include <string.h>   // for memcmp
    1.36 +
    1.37 +/**************************************************************************/
    1.38 +
    1.39 +typedef unsigned char UChar;
    1.40 +
    1.41 +
    1.42 +static const UChar
    1.43 +_rw_upper [] = {
    1.44 +
    1.45 +#if 'A' == 0x41
    1.46 +
    1.47 +    // basic ASCII:
    1.48 +    //          .0  .1  .2  .3  .4  .5  .6  .7  .8  .9  .a  .b  .c  .d  .e  .f
    1.49 +    //         --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    1.50 +    //         NUL SOH STX ETX EOT ENQ ACK BEL BS  TAB  LF  VT  FF  CR  SO  SI
    1.51 +    /* 0. */ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
    1.52 +    //         DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN  EM SUB ESC  FS  GS  RS  US
    1.53 +    /* 1. */ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
    1.54 +    //         SPC   !   "   #   $   %   '   '   (   )   *   +   ,   -   .   /
    1.55 +    /* 2. */ "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
    1.56 +    //           0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?
    1.57 +    /* 3. */ "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
    1.58 +    //               A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
    1.59 +    /* 4. */ "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
    1.60 +    //           P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _
    1.61 +    /* 5. */ "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
    1.62 +    //           `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o
    1.63 +    /* 6. */ "\x60\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
    1.64 +    //           p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ DEL
    1.65 +    /* 7. */ "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x7b\x7c\x7d\x7e\x7f"
    1.66 +
    1.67 +    // extended ASCII:
    1.68 +    /* 8. */ "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
    1.69 +    /* 9. */ "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
    1.70 +    /* a. */ "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
    1.71 +    /* b. */ "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
    1.72 +    /* c. */ "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
    1.73 +    /* d. */ "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
    1.74 +    /* e. */ "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
    1.75 +    /* f. */ "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
    1.76 +
    1.77 +#elif 'A' == 0xc1
    1.78 +
    1.79 +    // EBCDIC:
    1.80 +    //          .0  .1  .2  .3  .4  .5  .6  .7  .8  .9  .a  .b  .c  .d  .e  .f
    1.81 +    //         --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
    1.82 +    //         NUL SOH STX ETX  PF  HT  LC DEL         SMM  VT  FF  CR  SO  SI
    1.83 +    /* 0. */ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
    1.84 +    //         DLE DC1 DC2  TM RES  NL  BS  IL CAN  EM  CC CU1 IFS IGS IRS IUS
    1.85 +    /* 1. */ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
    1.86 +    //          DS SOS  FS     BYP  LF ETB ESC          SM CU2     ENQ ACK BEL
    1.87 +    /* 2. */ "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
    1.88 +    //                 SYN      PN  RS  UC EOT             CU3 DC4 NAK     SUB
    1.89 +    /* 3. */ "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
    1.90 +    //          SP                                     ct.  .   <   (   +   |
    1.91 +    /* 4. */ "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
    1.92 +    //          &                                       !   $   *   )   ;   ~
    1.93 +    /* 5. */ "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
    1.94 +    //          -   /                                       ,   %   _   >   ?
    1.95 +    /* 6. */ "\x60\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
    1.96 +    //                                                  :   #   @   '   =   "
    1.97 +    /* 7. */ "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
    1.98 +    //              a   b   c   d   e   f   g   h   i
    1.99 +    /* 8. */ "\x80\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\x8a\x8b\x8c\x8d\x8e\x8f"
   1.100 +    //              j   k   l   m   n   o   p   q   r
   1.101 +    /* 9. */ "\x90\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\x9a\x9b\x9c\x9d\x9e\x9f"
   1.102 +    //                  s   t   u   v   w   x   y   z
   1.103 +    /* a. */ "\xa0\xa1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xaa\xab\xac\xad\xae\xaf"
   1.104 +    //                                            `
   1.105 +    /* b. */ "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
   1.106 +    //              A   B   C   D   E   F   G   H   I
   1.107 +    /* c. */ "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
   1.108 +    //              J   K   L   M   N   O   P   Q   R
   1.109 +    /* d. */ "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
   1.110 +    //                  S   T   U   V   W   X   Y   Z
   1.111 +    /* e. */ "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
   1.112 +    //          0   1   2   3   4   5   6   7   8   9
   1.113 +    /* f. */ "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
   1.114 +
   1.115 +#else   // 'A' != 0x41 && 'A' != 0xc1
   1.116 +#  error unknown character set (neither ASCII nor EBCDIC)
   1.117 +#endif   // ASCII or EBCDIC
   1.118 +
   1.119 +};
   1.120 +
   1.121 +
   1.122 +// returns 1 iff all size bytes of the object pointed to by buf are 0
   1.123 +static int
   1.124 +_rw_iszero (const void *buf, _RWSTD_SIZE_T size)
   1.125 +{
   1.126 +    for (_RWSTD_SIZE_T i = 0; i != size; ++i)
   1.127 +        if (_RWSTD_STATIC_CAST (const UChar*, buf)[i])
   1.128 +            return 0;
   1.129 +
   1.130 +    return 1;
   1.131 +}
   1.132 +
   1.133 +
   1.134 +// compares two objects of equal size
   1.135 +static int
   1.136 +_rw_cmpx (const void *buf1,
   1.137 +          const void *buf2,
   1.138 +          _RWSTD_SIZE_T nelems,
   1.139 +          _RWSTD_SIZE_T size,
   1.140 +          int           flags = 0)
   1.141 +{
   1.142 +    const UChar *p1 = _RWSTD_STATIC_CAST (const UChar*, buf1);
   1.143 +    const UChar *p2 = _RWSTD_STATIC_CAST (const UChar*, buf2);
   1.144 +
   1.145 +    int ret = 0;
   1.146 +
   1.147 +    for ( ; nelems; p1 += size, p2 += size, --nelems) {
   1.148 +
   1.149 +        ret = memcmp (p1, p2, size);
   1.150 +
   1.151 +        if (flags & CMP_NULTERM) {
   1.152 +
   1.153 +            const int zero1 = _rw_iszero (p1, size);
   1.154 +            const int zero2 = _rw_iszero (p2, size);
   1.155 +
   1.156 +            if (zero1 || zero2) {
   1.157 +                ret = zero1 - zero2;
   1.158 +                break;
   1.159 +            }
   1.160 +        }
   1.161 +
   1.162 +        if (ret && (flags & CMP_NOCASE)) {
   1.163 +            // FIXME: implement
   1.164 +            fprintf (stderr,
   1.165 +                     "%s:%d: case insensitive comparison not implemented\n",
   1.166 +                     __FILE__, __LINE__);
   1.167 +            abort ();
   1.168 +        }
   1.169 +    }
   1.170 +
   1.171 +    if (flags & CMP_RETOFF) {
   1.172 +        // FIXME: implement
   1.173 +        fprintf (stderr,
   1.174 +                 "%s:%d: returning offset not implemented\n",
   1.175 +                 __FILE__, __LINE__);
   1.176 +        abort ();
   1.177 +    }
   1.178 +
   1.179 +    return ret < 0 ? -1 : ret ? +1 : 0;
   1.180 +}
   1.181 +
   1.182 +
   1.183 +// compares two byte arrays
   1.184 +static int
   1.185 +_rw_cmp1 (const void *buf1,
   1.186 +          const void *buf2,
   1.187 +          _RWSTD_SIZE_T nelems,
   1.188 +          int           flags = 0)
   1.189 +{
   1.190 +#ifdef _RWSTD_UINT8_T
   1.191 +
   1.192 +    typedef _RWSTD_UINT8_T UI8T;
   1.193 +
   1.194 +    if (0 == flags) {
   1.195 +        const int ret = memcmp (buf1, buf2, nelems);
   1.196 +        return ret < 0 ? -1 : ret ? +1 : 0;
   1.197 +    }
   1.198 +
   1.199 +    if (CMP_NULTERM == flags) {
   1.200 +        const int ret =
   1.201 +            strncmp (_RWSTD_STATIC_CAST (const char*, buf1),
   1.202 +                     _RWSTD_STATIC_CAST (const char*, buf2),
   1.203 +                     nelems);
   1.204 +        return ret < 0 ? -1 : ret ? +1 : 0;
   1.205 +    }
   1.206 +
   1.207 +    int ret = 0;
   1.208 +    int inx = 0;
   1.209 +
   1.210 +    const UI8T *pi1 = _RWSTD_STATIC_CAST (const UI8T*, buf1);
   1.211 +    const UI8T *pi2 = _RWSTD_STATIC_CAST (const UI8T*, buf2);
   1.212 +
   1.213 +    for (; nelems; ++pi1, ++pi2, --nelems, ++inx) {
   1.214 +
   1.215 +        const UI8T ui1 = *pi1;
   1.216 +        const UI8T ui2 = *pi2;
   1.217 +
   1.218 +        if (flags & CMP_NULTERM) {
   1.219 +
   1.220 +            if (0 == ui1) {
   1.221 +                ret = ui2 ? -1 : 0;
   1.222 +                break;
   1.223 +            }
   1.224 +
   1.225 +            if (0 == ui2) {
   1.226 +                ret = +1;
   1.227 +                break;
   1.228 +            }
   1.229 +        }
   1.230 +
   1.231 +        if (ui1 != ui2) {
   1.232 +            if (flags & CMP_NOCASE) {
   1.233 +                if (_rw_upper [ui1] != _rw_upper [ui2]) {
   1.234 +                    ret = _rw_upper [ui1] < _rw_upper [ui2] ? -1 : +1;
   1.235 +                    break;
   1.236 +                }
   1.237 +            }
   1.238 +            else {
   1.239 +                ret = ui1 < ui2 ? -1 : +1;
   1.240 +                break;
   1.241 +            }
   1.242 +        }
   1.243 +    }
   1.244 +
   1.245 +    if (CMP_RETOFF & flags) {
   1.246 +        ret = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, inx) < nelems ? inx : -1;
   1.247 +    }
   1.248 +            
   1.249 +    return ret;
   1.250 +
   1.251 +#else // if !defined (_RWSTD_UINT8_T)
   1.252 +#  error _RWSTD_UINT8_T not #defined (configuration problem?)
   1.253 +#endif   // _RWSTD_UINT8_T
   1.254 +
   1.255 +}
   1.256 +
   1.257 +
   1.258 +// compares two arrays of objects each 16 bits in size
   1.259 +static int
   1.260 +_rw_cmp2 (const void *buf1,
   1.261 +          const void *buf2,
   1.262 +          _RWSTD_SIZE_T nelems,
   1.263 +          int           flags = 0)
   1.264 +{
   1.265 +#ifdef _RWSTD_UINT16_T
   1.266 +
   1.267 +    typedef _RWSTD_UINT16_T UI16T;
   1.268 +
   1.269 +    int ret = 0;
   1.270 +    int inx = 0;
   1.271 +
   1.272 +    const UI16T *pi1 = _RWSTD_STATIC_CAST (const UI16T*, buf1);
   1.273 +    const UI16T *pi2 = _RWSTD_STATIC_CAST (const UI16T*, buf2);
   1.274 +
   1.275 +    for (; nelems; ++pi1, ++pi2, --nelems) {
   1.276 +
   1.277 +        const UI16T ui1 = *pi1;
   1.278 +        const UI16T ui2 = *pi2;
   1.279 +
   1.280 +        if (flags & CMP_NULTERM) {
   1.281 +
   1.282 +            if (0 == ui1) {
   1.283 +                ret = ui2 ? -1 : 0;
   1.284 +                break;
   1.285 +            }
   1.286 +
   1.287 +            if (0 == ui2) {
   1.288 +                ret = +1;
   1.289 +                break;
   1.290 +            }
   1.291 +        }
   1.292 +
   1.293 +        if (ui1 != ui2) {
   1.294 +            if (flags & CMP_NOCASE) {
   1.295 +                if (_RWSTD_UCHAR_MAX < ui1 || _RWSTD_UCHAR_MAX < ui2) {
   1.296 +                    ret = ui1 < ui2 ? -1 : +1;
   1.297 +                    break;
   1.298 +                }
   1.299 +                else if (_rw_upper [ui1] != _rw_upper [ui2]) {
   1.300 +                    ret = _rw_upper [ui1] < _rw_upper [ui2] ? -1 : +1;
   1.301 +                    break;
   1.302 +                }
   1.303 +            }
   1.304 +            else {
   1.305 +                ret = ui1 < ui2 ? -1 : +1;
   1.306 +                break;
   1.307 +            }
   1.308 +        }
   1.309 +    }
   1.310 +
   1.311 +    if (CMP_RETOFF & flags) {
   1.312 +        ret = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, inx) < nelems ? inx : -1;
   1.313 +    }
   1.314 +            
   1.315 +    return ret;
   1.316 +
   1.317 +#else   // if !defined (_RWSTD_UINT16_T)
   1.318 +
   1.319 +    return _rw_cmpx (buf1, buf2, nelems, 2, flags);
   1.320 +
   1.321 +#endif   // _RWSTD_UINT16_T
   1.322 +}
   1.323 +
   1.324 +
   1.325 +// compares two arrays of objects each 32 bits in size
   1.326 +static int
   1.327 +_rw_cmp4 (const void *buf1,
   1.328 +          const void *buf2,
   1.329 +          _RWSTD_SIZE_T nelems,
   1.330 +          int           flags = 0)
   1.331 +{
   1.332 +#ifdef _RWSTD_UINT32_T
   1.333 +
   1.334 +    typedef _RWSTD_UINT32_T UI32T;
   1.335 +
   1.336 +    int ret = 0;
   1.337 +    int inx = 0;
   1.338 +
   1.339 +    const UI32T *pi1 = _RWSTD_STATIC_CAST (const UI32T*, buf1);
   1.340 +    const UI32T *pi2 = _RWSTD_STATIC_CAST (const UI32T*, buf2);
   1.341 +
   1.342 +    for (; nelems; ++pi1, ++pi2, --nelems) {
   1.343 +
   1.344 +        const UI32T ui1 = *pi1;
   1.345 +        const UI32T ui2 = *pi2;
   1.346 +
   1.347 +        if (flags & CMP_NULTERM) {
   1.348 +
   1.349 +            if (0 == ui1) {
   1.350 +                ret = ui2 ? -1 : 0;
   1.351 +                break;
   1.352 +            }
   1.353 +
   1.354 +            if (0 == ui2) {
   1.355 +                ret = +1;
   1.356 +                break;
   1.357 +            }
   1.358 +        }
   1.359 +
   1.360 +        if (ui1 != ui2) {
   1.361 +            if (flags & CMP_NOCASE) {
   1.362 +                if (_RWSTD_UCHAR_MAX < ui1 || _RWSTD_UCHAR_MAX < ui2) {
   1.363 +                    ret = ui1 < ui2 ? -1 : +1;
   1.364 +                    break;
   1.365 +                }
   1.366 +                else if (_rw_upper [ui1] != _rw_upper [ui2]) {
   1.367 +                    ret = _rw_upper [ui1] < _rw_upper [ui2] ? -1 : +1;
   1.368 +                    break;
   1.369 +                }
   1.370 +            }
   1.371 +            else {
   1.372 +                ret = ui1 < ui2 ? -1 : +1;
   1.373 +                break;
   1.374 +            }
   1.375 +        }
   1.376 +    }
   1.377 +
   1.378 +    if (CMP_RETOFF & flags) {
   1.379 +        ret = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, inx) < nelems ? inx : -1;
   1.380 +    }
   1.381 +            
   1.382 +    return ret;
   1.383 +
   1.384 +#else   // if !defined (_RWSTD_UINT32_T)
   1.385 +
   1.386 +    return _rw_cmpx (buf1, buf2, nelems, 4, flags);
   1.387 +
   1.388 +#endif   // _RWSTD_UINT32_T
   1.389 +}
   1.390 +
   1.391 +
   1.392 +// compares two arrays of objects each 64 bits in size
   1.393 +static int
   1.394 +_rw_cmp8 (const void *buf1,
   1.395 +          const void *buf2,
   1.396 +          _RWSTD_SIZE_T nelems,
   1.397 +          int           flags = 0)
   1.398 +{
   1.399 +#ifdef _RWSTD_UINT64_T
   1.400 +
   1.401 +    typedef _RWSTD_UINT64_T UI64T;
   1.402 +
   1.403 +    int ret = 0;
   1.404 +    int inx = 0;
   1.405 +
   1.406 +    const UI64T *pi1 = _RWSTD_STATIC_CAST (const UI64T*, buf1);
   1.407 +    const UI64T *pi2 = _RWSTD_STATIC_CAST (const UI64T*, buf2);
   1.408 +
   1.409 +    for (; nelems; ++pi1, ++pi2, --nelems) {
   1.410 +
   1.411 +        const UI64T ui1 = *pi1;
   1.412 +        const UI64T ui2 = *pi2;
   1.413 +
   1.414 +        if (flags & CMP_NULTERM) {
   1.415 +
   1.416 +            if (0 == ui1) {
   1.417 +                ret = ui2 ? -1 : 0;
   1.418 +                break;
   1.419 +            }
   1.420 +
   1.421 +            if (0 == ui2) {
   1.422 +                ret = +1;
   1.423 +                break;
   1.424 +            }
   1.425 +        }
   1.426 +
   1.427 +        if (ui1 != ui2) {
   1.428 +            if (flags & CMP_NOCASE) {
   1.429 +                if (_RWSTD_UCHAR_MAX < ui1 || _RWSTD_UCHAR_MAX < ui2) {
   1.430 +                    ret = ui1 < ui2 ? -1 : +1;
   1.431 +                    break;
   1.432 +                }
   1.433 +                else if (_rw_upper [ui1] != _rw_upper [ui2]) {
   1.434 +                    ret = _rw_upper [ui1] < _rw_upper [ui2] ? -1 : +1;
   1.435 +                    break;
   1.436 +                }
   1.437 +            }
   1.438 +            else {
   1.439 +                ret = ui1 < ui2 ? -1 : +1;
   1.440 +                break;
   1.441 +            }
   1.442 +        }
   1.443 +    }
   1.444 +
   1.445 +    if (CMP_RETOFF & flags) {
   1.446 +        ret = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, inx) < nelems ? inx : -1;
   1.447 +    }
   1.448 +            
   1.449 +    return ret;
   1.450 +
   1.451 +#else   // if !defined (_RWSTD_UINT64_T)
   1.452 +
   1.453 +    return _rw_cmpx (buf1, buf2, nelems, 8, flags);
   1.454 +
   1.455 +#endif   // _RWSTD_UINT64_T
   1.456 +}
   1.457 +
   1.458 +
   1.459 +// compares two arrays of objects of unequal size
   1.460 +static int
   1.461 +_rw_cmpxx (const void*   buf1,
   1.462 +           const void*   buf2,
   1.463 +           _RWSTD_SIZE_T nelems,
   1.464 +           _RWSTD_SIZE_T size1,
   1.465 +           _RWSTD_SIZE_T size2,
   1.466 +           int           flags)
   1.467 +{
   1.468 +    int ret = 0;
   1.469 +    int inx = 0;
   1.470 +
   1.471 +    for (; nelems; --nelems, ++inx) {
   1.472 +
   1.473 +#ifdef _RWSTD_UINT64_T
   1.474 +
   1.475 +        _RWSTD_UINT64_T ui1;
   1.476 +        _RWSTD_UINT64_T ui2;
   1.477 +
   1.478 +#else   // if !defined (_RWSTD_UINT64_T)
   1.479 +
   1.480 +        _RWSTD_SIZE_T ui1;
   1.481 +        _RWSTD_SIZE_T ui2;
   1.482 +
   1.483 +#endif   // _RWSTD_UINT64_T
   1.484 +
   1.485 +        switch (size1) {
   1.486 +
   1.487 +#ifdef _RWSTD_UINT8_T
   1.488 +
   1.489 +        case 1: {
   1.490 +            const _RWSTD_UINT8_T* ptr =
   1.491 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT8_T*, buf1);
   1.492 +            ui1 = *ptr++;
   1.493 +            buf1 = ptr;
   1.494 +            break;
   1.495 +        }
   1.496 +
   1.497 +#endif   // _RWSTD_UINT8_T
   1.498 +
   1.499 +#ifdef _RWSTD_UINT16_T
   1.500 +
   1.501 +        case 2: {
   1.502 +            const _RWSTD_UINT16_T* ptr =
   1.503 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT16_T*, buf1);
   1.504 +            ui1 = *ptr++;
   1.505 +            buf1 = ptr;
   1.506 +            break;
   1.507 +        }
   1.508 +
   1.509 +#endif   // _RWSTD_UINT16_T
   1.510 +
   1.511 +#ifdef _RWSTD_UINT32_T
   1.512 +
   1.513 +        case 4: {
   1.514 +            const _RWSTD_UINT32_T* ptr =
   1.515 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT32_T*, buf1);
   1.516 +            ui1 = *ptr++;
   1.517 +            buf1 = ptr;
   1.518 +            break;
   1.519 +        }
   1.520 +
   1.521 +#endif   // _RWSTD_UINT32_T
   1.522 +
   1.523 +#ifdef _RWSTD_UINT64_T
   1.524 +
   1.525 +        case 8: {
   1.526 +            const _RWSTD_UINT64_T* ptr =
   1.527 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT64_T*, buf1);
   1.528 +            ui1 = *ptr++;
   1.529 +            buf1 = ptr;
   1.530 +            break;
   1.531 +        }
   1.532 +
   1.533 +#endif   // _RWSTD_UINT64_T
   1.534 +
   1.535 +        default:
   1.536 +            fprintf (stderr,
   1.537 +                     "%s:%d: comparison of objects %u and %u bytes in size "
   1.538 +                     "not implemented\n", __FILE__, __LINE__,
   1.539 +                     unsigned (size1), unsigned (size2));
   1.540 +            abort ();
   1.541 +        }
   1.542 +
   1.543 +        switch (size2) {
   1.544 +
   1.545 +#ifdef _RWSTD_UINT8_T
   1.546 +
   1.547 +        case 1: {
   1.548 +            const _RWSTD_UINT8_T* ptr =
   1.549 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT8_T*, buf2);
   1.550 +            ui2 = *ptr++;
   1.551 +            buf2 = ptr;
   1.552 +            break;
   1.553 +        }
   1.554 +
   1.555 +#endif   // _RWSTD_UINT8_T
   1.556 +
   1.557 +#ifdef _RWSTD_UINT16_T
   1.558 +
   1.559 +        case 2: {
   1.560 +            const _RWSTD_UINT16_T* ptr =
   1.561 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT16_T*, buf2);
   1.562 +            ui2 = *ptr++;
   1.563 +            buf2 = ptr;
   1.564 +            break;
   1.565 +        }
   1.566 +
   1.567 +#endif   // _RWSTD_UINT16_T
   1.568 +
   1.569 +#ifdef _RWSTD_UINT32_T
   1.570 +
   1.571 +        case 4: {
   1.572 +            const _RWSTD_UINT32_T* ptr =
   1.573 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT32_T*, buf2);
   1.574 +            ui2 = *ptr++;
   1.575 +            buf2 = ptr;
   1.576 +            break;
   1.577 +        }
   1.578 +
   1.579 +#endif   // _RWSTD_UINT32_T
   1.580 +
   1.581 +#ifdef _RWSTD_UINT64_T
   1.582 +
   1.583 +        case 8: {
   1.584 +            const _RWSTD_UINT64_T* ptr =
   1.585 +                _RWSTD_STATIC_CAST (const _RWSTD_UINT64_T*, buf2);
   1.586 +            ui2 = *ptr++;
   1.587 +            buf2 = ptr;
   1.588 +            break;
   1.589 +        }
   1.590 +
   1.591 +#endif   // _RWSTD_UINT64_T
   1.592 +
   1.593 +        default:
   1.594 +            fprintf (stderr,
   1.595 +                     "%s:%d: comparison of objects %u and %u bytes in size "
   1.596 +                     "not implemented\n", __FILE__, __LINE__,
   1.597 +                     unsigned (size1), unsigned (size2));
   1.598 +            abort ();
   1.599 +        }
   1.600 +
   1.601 +        if (flags & CMP_NULTERM) {
   1.602 +
   1.603 +            if (0 == ui1) {
   1.604 +                ret = ui2 ? -1 : 0;
   1.605 +                break;
   1.606 +            }
   1.607 +
   1.608 +            if (0 == ui2) {
   1.609 +                ret = +1;
   1.610 +                break;
   1.611 +            }
   1.612 +        }
   1.613 +
   1.614 +        if (ui1 != ui2) {
   1.615 +            if (flags & CMP_NOCASE) {
   1.616 +                if (_RWSTD_UCHAR_MAX < ui1 || _RWSTD_UCHAR_MAX < ui2) {
   1.617 +                    ret = ui1 < ui2 ? -1 : +1;
   1.618 +                    break;
   1.619 +                }
   1.620 +                else if (_rw_upper [ui1] != _rw_upper [ui2]) {
   1.621 +                    ret = _rw_upper [ui1] < _rw_upper [ui2] ? -1 : +1;
   1.622 +                    break;
   1.623 +                }
   1.624 +            }
   1.625 +            else {
   1.626 +                ret = ui1 < ui2 ? -1 : +1;
   1.627 +                break;
   1.628 +            }
   1.629 +        }
   1.630 +    }
   1.631 +
   1.632 +    if (CMP_RETOFF & flags) {
   1.633 +        ret = _RWSTD_STATIC_CAST (_RWSTD_SIZE_T, inx) < nelems ? inx : -1;
   1.634 +    }
   1.635 +
   1.636 +    return ret;
   1.637 +}
   1.638 +
   1.639 +
   1.640 +_TEST_EXPORT int
   1.641 +rw_valcmp (const void*   buf1,
   1.642 +           const void*   buf2,
   1.643 +           _RWSTD_SIZE_T nelems,
   1.644 +           _RWSTD_SIZE_T size1,
   1.645 +           _RWSTD_SIZE_T size2,
   1.646 +           int           flags /* = 0 */)
   1.647 +{
   1.648 +    if (size1 == size2) {
   1.649 +
   1.650 +        switch (size1) {
   1.651 +        case 1: return _rw_cmp1 (buf1, buf2, nelems, flags);
   1.652 +        case 2: return _rw_cmp2 (buf1, buf2, nelems, flags);
   1.653 +        case 4: return _rw_cmp4 (buf1, buf2, nelems, flags);
   1.654 +        case 8: return _rw_cmp8 (buf1, buf2, nelems, flags);
   1.655 +        }
   1.656 +
   1.657 +        return _rw_cmpx (buf1, buf2, nelems, size1, flags);
   1.658 +    }
   1.659 +
   1.660 +    return _rw_cmpxx (buf1, buf2, nelems, size1, size2, flags);
   1.661 +}
   1.662 +
   1.663 +
   1.664 +_TEST_EXPORT int
   1.665 +rw_strncmp (const char*   str1,
   1.666 +            const char*   str2,
   1.667 +            _RWSTD_SIZE_T nelems /* = _RWSTD_SIZE_MAX */,
   1.668 +           int            flags /* = CMP_NULTERM */)
   1.669 +{
   1.670 +    return rw_valcmp (str1, str2, nelems, 1, 1, flags);
   1.671 +}
   1.672 +
   1.673 +
   1.674 +#ifndef _RWSTD_NO_WCHAR_T
   1.675 +
   1.676 +_TEST_EXPORT int
   1.677 +rw_strncmp (const char*    str1,
   1.678 +            const wchar_t* str2,
   1.679 +            _RWSTD_SIZE_T  nelems /* = _RWSTD_SIZE_MAX */,
   1.680 +           int             flags /* = CMP_NULTERM */)
   1.681 +{
   1.682 +    return rw_valcmp (str1, str2, nelems, 1, sizeof (wchar_t), flags);
   1.683 +}
   1.684 +
   1.685 +
   1.686 +_TEST_EXPORT int
   1.687 +rw_strncmp (const wchar_t* str1,
   1.688 +            const char*    str2,
   1.689 +            _RWSTD_SIZE_T  nelems /* = _RWSTD_SIZE_MAX */,
   1.690 +            int             flags /* = CMP_NULTERM */)
   1.691 +{
   1.692 +    return rw_valcmp (str1, str2, nelems, sizeof (wchar_t), 1, flags);
   1.693 +}
   1.694 +
   1.695 +
   1.696 +_TEST_EXPORT int
   1.697 +rw_strncmp (const wchar_t* str1,
   1.698 +            const wchar_t* str2,
   1.699 +            _RWSTD_SIZE_T  nelems /* = _RWSTD_SIZE_MAX */,
   1.700 +           int             flags /* = CMP_NULTERM */)
   1.701 +{
   1.702 +    return rw_valcmp (str1, str2, nelems,
   1.703 +                      sizeof (wchar_t), sizeof (wchar_t), flags);
   1.704 +}
   1.705 +
   1.706 +#endif   // _RWSTD_NO_WCHAR_T
   1.707 +
   1.708 +
   1.709 +// floating point comparison helpers based on
   1.710 +// http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
   1.711 +
   1.712 +_TEST_EXPORT int
   1.713 +rw_fltcmp (float x, float y)
   1.714 +{
   1.715 +#if _RWSTD_SHRT_SIZE == _RWSTD_INT_SIZE
   1.716 +    typedef short IntT;
   1.717 +    #ifdef __ARMCC__
   1.718 +    #pragma diag_suppress 68
   1.719 +    #endif
   1.720 +    const IntT imin = _RWSTD_SHRT_MIN;
   1.721 +    
   1.722 +#elif _RWSTD_FLT_SIZE == _RWSTD_INT_SIZE
   1.723 +    typedef int IntT;
   1.724 +    const IntT imin = _RWSTD_INT_MIN;
   1.725 +#elif _RWSTD_FLT_SIZE == _RWSTD_LONG_SIZE
   1.726 +    typedef long IntT;
   1.727 +    const IntT imin = _RWSTD_LONG_MIN;
   1.728 +#elif _RWSTD_FLT_SIZE == _RWSTD_LLONG_SIZE
   1.729 +    typedef _RWSTD_LONG_LONG IntT;
   1.730 +    const IntT imin = _RWSTD_LLONG_MIN;
   1.731 +#else
   1.732 +    // ???
   1.733 +#  error no integral type of the same size as float exists
   1.734 +#endif
   1.735 +
   1.736 +    if (x == y)
   1.737 +        return 0;
   1.738 +
   1.739 +    // make both arguments lexicographically ordered as twos-complement ints
   1.740 +    IntT x_int = *(IntT*)&x;
   1.741 +    if (x_int < 0)
   1.742 +        x_int = imin - x_int;
   1.743 +
   1.744 +    IntT y_int = *(IntT*)&y;
   1.745 +    if (y_int < 0)
   1.746 +        y_int = imin - y_int;
   1.747 +
   1.748 +    const IntT int_diff = x_int - y_int;
   1.749 +
   1.750 +    return int_diff;
   1.751 +}
   1.752 +
   1.753 +
   1.754 +#define Abs(x) ((x) < 0 ? -(x) : (x))
   1.755 +
   1.756 +
   1.757 +_TEST_EXPORT int
   1.758 +rw_dblcmp (double x, double y)
   1.759 +{
   1.760 +#if _RWSTD_DBL_SIZE == _RWSTD_INT_SIZE
   1.761 +    typedef int IntT;
   1.762 +    const IntT imin = _RWSTD_INT_MIN;
   1.763 +#elif _RWSTD_DBL_SIZE == _RWSTD_LONG_SIZE
   1.764 +    typedef long IntT;
   1.765 +    const IntT imin = _RWSTD_LONG_MIN;
   1.766 +#elif _RWSTD_DBL_SIZE == _RWSTD_LLONG_SIZE
   1.767 +    typedef _RWSTD_LONG_LONG IntT;
   1.768 +    const IntT imin = _RWSTD_LLONG_MIN;
   1.769 +#endif
   1.770 +
   1.771 +#if _RWSTD_LLONG_SIZE < _RWSTD_DBL_SIZE
   1.772 +
   1.773 +    if (x == y)
   1.774 +        return 0;
   1.775 +
   1.776 +    // FIXME: use integer math as in the functions above
   1.777 +
   1.778 +    const double diff = x - y;
   1.779 +
   1.780 +    // check absolute error
   1.781 +    if (Abs (diff) < _RWSTD_DBL_EPSILON)
   1.782 +        return 0;
   1.783 +
   1.784 +    // check relative error
   1.785 +    const double relerr =
   1.786 +        Abs (x) < Abs (y) ? Abs (diff / y) : Abs (diff / x);
   1.787 +
   1.788 +    if (relerr <= 0.0000001)
   1.789 +        return 0;
   1.790 +
   1.791 +    return x < y ? -1 : +1;
   1.792 +
   1.793 +#else   // if !(_RWSTD_LLONG_SIZE < _RWSTD_DBL_SIZE)
   1.794 +
   1.795 +    if (x == y)
   1.796 +        return 0;
   1.797 +
   1.798 +    IntT x_int = *(IntT*)&x;
   1.799 +    if (x_int < 0)
   1.800 +        x_int = imin - x_int;
   1.801 +
   1.802 +    IntT y_int = *(IntT*)&y;
   1.803 +    if (y_int < 0)
   1.804 +        y_int = imin - y_int;
   1.805 +
   1.806 +    const IntT int_diff = x_int - y_int;
   1.807 +
   1.808 +    return int_diff;
   1.809 +
   1.810 +#endif   // _RWSTD_LLONG_SIZE < _RWSTD_DBL_SIZE
   1.811 +
   1.812 +}
   1.813 +
   1.814 +
   1.815 +#ifndef _RWSTD_NO_LONG_DOUBLE
   1.816 +_TEST_EXPORT int
   1.817 +rw_ldblcmp (long double x, long double y)
   1.818 +{
   1.819 +    if (sizeof (long double) == sizeof (double))
   1.820 +        return rw_dblcmp (double (x), double (y));
   1.821 +
   1.822 +    #ifdef __ARMCC__
   1.823 +    #pragma diag_suppress 111
   1.824 +    #endif
   1.825 +    if (x == y)
   1.826 +        return 0;
   1.827 +    
   1.828 +    
   1.829 +
   1.830 +    // FIXME: use integer math as in the functions above
   1.831 +
   1.832 +    const long double diff = x - y;
   1.833 +
   1.834 +    // check absolute error
   1.835 +    if (Abs (diff) < _RWSTD_LDBL_EPSILON)
   1.836 +        return 0;
   1.837 +
   1.838 +    // check relative error
   1.839 +    const long double relerr =
   1.840 +        Abs (x) < Abs (y) ? Abs (diff / y) : Abs (diff / x);
   1.841 +
   1.842 +    if (relerr <= 0.0000001L)
   1.843 +        return 0;
   1.844 +
   1.845 +    return x < y ? -1 : +1;
   1.846 +}
   1.847 +
   1.848 +#endif   // _RWSTD_NO_LONG_DOUBLE
   1.849 +
   1.850 +#ifdef __SYMBIAN32__
   1.851 +_TEST_EXPORT int 
   1.852 + rw_strcmp (std::basic_string<char>& x ,/*std::basic_string<char> */ long double  y)
   1.853 + {
   1.854 +   const char * c= x.c_str(); 
   1.855 +   
   1.856 +   /*if(x.compare(y)==0)
   1.857 +       return 0;
   1.858 +   else
   1.859 +     return x.compare(y)<0?-1:+1;	      */
   1.860 +   float z=atof(c);
   1.861 +   
   1.862 +   if ((long double)z == y)
   1.863 +        return 0;
   1.864 +
   1.865 +    // FIXME: use integer math as in the functions above
   1.866 +
   1.867 +    const long double diff =((long double)z) - y;
   1.868 +
   1.869 +    // check absolute error
   1.870 +    if (Abs (diff) < _RWSTD_LDBL_EPSILON)
   1.871 +        return 0;
   1.872 +
   1.873 +    // check relative error
   1.874 +    const long double relerr =
   1.875 +        Abs ((long double)z) < Abs (y) ? Abs (diff / y) : Abs (diff /(long double)z);
   1.876 +
   1.877 +    if (relerr <= 0.0000001L)
   1.878 +        return 0;
   1.879 +
   1.880 +    return z < y ? -1 : +1;
   1.881 +     
   1.882 + }
   1.883 + 
   1.884 +_TEST_EXPORT int 
   1.885 + rw_strcmp (std::basic_string<wchar_t>& x ,long double y)
   1.886 +{
   1.887 + /*  if(x.compare(y)==0)
   1.888 +       return 0;
   1.889 +   else
   1.890 +     return x.compare(y)<0?-1:+1;	      */
   1.891 +  
   1.892 + int len=x.length();
   1.893 + const wchar_t *w=x.c_str();
   1.894 + char c[128];
   1.895 + WCtoC(w,len,c);
   1.896 + 
   1.897 +  float z=atof(c);
   1.898 +   
   1.899 +   if ((long double)z == y)
   1.900 +        return 0;
   1.901 +
   1.902 +    // FIXME: use integer math as in the functions above
   1.903 +
   1.904 +    const long double diff =((long double)z) - y;
   1.905 +
   1.906 +    // check absolute error
   1.907 +    if (Abs (diff) < _RWSTD_LDBL_EPSILON)
   1.908 +        return 0;
   1.909 +
   1.910 +    // check relative error
   1.911 +    const long double relerr =
   1.912 +        Abs ((long double)z) < Abs (y) ? Abs (diff / y) : Abs (diff /(long double)z);
   1.913 +
   1.914 +    if (relerr <= 0.0000001L)
   1.915 +        return 0;
   1.916 +
   1.917 +    return z < y ? -1 : +1;      
   1.918 +     
   1.919 + }   
   1.920 +_TEST_EXPORT
   1.921 + void WCtoC(const wchar_t *wstr, int len, char* str)
   1.922 +{
   1.923 +
   1.924 +	int i = 0;
   1.925 +	for (;i<len;i++)
   1.926 +		str[i] = wstr[i]; 
   1.927 +
   1.928 +}
   1.929 +
   1.930 +
   1.931 + #endif