P99
p99_bitset.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the author and copyright holder for this work is */
5 /* (C) copyright 2012-2013 Jens Gustedt, INRIA, France */
6 /* */
7 /* This file is free software; it is part of the P99 project. */
8 /* */
9 /* Licensed under the Apache License, Version 2.0 (the "License"); */
10 /* you may not use this file except in compliance with the License. */
11 /* You may obtain a copy of the License at */
12 /* */
13 /* http://www.apache.org/licenses/LICENSE-2.0 */
14 /* */
15 /* Unless required by applicable law or agreed to in writing, software */
16 /* distributed under the License is distributed on an "AS IS" BASIS, */
17 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
18 /* See the License for the specific language governing permissions and */
19 /* limitations under the License. */
20 /* */
21 #ifndef P99_BITSET_H_
22 #define P99_BITSET_H_
23 
24 #include "p99_tss.h"
25 #include "p99_c99.h"
26 #include "p99_errno.h"
27 #include "p99_int.h"
28 
29 
30 #if defined(UINT128_MAX)
31 typedef uint128_t p99_bitset;
32 # define P00_ISIN p00_isin2
33 #elif defined(p99x_uint128)
34 typedef p99x_uint128 p99_bitset;
35 # define P00_ISIN p00_isin2
36 #else
37 //#error "didn't find 128 bit type"
38 typedef uint64_t p99_bitset;
39 # define P00_ISIN p00_isin4
40 #endif
41 
42 # define P99_BITSET_C(X) ((p99_bitset)+(X))
43 
44 P99_CONSTANT(int, p99_bitset_bit, sizeof(p99_bitset)*CHAR_BIT);
45 P99_CONSTANT(int, p00_bitset_bit1, p99_bitset_bit);
46 P99_CONSTANT(int, p00_bitset_bit2, p99_bitset_bit<<1);
47 P99_CONSTANT(int, p00_bitset_bit3, p00_bitset_bit1|p00_bitset_bit2);
48 
51 uint8_t p00_bit_low(uint8_t p00_c) {
52  return p00_c % p99_bitset_bit;
53 }
54 
57 uint8_t p00_bit_high(uint8_t p00_c) {
58  return p00_c / p99_bitset_bit;
59 }
60 
63 p99_bitset p00_set_bit(uint8_t p00_c) {
64  return P99_BITSET_C(1) << p00_bit_low(p00_c);
65 }
66 
67 #define P00_BITSET_CASE(B, _1, I) case I: if (p00_bit_high(p00_s2[I]) == B) p00_ret |= p00_set_bit(p00_s2[I])
68 #define P00_BITSET_CASE128(B, _1, I) case I+128: if (p00_bit_high(p00_s2[I+128]) == B) p00_ret |= p00_set_bit(p00_s2[I+128])
69 
70 
71 #define P00_BITS_DECLARE(B) \
72 P99_CONST_FUNCTION \
73 p99_inline \
74 p99_bitset P99_PASTE2(p00_bits, B)(rsize_t p00_s2l, uint8_t const p00_s2[const restrict p00_s2l]) { \
75  p99_bitset p00_ret = 0; \
76  switch (p00_s2l - 2) { \
77  P99_FOR(B, 128, P00_PES, P00_BITSET_CASE128, P00_ALLZERO); \
78  P99_FOR(B, 128, P00_PES, P00_BITSET_CASE, P00_ALLZERO); \
79  } \
80  return p00_ret; \
81 } \
82 P99_MACRO_END(P00_BITS_DECLARE, B)
83 
84 P00_BITS_DECLARE(0);
85 P00_BITS_DECLARE(1);
86 P00_BITS_DECLARE(2);
87 P00_BITS_DECLARE(3);
88 
91 p99_bitset p00_isin1(uint8_t p00_c,
92  rsize_t p00_s2l, uint8_t const p00_s2[const restrict p00_s2l]) {
93  register p99_bitset const p00_b0 = p00_bits0(p00_s2l, p00_s2);
94  register p99_bitset p00_ret = p00_set_bit(p00_c);
95  p00_ret &= p00_b0;
96  return p00_ret;
97 }
98 
101 p99_bitset p00_isin2(uint8_t p00_c,
102  rsize_t p00_s2l, uint8_t const p00_s2[const restrict p00_s2l]) {
103  register p99_bitset const p00_b0 = p00_bits0(p00_s2l, p00_s2);
104  register p99_bitset const p00_b1 = p00_bits1(p00_s2l, p00_s2);
105 
106  register p99_bitset p00_ret = p00_set_bit(p00_c);
107  p00_ret &= (p00_c < p99_bitset_bit) ? p00_b0 : p00_b1;
108  return p00_ret;
109 }
110 
113 p99_bitset p00_isin4(uint8_t p00_c,
114  rsize_t p00_s2l, uint8_t const p00_s2[const restrict p00_s2l]) {
115  register p99_bitset const p00_b0 = p00_bits0(p00_s2l, p00_s2);
116  register p99_bitset const p00_b1 = p00_bits1(p00_s2l, p00_s2);
117  register p99_bitset const p00_b2 = p00_bits2(p00_s2l, p00_s2);
118  register p99_bitset const p00_b3 = p00_bits3(p00_s2l, p00_s2);
119 
120  register p99_bitset p00_ret = p00_set_bit(p00_c);
121  switch (p00_c & p00_bitset_bit3) {
122  default: p00_ret &= p00_b0; break;
123  case p00_bitset_bit1: p00_ret &= p00_b1; break;
124  case p00_bitset_bit2: p00_ret &= p00_b2; break;
125  case p00_bitset_bit3: p00_ret &= p00_b3; break;
126  }
127  return p00_ret;
128 }
129 
130 #define P00_BLANK " \t"
131 #define P00_SPACE " \f\n\r\t\v"
132 #define P00_LOWER "abcdefghijklmnopqrstuvwxyz"
133 #define P00_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
134 #define P00_ALPHA P00_UPPER P00_LOWER
135 #define P00_DIGIT "0123456789"
136 #define P00_ALNUM P00_ALPHA P00_DIGIT
137 #define P00_XDIGIT "0123456789abcdefABCDEF"
138 
141 p99_bitset p99_is_blank(uint8_t p00_c) {
142  return P00_ISIN(p00_c, sizeof P00_BLANK, (uint8_t const*)P00_BLANK);
143 }
144 
147 p99_bitset p99_is_space(uint8_t p00_c) {
148  return P00_ISIN(p00_c, sizeof P00_SPACE, (uint8_t const*)P00_SPACE);
149 }
150 
153 p99_bitset p99_is_lower(uint8_t p00_c) {
154  return P00_ISIN(p00_c, sizeof P00_LOWER, (uint8_t const*)P00_LOWER);
155 }
156 
159 p99_bitset p99_is_upper(uint8_t p00_c) {
160  return P00_ISIN(p00_c, sizeof P00_UPPER, (uint8_t const*)P00_UPPER);
161 }
162 
165 p99_bitset p99_is_alpha(uint8_t p00_c) {
166  return P00_ISIN(p00_c, sizeof P00_ALPHA, (uint8_t const*)P00_ALPHA);
167 }
168 
171 p99_bitset p99_is_digit(uint8_t p00_c) {
172  return P00_ISIN(p00_c, sizeof P00_DIGIT, (uint8_t const*)P00_DIGIT);
173 }
174 
177 p99_bitset p99_is_xdigit(uint8_t p00_c) {
178  return P00_ISIN(p00_c, sizeof P00_XDIGIT, (uint8_t const*)P00_XDIGIT);
179 }
180 
183 p99_bitset p99_is_alnum(uint8_t p00_c) {
184  return P00_ISIN(p00_c, sizeof P00_ALNUM, (uint8_t const*)P00_ALNUM);
185 }
186 
187 #endif
p99_is_xdigit
P99_CONST_FUNCTION p99_bitset p99_is_xdigit(uint8_t p00_c)
Definition: p99_bitset.h:177
p99_is_lower
P99_CONST_FUNCTION p99_bitset p99_is_lower(uint8_t p00_c)
Definition: p99_bitset.h:153
rsize_t
size_t rsize_t
Definition: p99_libc.h:164
p99_c99.h
C99 specific include files that are required by the standard.
p99_tss.h
p99_errno.h
P99_CONSTANT
#define P99_CONSTANT(T, NAME, INIT)
define a compile time constant NAME of type T with value INIT
Definition: p99_enum.h:258
p99_is_blank
P99_CONST_FUNCTION p99_bitset p99_is_blank(uint8_t p00_c)
Definition: p99_bitset.h:141
P99_BITSET_C
#define P99_BITSET_C(X)
Definition: p99_bitset.h:42
p99_bitset_bit
register const int p99_bitset_bit
Definition: p99_bitset.h:44
p99_is_alpha
P99_CONST_FUNCTION p99_bitset p99_is_alpha(uint8_t p00_c)
Definition: p99_bitset.h:165
p99_is_digit
P99_CONST_FUNCTION p99_bitset p99_is_digit(uint8_t p00_c)
Definition: p99_bitset.h:171
p99_bitset
uint64_t p99_bitset
Definition: p99_bitset.h:38
p99_inline
#define p99_inline
Try to force a function always to be inlined.
Definition: p99_compiler.h:496
p99_is_space
P99_CONST_FUNCTION p99_bitset p99_is_space(uint8_t p00_c)
Definition: p99_bitset.h:147
P99_CONST_FUNCTION
#define P99_CONST_FUNCTION
On architectures that support this, assert that a function is "const", i.e only depends on parameters...
Definition: p99_compiler.h:622
p99_is_alnum
P99_CONST_FUNCTION p99_bitset p99_is_alnum(uint8_t p00_c)
Definition: p99_bitset.h:183
p99_is_upper
P99_CONST_FUNCTION p99_bitset p99_is_upper(uint8_t p00_c)
Definition: p99_bitset.h:159
p99_int.h
Macros handling integer types and initialization.
p99x_uint128
extendedInt p99x_uint128
An unsigned integer type of width 128 that exceeds the C99 specifications.
Definition: p99_int.h:149