Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef TextCodecASCIIFastPath_h
     28 #define TextCodecASCIIFastPath_h
     29 
     30 #include <stdint.h>
     31 
     32 namespace WebCore {
     33 
     34 // Assuming that a pointer is the size of a "machine word", then
     35 // uintptr_t is an integer type that is also a machine word.
     36 typedef uintptr_t MachineWord;
     37 
     38 // This constant has type uintptr_t since we will use it to align
     39 // pointers. Not because MachineWord is uintptr_t.
     40 const uintptr_t machineWordAlignmentMask = sizeof(MachineWord) - 1;
     41 
     42 template<size_t size> struct NonASCIIMask;
     43 template<> struct NonASCIIMask<4> {
     44     static unsigned value() { return 0x80808080U; }
     45 };
     46 template<> struct NonASCIIMask<8> {
     47     static unsigned long long value() { return 0x8080808080808080ULL; }
     48 };
     49 
     50 template<size_t size> struct UCharByteFiller;
     51 template<> struct UCharByteFiller<4> {
     52     static void copy(UChar* destination, const uint8_t* source)
     53     {
     54         destination[0] = source[0];
     55         destination[1] = source[1];
     56         destination[2] = source[2];
     57         destination[3] = source[3];
     58     }
     59 };
     60 template<> struct UCharByteFiller<8> {
     61     static void copy(UChar* destination, const uint8_t* source)
     62     {
     63         destination[0] = source[0];
     64         destination[1] = source[1];
     65         destination[2] = source[2];
     66         destination[3] = source[3];
     67         destination[4] = source[4];
     68         destination[5] = source[5];
     69         destination[6] = source[6];
     70         destination[7] = source[7];
     71     }
     72 };
     73 
     74 inline bool isAllASCII(MachineWord word)
     75 {
     76     return !(word & NonASCIIMask<sizeof(MachineWord)>::value());
     77 }
     78 
     79 inline void copyASCIIMachineWord(UChar* destination, const uint8_t* source)
     80 {
     81     UCharByteFiller<sizeof(MachineWord)>::copy(destination, source);
     82 }
     83 
     84 inline bool isAlignedToMachineWord(const void* pointer)
     85 {
     86     return !(reinterpret_cast<uintptr_t>(pointer) & machineWordAlignmentMask);
     87 }
     88 
     89 template<typename T> inline T* alignToMachineWord(T* pointer)
     90 {
     91     return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(pointer) & ~machineWordAlignmentMask);
     92 }
     93 
     94 } // namespace WebCore
     95 
     96 #endif // TextCodecASCIIFastPath_h
     97