os/ossrv/genericopenlibs/liboil/src/utf8_fast.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/liboil/src/utf8_fast.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,346 @@
     1.4 +/*
     1.5 + * LIBOIL - Library of Optimized Inner Loops
     1.6 + * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions
    1.11 + * are met:
    1.12 + * 1. Redistributions of source code must retain the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer.
    1.14 + * 2. Redistributions in binary form must reproduce the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer in the
    1.16 + *    documentation and/or other materials provided with the distribution.
    1.17 + * 
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    1.20 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    1.22 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.23 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.24 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.25 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.26 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    1.27 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +//Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
    1.31 +
    1.32 +#ifdef HAVE_CONFIG_H
    1.33 +#include "config.h"
    1.34 +#endif
    1.35 +
    1.36 +#include <liboil/liboil.h>
    1.37 +#include "liboil/utf8/utf8.h"
    1.38 +
    1.39 +
    1.40 +#ifdef HAVE_UNALIGNED_ACCESS
    1.41 +static void
    1.42 +utf8_validate_fast (int32_t *d_1, uint8_t *s, int n)
    1.43 +{
    1.44 +  int i;
    1.45 +  int extra_bytes;
    1.46 +  int mask;
    1.47 +
    1.48 +  i=0;
    1.49 +  while (i<n) {
    1.50 +    if (i < n-3 && (*(uint32_t *)(s+i) & 0x80808080) == 0) {
    1.51 +      i+=4;
    1.52 +      continue;
    1.53 +    }
    1.54 +    if (s[i] < 128) {
    1.55 +      i++;
    1.56 +      continue;
    1.57 +    }
    1.58 +    if ((s[i] & 0xe0) == 0xc0) {
    1.59 +      extra_bytes = 1;
    1.60 +      mask = 0x7f;
    1.61 +    } else if ((s[i] & 0xf0) == 0xe0) {
    1.62 +      extra_bytes = 2;
    1.63 +      mask = 0x1f;
    1.64 +    } else if ((s[i] & 0xf8) == 0xf0) {
    1.65 +      extra_bytes = 3;
    1.66 +      mask = 0x0f;
    1.67 +    } else {
    1.68 +      goto error;
    1.69 +    }
    1.70 +    if (i + extra_bytes >= n) goto error;
    1.71 +    while(extra_bytes--) {
    1.72 +      i++;
    1.73 +      if ((s[i] & 0xc0) != 0x80) goto error;
    1.74 +    }
    1.75 +    i++;
    1.76 +  }
    1.77 +
    1.78 +error:
    1.79 +  d_1[0] = i;
    1.80 +}
    1.81 +OIL_DEFINE_IMPL (utf8_validate_fast, utf8_validate);
    1.82 +#endif
    1.83 +
    1.84 +static void
    1.85 +utf8_validate_fast2 (int32_t *d_1, uint8_t *s, int n)
    1.86 +{
    1.87 +  int i;
    1.88 +  uint8_t x;
    1.89 +
    1.90 +  i=0;
    1.91 +  while (i<n) {
    1.92 +    x = s[i];
    1.93 +    if (!(x & 0x80)) {
    1.94 +      i++;
    1.95 +      continue;
    1.96 +    }
    1.97 +    x <<= 1;
    1.98 +    if (!(x & 0x80)) {
    1.99 +      goto error;
   1.100 +    }
   1.101 +    x <<= 1;
   1.102 +    if (!(x & 0x80)) {
   1.103 +      if (i + 1 >= n) goto error;
   1.104 +      i++;
   1.105 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.106 +      i++;
   1.107 +      continue;
   1.108 +    }
   1.109 +    x <<= 1;
   1.110 +    if (!(x & 0x80)) {
   1.111 +      if (i + 2 >= n) goto error;
   1.112 +      i++;
   1.113 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.114 +      i++;
   1.115 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.116 +      i++;
   1.117 +      continue;
   1.118 +    }
   1.119 +    x <<= 1;
   1.120 +    if (!(x & 0x80)) {
   1.121 +      if (i + 3 >= n) goto error;
   1.122 +      i++;
   1.123 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.124 +      i++;
   1.125 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.126 +      i++;
   1.127 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.128 +      i++;
   1.129 +      continue;
   1.130 +    }
   1.131 +    goto error;
   1.132 +  }
   1.133 +
   1.134 +error:
   1.135 +  d_1[0] = i;
   1.136 +}
   1.137 +OIL_DEFINE_IMPL (utf8_validate_fast2, utf8_validate);
   1.138 +
   1.139 +#ifdef HAVE_UNALIGNED_ACCESS
   1.140 +static void
   1.141 +utf8_validate_fast3 (int32_t *d_1, uint8_t *s, int n)
   1.142 +{
   1.143 +  int i;
   1.144 +  uint8_t x;
   1.145 +
   1.146 +  i=0;
   1.147 +  while (i<n) {
   1.148 +    if (i < n-3 && (*(uint32_t *)(s+i) & 0x80808080) == 0) {
   1.149 +      i+=4;
   1.150 +      continue;
   1.151 +    }
   1.152 +    x = s[i];
   1.153 +    if (!(x & 0x80)) {
   1.154 +      i++;
   1.155 +      continue;
   1.156 +    }
   1.157 +    if (!(x & 0x40)) {
   1.158 +      goto error;
   1.159 +    }
   1.160 +    if (!(x & 0x20)) {
   1.161 +      if (i + 1 >= n) goto error;
   1.162 +      i++;
   1.163 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.164 +      i++;
   1.165 +      continue;
   1.166 +    }
   1.167 +    if (!(x & 0x10)) {
   1.168 +      if (i + 2 >= n) goto error;
   1.169 +      i++;
   1.170 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.171 +      i++;
   1.172 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.173 +      i++;
   1.174 +      continue;
   1.175 +    }
   1.176 +    if (!(x & 0x08)) {
   1.177 +      if (i + 3 >= n) goto error;
   1.178 +      i++;
   1.179 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.180 +      i++;
   1.181 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.182 +      i++;
   1.183 +      if ((s[i] & 0xc0) != 0x80) goto error;
   1.184 +      i++;
   1.185 +      continue;
   1.186 +    }
   1.187 +    goto error;
   1.188 +  }
   1.189 +
   1.190 +error:
   1.191 +  d_1[0] = i;
   1.192 +}
   1.193 +OIL_DEFINE_IMPL (utf8_validate_fast3, utf8_validate);
   1.194 +#endif
   1.195 +
   1.196 +static uint8_t utf8_table[256] = {
   1.197 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.198 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.199 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.200 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.201 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.202 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.203 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.204 +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1.205 +  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
   1.206 +  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
   1.207 +  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
   1.208 +  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
   1.209 +  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1.210 +  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1.211 +  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
   1.212 +  3, 3, 3, 3, 3, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8
   1.213 +};
   1.214 +  
   1.215 +static void
   1.216 +utf8_validate_lookup (int32_t *d_1, uint8_t *s, int n)
   1.217 +{
   1.218 +  int i;
   1.219 +  uint8_t x;
   1.220 +
   1.221 +  i=0;
   1.222 +  while (i<n) {
   1.223 +    x = utf8_table[s[i]];
   1.224 +    if (x > 0) {
   1.225 +      if (x == 8 || i + x >= n) goto error;
   1.226 +      while (x>0) {
   1.227 +        i++;
   1.228 +        if ((s[i] & 0xc0) != 0x80) goto error;
   1.229 +        x--;
   1.230 +      }
   1.231 +    }
   1.232 +    i++;
   1.233 +  }
   1.234 +
   1.235 +error:
   1.236 +  d_1[0] = i;
   1.237 +}
   1.238 +OIL_DEFINE_IMPL (utf8_validate_lookup, utf8_validate);
   1.239 +
   1.240 +#if 0
   1.241 +static void
   1.242 +utf8_validate_asm1 (int32_t *d_1, uint8_t *s, int n)
   1.243 +{
   1.244 +  uint8_t *tmp = s;
   1.245 +
   1.246 +  asm (
   1.247 +      "1:\n"
   1.248 +      "  movb (%%eax), %%bl\n"
   1.249 +      "  testb %%bl, %%bl\n"
   1.250 +      //"  jns 3f\n"
   1.251 +      "  js 2f\n"
   1.252 +      "3:\n"
   1.253 +      "  addl $1, %%eax\n"
   1.254 +      "  subl $1, %%ecx\n"
   1.255 +      "  jne 1b\n"
   1.256 +      "2:\n"
   1.257 +    : "+a" (tmp), "+c" (n)
   1.258 +    :
   1.259 +    : "ebx" );
   1.260 +
   1.261 +  d_1[0] = tmp - s;
   1.262 +}
   1.263 +OIL_DEFINE_IMPL (utf8_validate_asm1, utf8_validate);
   1.264 +
   1.265 +static void
   1.266 +utf8_validate_asm2 (int32_t *d_1, uint8_t *s, int n)
   1.267 +{
   1.268 +  uint8_t *tmp = s;
   1.269 +
   1.270 +  asm (
   1.271 +      "1:\n"
   1.272 +      "  testl $0x80808080, (%%eax)\n"
   1.273 +      "  jne 2f\n"
   1.274 +      "  testl $0x80808080, 4(%%eax)\n"
   1.275 +      "  jne 2f\n"
   1.276 +      "  testl $0x80808080, 8(%%eax)\n"
   1.277 +      "  jne 2f\n"
   1.278 +      "  testl $0x80808080, 12(%%eax)\n"
   1.279 +      "  jne 2f\n"
   1.280 +      "  addl $16, %%eax\n"
   1.281 +      "  subl $16, %%ecx\n"
   1.282 +      "  jge 1b\n"
   1.283 +      "  jl 4f\n"
   1.284 +      "2:\n"
   1.285 +      "  movb (%%eax), %%bl\n"
   1.286 +      "  testb %%bl, %%bl\n"
   1.287 +      "  js 4f\n"
   1.288 +      "3:\n"
   1.289 +      "  addl $1, %%eax\n"
   1.290 +      "  subl $1, %%ecx\n"
   1.291 +      "  jne 1b\n"
   1.292 +      "4:\n"
   1.293 +    : "+a" (tmp), "+c" (n)
   1.294 +    :
   1.295 +    : "ebx" );
   1.296 +
   1.297 +  d_1[0] = tmp - s;
   1.298 +}
   1.299 +OIL_DEFINE_IMPL (utf8_validate_asm2, utf8_validate);
   1.300 +#endif
   1.301 +
   1.302 +
   1.303 +
   1.304 +#ifdef HAVE_UNALIGNED_ACCESS
   1.305 +#ifdef	__SYMBIAN32__
   1.306 + 
   1.307 +OilFunctionImpl* __oil_function_impl_utf8_validate_fast() {
   1.308 +		return &_oil_function_impl_utf8_validate_fast;
   1.309 +}
   1.310 +#endif
   1.311 +#endif
   1.312 +
   1.313 +#ifdef	__SYMBIAN32__
   1.314 + 
   1.315 +OilFunctionImpl* __oil_function_impl_utf8_validate_fast2() {
   1.316 +		return &_oil_function_impl_utf8_validate_fast2;
   1.317 +}
   1.318 +#endif
   1.319 +
   1.320 +#ifdef HAVE_UNALIGNED_ACCESS
   1.321 +#ifdef	__SYMBIAN32__
   1.322 + 
   1.323 +OilFunctionImpl* __oil_function_impl_utf8_validate_fast3() {
   1.324 +		return &_oil_function_impl_utf8_validate_fast3;
   1.325 +}
   1.326 +#endif
   1.327 +#endif
   1.328 +
   1.329 +#ifdef	__SYMBIAN32__
   1.330 + 
   1.331 +OilFunctionImpl* __oil_function_impl_utf8_validate_lookup() {
   1.332 +		return &_oil_function_impl_utf8_validate_lookup;
   1.333 +}
   1.334 +#endif
   1.335 +
   1.336 +#ifdef	__SYMBIAN32__
   1.337 + 
   1.338 +OilFunctionImpl* __oil_function_impl_utf8_validate_asm1() {
   1.339 +		return &_oil_function_impl_utf8_validate_asm1;
   1.340 +}
   1.341 +#endif
   1.342 +
   1.343 +#ifdef	__SYMBIAN32__
   1.344 + 
   1.345 +OilFunctionImpl* __oil_function_impl_utf8_validate_asm2() {
   1.346 +		return &_oil_function_impl_utf8_validate_asm2;
   1.347 +}
   1.348 +#endif
   1.349 +