os/ossrv/genericopenlibs/liboil/src/utf8_fast.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
 * LIBOIL - Library of Optimized Inner Loops
sl@0
     3
 * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
sl@0
     4
 * All rights reserved.
sl@0
     5
 *
sl@0
     6
 * Redistribution and use in source and binary forms, with or without
sl@0
     7
 * modification, are permitted provided that the following conditions
sl@0
     8
 * are met:
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer.
sl@0
    11
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    12
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    13
 *    documentation and/or other materials provided with the distribution.
sl@0
    14
 * 
sl@0
    15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
sl@0
    16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sl@0
    17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    18
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
sl@0
    19
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sl@0
    20
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sl@0
    21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    23
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sl@0
    24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sl@0
    25
 * POSSIBILITY OF SUCH DAMAGE.
sl@0
    26
 */
sl@0
    27
//Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
sl@0
    28
sl@0
    29
#ifdef HAVE_CONFIG_H
sl@0
    30
#include "config.h"
sl@0
    31
#endif
sl@0
    32
sl@0
    33
#include <liboil/liboil.h>
sl@0
    34
#include "liboil/utf8/utf8.h"
sl@0
    35
sl@0
    36
sl@0
    37
#ifdef HAVE_UNALIGNED_ACCESS
sl@0
    38
static void
sl@0
    39
utf8_validate_fast (int32_t *d_1, uint8_t *s, int n)
sl@0
    40
{
sl@0
    41
  int i;
sl@0
    42
  int extra_bytes;
sl@0
    43
  int mask;
sl@0
    44
sl@0
    45
  i=0;
sl@0
    46
  while (i<n) {
sl@0
    47
    if (i < n-3 && (*(uint32_t *)(s+i) & 0x80808080) == 0) {
sl@0
    48
      i+=4;
sl@0
    49
      continue;
sl@0
    50
    }
sl@0
    51
    if (s[i] < 128) {
sl@0
    52
      i++;
sl@0
    53
      continue;
sl@0
    54
    }
sl@0
    55
    if ((s[i] & 0xe0) == 0xc0) {
sl@0
    56
      extra_bytes = 1;
sl@0
    57
      mask = 0x7f;
sl@0
    58
    } else if ((s[i] & 0xf0) == 0xe0) {
sl@0
    59
      extra_bytes = 2;
sl@0
    60
      mask = 0x1f;
sl@0
    61
    } else if ((s[i] & 0xf8) == 0xf0) {
sl@0
    62
      extra_bytes = 3;
sl@0
    63
      mask = 0x0f;
sl@0
    64
    } else {
sl@0
    65
      goto error;
sl@0
    66
    }
sl@0
    67
    if (i + extra_bytes >= n) goto error;
sl@0
    68
    while(extra_bytes--) {
sl@0
    69
      i++;
sl@0
    70
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
    71
    }
sl@0
    72
    i++;
sl@0
    73
  }
sl@0
    74
sl@0
    75
error:
sl@0
    76
  d_1[0] = i;
sl@0
    77
}
sl@0
    78
OIL_DEFINE_IMPL (utf8_validate_fast, utf8_validate);
sl@0
    79
#endif
sl@0
    80
sl@0
    81
static void
sl@0
    82
utf8_validate_fast2 (int32_t *d_1, uint8_t *s, int n)
sl@0
    83
{
sl@0
    84
  int i;
sl@0
    85
  uint8_t x;
sl@0
    86
sl@0
    87
  i=0;
sl@0
    88
  while (i<n) {
sl@0
    89
    x = s[i];
sl@0
    90
    if (!(x & 0x80)) {
sl@0
    91
      i++;
sl@0
    92
      continue;
sl@0
    93
    }
sl@0
    94
    x <<= 1;
sl@0
    95
    if (!(x & 0x80)) {
sl@0
    96
      goto error;
sl@0
    97
    }
sl@0
    98
    x <<= 1;
sl@0
    99
    if (!(x & 0x80)) {
sl@0
   100
      if (i + 1 >= n) goto error;
sl@0
   101
      i++;
sl@0
   102
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   103
      i++;
sl@0
   104
      continue;
sl@0
   105
    }
sl@0
   106
    x <<= 1;
sl@0
   107
    if (!(x & 0x80)) {
sl@0
   108
      if (i + 2 >= n) goto error;
sl@0
   109
      i++;
sl@0
   110
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   111
      i++;
sl@0
   112
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   113
      i++;
sl@0
   114
      continue;
sl@0
   115
    }
sl@0
   116
    x <<= 1;
sl@0
   117
    if (!(x & 0x80)) {
sl@0
   118
      if (i + 3 >= n) goto error;
sl@0
   119
      i++;
sl@0
   120
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   121
      i++;
sl@0
   122
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   123
      i++;
sl@0
   124
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   125
      i++;
sl@0
   126
      continue;
sl@0
   127
    }
sl@0
   128
    goto error;
sl@0
   129
  }
sl@0
   130
sl@0
   131
error:
sl@0
   132
  d_1[0] = i;
sl@0
   133
}
sl@0
   134
OIL_DEFINE_IMPL (utf8_validate_fast2, utf8_validate);
sl@0
   135
sl@0
   136
#ifdef HAVE_UNALIGNED_ACCESS
sl@0
   137
static void
sl@0
   138
utf8_validate_fast3 (int32_t *d_1, uint8_t *s, int n)
sl@0
   139
{
sl@0
   140
  int i;
sl@0
   141
  uint8_t x;
sl@0
   142
sl@0
   143
  i=0;
sl@0
   144
  while (i<n) {
sl@0
   145
    if (i < n-3 && (*(uint32_t *)(s+i) & 0x80808080) == 0) {
sl@0
   146
      i+=4;
sl@0
   147
      continue;
sl@0
   148
    }
sl@0
   149
    x = s[i];
sl@0
   150
    if (!(x & 0x80)) {
sl@0
   151
      i++;
sl@0
   152
      continue;
sl@0
   153
    }
sl@0
   154
    if (!(x & 0x40)) {
sl@0
   155
      goto error;
sl@0
   156
    }
sl@0
   157
    if (!(x & 0x20)) {
sl@0
   158
      if (i + 1 >= n) goto error;
sl@0
   159
      i++;
sl@0
   160
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   161
      i++;
sl@0
   162
      continue;
sl@0
   163
    }
sl@0
   164
    if (!(x & 0x10)) {
sl@0
   165
      if (i + 2 >= n) goto error;
sl@0
   166
      i++;
sl@0
   167
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   168
      i++;
sl@0
   169
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   170
      i++;
sl@0
   171
      continue;
sl@0
   172
    }
sl@0
   173
    if (!(x & 0x08)) {
sl@0
   174
      if (i + 3 >= n) goto error;
sl@0
   175
      i++;
sl@0
   176
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   177
      i++;
sl@0
   178
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   179
      i++;
sl@0
   180
      if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   181
      i++;
sl@0
   182
      continue;
sl@0
   183
    }
sl@0
   184
    goto error;
sl@0
   185
  }
sl@0
   186
sl@0
   187
error:
sl@0
   188
  d_1[0] = i;
sl@0
   189
}
sl@0
   190
OIL_DEFINE_IMPL (utf8_validate_fast3, utf8_validate);
sl@0
   191
#endif
sl@0
   192
sl@0
   193
static uint8_t utf8_table[256] = {
sl@0
   194
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   195
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   196
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   197
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   198
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   199
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   200
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   201
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
sl@0
   202
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
sl@0
   203
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
sl@0
   204
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
sl@0
   205
  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
sl@0
   206
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
sl@0
   207
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
sl@0
   208
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
sl@0
   209
  3, 3, 3, 3, 3, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8
sl@0
   210
};
sl@0
   211
  
sl@0
   212
static void
sl@0
   213
utf8_validate_lookup (int32_t *d_1, uint8_t *s, int n)
sl@0
   214
{
sl@0
   215
  int i;
sl@0
   216
  uint8_t x;
sl@0
   217
sl@0
   218
  i=0;
sl@0
   219
  while (i<n) {
sl@0
   220
    x = utf8_table[s[i]];
sl@0
   221
    if (x > 0) {
sl@0
   222
      if (x == 8 || i + x >= n) goto error;
sl@0
   223
      while (x>0) {
sl@0
   224
        i++;
sl@0
   225
        if ((s[i] & 0xc0) != 0x80) goto error;
sl@0
   226
        x--;
sl@0
   227
      }
sl@0
   228
    }
sl@0
   229
    i++;
sl@0
   230
  }
sl@0
   231
sl@0
   232
error:
sl@0
   233
  d_1[0] = i;
sl@0
   234
}
sl@0
   235
OIL_DEFINE_IMPL (utf8_validate_lookup, utf8_validate);
sl@0
   236
sl@0
   237
#if 0
sl@0
   238
static void
sl@0
   239
utf8_validate_asm1 (int32_t *d_1, uint8_t *s, int n)
sl@0
   240
{
sl@0
   241
  uint8_t *tmp = s;
sl@0
   242
sl@0
   243
  asm (
sl@0
   244
      "1:\n"
sl@0
   245
      "  movb (%%eax), %%bl\n"
sl@0
   246
      "  testb %%bl, %%bl\n"
sl@0
   247
      //"  jns 3f\n"
sl@0
   248
      "  js 2f\n"
sl@0
   249
      "3:\n"
sl@0
   250
      "  addl $1, %%eax\n"
sl@0
   251
      "  subl $1, %%ecx\n"
sl@0
   252
      "  jne 1b\n"
sl@0
   253
      "2:\n"
sl@0
   254
    : "+a" (tmp), "+c" (n)
sl@0
   255
    :
sl@0
   256
    : "ebx" );
sl@0
   257
sl@0
   258
  d_1[0] = tmp - s;
sl@0
   259
}
sl@0
   260
OIL_DEFINE_IMPL (utf8_validate_asm1, utf8_validate);
sl@0
   261
sl@0
   262
static void
sl@0
   263
utf8_validate_asm2 (int32_t *d_1, uint8_t *s, int n)
sl@0
   264
{
sl@0
   265
  uint8_t *tmp = s;
sl@0
   266
sl@0
   267
  asm (
sl@0
   268
      "1:\n"
sl@0
   269
      "  testl $0x80808080, (%%eax)\n"
sl@0
   270
      "  jne 2f\n"
sl@0
   271
      "  testl $0x80808080, 4(%%eax)\n"
sl@0
   272
      "  jne 2f\n"
sl@0
   273
      "  testl $0x80808080, 8(%%eax)\n"
sl@0
   274
      "  jne 2f\n"
sl@0
   275
      "  testl $0x80808080, 12(%%eax)\n"
sl@0
   276
      "  jne 2f\n"
sl@0
   277
      "  addl $16, %%eax\n"
sl@0
   278
      "  subl $16, %%ecx\n"
sl@0
   279
      "  jge 1b\n"
sl@0
   280
      "  jl 4f\n"
sl@0
   281
      "2:\n"
sl@0
   282
      "  movb (%%eax), %%bl\n"
sl@0
   283
      "  testb %%bl, %%bl\n"
sl@0
   284
      "  js 4f\n"
sl@0
   285
      "3:\n"
sl@0
   286
      "  addl $1, %%eax\n"
sl@0
   287
      "  subl $1, %%ecx\n"
sl@0
   288
      "  jne 1b\n"
sl@0
   289
      "4:\n"
sl@0
   290
    : "+a" (tmp), "+c" (n)
sl@0
   291
    :
sl@0
   292
    : "ebx" );
sl@0
   293
sl@0
   294
  d_1[0] = tmp - s;
sl@0
   295
}
sl@0
   296
OIL_DEFINE_IMPL (utf8_validate_asm2, utf8_validate);
sl@0
   297
#endif
sl@0
   298
sl@0
   299
sl@0
   300
sl@0
   301
#ifdef HAVE_UNALIGNED_ACCESS
sl@0
   302
#ifdef	__SYMBIAN32__
sl@0
   303
 
sl@0
   304
OilFunctionImpl* __oil_function_impl_utf8_validate_fast() {
sl@0
   305
		return &_oil_function_impl_utf8_validate_fast;
sl@0
   306
}
sl@0
   307
#endif
sl@0
   308
#endif
sl@0
   309
sl@0
   310
#ifdef	__SYMBIAN32__
sl@0
   311
 
sl@0
   312
OilFunctionImpl* __oil_function_impl_utf8_validate_fast2() {
sl@0
   313
		return &_oil_function_impl_utf8_validate_fast2;
sl@0
   314
}
sl@0
   315
#endif
sl@0
   316
sl@0
   317
#ifdef HAVE_UNALIGNED_ACCESS
sl@0
   318
#ifdef	__SYMBIAN32__
sl@0
   319
 
sl@0
   320
OilFunctionImpl* __oil_function_impl_utf8_validate_fast3() {
sl@0
   321
		return &_oil_function_impl_utf8_validate_fast3;
sl@0
   322
}
sl@0
   323
#endif
sl@0
   324
#endif
sl@0
   325
sl@0
   326
#ifdef	__SYMBIAN32__
sl@0
   327
 
sl@0
   328
OilFunctionImpl* __oil_function_impl_utf8_validate_lookup() {
sl@0
   329
		return &_oil_function_impl_utf8_validate_lookup;
sl@0
   330
}
sl@0
   331
#endif
sl@0
   332
sl@0
   333
#ifdef	__SYMBIAN32__
sl@0
   334
 
sl@0
   335
OilFunctionImpl* __oil_function_impl_utf8_validate_asm1() {
sl@0
   336
		return &_oil_function_impl_utf8_validate_asm1;
sl@0
   337
}
sl@0
   338
#endif
sl@0
   339
sl@0
   340
#ifdef	__SYMBIAN32__
sl@0
   341
 
sl@0
   342
OilFunctionImpl* __oil_function_impl_utf8_validate_asm2() {
sl@0
   343
		return &_oil_function_impl_utf8_validate_asm2;
sl@0
   344
}
sl@0
   345
#endif
sl@0
   346