Ignore:
Timestamp:
Jan 13, 2008, 8:50:53 PM (11 years ago)
Author:
cameron
Message:

Initiating ASCII/EBCDIC selection using templates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/multiliteral.h

    r11 r17  
    1919#include <assert.h>
    2020#include <stdint.h>
     21#include "charsets/ASCII_EBCDIC.h"
    2122
    2223/*
     
    252616, 32 or 64 bit integer value.   These functions are intended to be called
    2627with constant arguments and hence to be applied at compile time.
     28This is achieved using template metaprogramming.
    2729*/
    2830
    29 static inline uint16_t c2int16(unsigned char c1, unsigned char c2) {
     31template <CharBase C, unsigned char c1, unsigned char c2>
     32struct c2int16 {
     33  static uint16_t const value =
    3034#if BYTE_ORDER == BIG_ENDIAN
    31   return (((uint16_t) c1) << 8) + (uint16_t) c2;
     35    (((uint16_t) Ord<C,c1>::value) << 8) + (uint16_t) Ord<C,c2>::value;
    3236#endif
    3337#if BYTE_ORDER == LITTLE_ENDIAN
    34   return (uint16_t) c1 + (((uint16_t) c2) << 8);
     38    (uint16_t) Ord<C,c1>::value + (((uint16_t) Ord<C,c2>::value) << 8);
    3539#endif
    36 }
     40};
    3741
    38 static inline uint32_t c4int32(unsigned char c1, unsigned char c2,
    39                         unsigned char c3, unsigned char c4) {
     42template <CharBase C, unsigned char c1, unsigned char c2,
     43                      unsigned char c3, unsigned char c4>
     44struct c4int32 {
     45  static uint32_t const value =
    4046#if BYTE_ORDER == BIG_ENDIAN
    41   return (((uint32_t) c2int16(c1, c2)) << 16) + (uint32_t) c2int16(c3, c4);
     47    (((uint16_t) c2int16<C,c1,c2>::value) << 16) + (uint16_t) c2int16<C,c3,c4>::value;
    4248#endif
    4349#if BYTE_ORDER == LITTLE_ENDIAN
    44   return ((uint32_t) c2int16(c1, c2)) + (((uint32_t) c2int16(c3, c4)) << 16);
     50    (uint16_t) c2int16<C,c1,c2>::value + (((uint16_t) c2int16<C,c3,c4>::value) << 16);
    4551#endif
    46 }
     52};
    4753
    48 static inline uint64_t c8int64(unsigned char c1, unsigned char c2,
    49                         unsigned char c3, unsigned char c4,
    50                         unsigned char c5, unsigned char c6,
    51                         unsigned char c7, unsigned char c8) {
     54template <CharBase C, unsigned char c1, unsigned char c2,
     55                      unsigned char c3, unsigned char c4,
     56                      unsigned char c5, unsigned char c6,
     57                      unsigned char c7, unsigned char c8>
     58struct c8int64 {
     59  static uint64_t const value =
    5260#if BYTE_ORDER == BIG_ENDIAN
    53   return ((uint64_t) c4int32(c1, c2, c3, c4)) << 32 +
    54           (uint64_t) c4int32(c5, c6, c7, c8);
     61    ((uint64_t) c4int32<C, c1, c2, c3, c4>::value) << 32 +
     62     (uint64_t) c4int32<C, c5, c6, c7, c8>::value;
    5563#endif
    5664#if BYTE_ORDER == LITTLE_ENDIAN
    57   return ((uint64_t) c4int32(c1, c2, c3, c4)) +
    58          (((uint64_t) c4int32(c5, c6, c7, c8)) << 32);
     65    ((uint64_t) c4int32<C, c1, c2, c3, c4>::value) +
     66    ((uint64_t) c4int32<C, c5, c6, c7, c8>::value) << 32;
    5967#endif
    60 }
     68};
     69
    6170
    6271/*  Specialized helpers for 3, 5, 6, and 7 character combinations. */
    6372
    64 static inline uint32_t c3int32(unsigned char c1, unsigned char c2,
    65                                unsigned char c3) {
    66   return c4int32(c1, c2, c3, 0);
    67 }
     73template <CharBase C, unsigned char c1, unsigned char c2,
     74                      unsigned char c3>
     75struct c3int32 {
     76  static uint32_t const value = c4int32<C, c1, c2, c3, 0>::value;
     77};
    6878
    69 static inline uint64_t c5int64(unsigned char c1, unsigned char c2,
    70                                unsigned char c3, unsigned char c4,
    71                                unsigned char c5) {
    72   return c8int64(c1, c2, c3, c4, c5, 0, 0, 0);
    73 }
     79template <CharBase C, unsigned char c1, unsigned char c2,
     80                      unsigned char c3, unsigned char c4,
     81                      unsigned char c5>
     82struct c5int64 {
     83  static uint64_t const value = c8int64<C, c1, c2, c3, c4, c5, 0, 0, 0>::value;
     84};
    7485
    75 static inline uint64_t c6int64(unsigned char c1, unsigned char c2,
    76                                unsigned char c3, unsigned char c4,
    77                                unsigned char c5, unsigned char c6) {
    78   return c8int64(c1, c2, c3, c4, c5, c6, 0, 0);
    79 }
     86template <CharBase C, unsigned char c1, unsigned char c2,
     87                      unsigned char c3, unsigned char c4,
     88                      unsigned char c5, unsigned char c6>
     89struct c6int64 {
     90  static uint64_t const value = c8int64<C, c1, c2, c3, c4, c5, c6, 0, 0>::value;
     91};
    8092
    81 static inline uint64_t c7int64(unsigned char c1, unsigned char c2,
    82                                unsigned char c3, unsigned char c4,
    83                                unsigned char c5, unsigned char c6,
    84                                unsigned char c7) {
    85   return c8int64(c1, c2, c3, c4, c5, c6, c7, 0);
    86 }
     93template <CharBase C, unsigned char c1, unsigned char c2,
     94                      unsigned char c3, unsigned char c4,
     95                      unsigned char c5, unsigned char c6,
     96                      unsigned char c7>
     97struct c7int64 {
     98  static uint64_t const value = c8int64<C, c1, c2, c3, c4, c5, c6, c7, 0>::value;
     99};
     100
    87101
    88102/*
     
    104118
    105119static inline uint32_t s3int32(unsigned char s[]) {
    106   return s4int32(s) & c3int32(0xFF, 0xFF, 0xFF);
     120  return s4int32(s) & c3int32<Native, 0xFF, 0xFF, 0xFF>::value;
    107121}
    108122
    109123static inline uint64_t s5int64(unsigned char s[]) {
    110   return s8int64(s) & c5int64(0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
     124  return s8int64(s) & c5int64<Native, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF>::value;
    111125}
    112126
    113127static inline uint64_t s6int64(unsigned char s[]) {
    114   return s8int64(s) & c6int64(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
     128  return s8int64(s) & c6int64<Native, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF>::value;
    115129}
    116130
    117131static inline uint64_t s7int64(unsigned char s[]) {
    118   return s8int64(s) & c7int64(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
     132  return s8int64(s) & c7int64<Native, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF>::value;
    119133}
    120134
Note: See TracChangeset for help on using the changeset viewer.