P99
p99_choice.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 authors and copyright holders for this work are as follows: */
5 /* (C) copyright 2010-2013 Jens Gustedt, INRIA, France */
6 /* (C) copyright 2012 William Morris */
7 /* */
8 /* This file is free software; it is part of the P99 project. */
9 /* */
10 /* Licensed under the Apache License, Version 2.0 (the "License"); */
11 /* you may not use this file except in compliance with the License. */
12 /* You may obtain a copy of the License at */
13 /* */
14 /* http://www.apache.org/licenses/LICENSE-2.0 */
15 /* */
16 /* Unless required by applicable law or agreed to in writing, software */
17 /* distributed under the License is distributed on an "AS IS" BASIS, */
18 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
19 /* See the License for the specific language governing permissions and */
20 /* limitations under the License. */
21 /* */
22 #ifndef P99_CHOICE_H_
23 # define P99_CHOICE_H_
24 
30 #include "p99_id.h"
31 #include "p99_for.h"
32 
33 #define P00_CASE_RETURN(NAME, X, I) case I: return X
34 
35 #define P99_CASE_RETURN(...) P99_FOR(,P99_NARG(__VA_ARGS__), P00_SEP, P00_CASE_RETURN, __VA_ARGS__)
36 
37 #define P99_CHOICE_FUNCTION(TYPE, NAME, DEFAULT, ...) \
38  \
39  \
40  \
41  \
42 P99_CONST_FUNCTION \
43 p99_inline \
44 TYPE NAME(size_t p00_x) { \
45  switch (p00_x) { \
46  P99_CASE_RETURN(__VA_ARGS__); \
47  default: return DEFAULT; \
48  } \
49 } \
50 P99_MACRO_END(_choice_function_, NAME)
51 
54  2, 3, 5, 7, 11, 13, 17, 19, 23, 31, 37, 41, 43, 47,
55  53, 57, 59, 61, 67, 71);
56 
57 
58 #define P00_UNIQUE_BIT_MULT_3 0x1DU
59 #define P00_UNIQUE_BIT_MULT_4 0xD2FU
60 #define P00_UNIQUE_BIT_MULT_5 0x077CB531U
61 #define P00_UNIQUE_BIT_MULT_6 0X022FDD63CC95386DU
62 
63 #define P00_UNIQUE_BIT_MULT(BITS) P99_PASTE2(P00_UNIQUE_BIT_MULT_, BITS)
64 
65 #define P00_UNIQUE_BIT_HASH(X, BITS, WIDTH) \
66  ((((X) * P00_UNIQUE_BIT_MULT(BITS)) >> (WIDTH - BITS)) \
67  & P99_PASTE3(UINT, WIDTH, _MAX))
68 
69 #define P00_UNIQUE_BIT_(BIT, BITS, WIDTH) \
70 P00_UNIQUE_BIT_HASH(P99_PASTE3(UINT, WIDTH, _C)(1) << BIT, BITS, WIDTH)
71 
72 
73 #define P00_UNIQUE_BIT_RETURN(NAME, X, I) case P00_UNIQUE_BIT_(I, X, NAME): return I
74 
75 
76 #ifdef P00_DOXYGEN
77 /* doxygen can't handle the P99_FOR */
78 #define P00_UNIQUE_BIT_FUNCTION(TYPE, NAME, DEFAULT, BITS, WIDTH) \
79 p99_inline \
80 TYPE P99_PASTE2(NAME, BITS)(size_t p00_x)
81 #else
82 #define P00_UNIQUE_BIT_FUNCTION(TYPE, NAME, DEFAULT, BITS, WIDTH) \
83 P99_CONST_FUNCTION \
84 p99_inline \
85 TYPE P99_PASTE2(NAME, BITS)(P99_PASTE3(uint, WIDTH, _t) p00_x) { \
86  switch (p00_x) { \
87  P99_FOR(WIDTH, WIDTH, P00_SEP, P00_UNIQUE_BIT_RETURN, P99_DUPL(WIDTH, BITS)); \
88  default: return DEFAULT; \
89  } \
90 } \
91 P99_MACRO_END(_unique_bit)
92 #endif
93 
94 P00_UNIQUE_BIT_FUNCTION(unsigned, p00_unique_bit_hash_, -1, 3, 8);
95 P00_UNIQUE_BIT_FUNCTION(unsigned, p00_unique_bit_hash_, -1, 4, 16);
96 P00_UNIQUE_BIT_FUNCTION(unsigned, p00_unique_bit_hash_, -1, 5, 32);
97 P00_UNIQUE_BIT_FUNCTION(unsigned, p00_unique_bit_hash_, -1, 6, 64);
98 
99 
105 #define P00_UNIQUE_BIT(BITS, WIDTH) \
106  \
107  \
108  \
109 P99_CONST_FUNCTION \
110 p99_inline \
111  unsigned P99_PASTE2(p99_unique_bit_, WIDTH)(P99_PASTE3(uint, WIDTH, _t) p00_x) { \
112  /* the index now only has BITS significant bits, so the default case of \
113  P99_PASTE2(p00_unique_bit_hash_, BITS) will never trigger.*/ \
114  return \
115  P99_PASTE2(p00_unique_bit_hash_, BITS) \
116  (P00_UNIQUE_BIT_HASH(p00_x, BITS, WIDTH)); \
117 } \
118  \
119  \
120  \
121 P99_CONST_FUNCTION \
122 p99_inline \
123 unsigned P99_PASTE2(p99_unique_bit_checked_, WIDTH)(P99_PASTE3(uint, WIDTH, _t) p00_x) { \
124  unsigned p00_ret = P99_PASTE2(p99_unique_bit_, WIDTH)(p00_x); \
125  return ((P99_PASTE3(UINT, WIDTH, _C)(1) << p00_ret) == p00_x) ? p00_ret : -1; \
126 } \
127 P99_MACRO_END(_unique_bit)
128 
129 P00_UNIQUE_BIT(3, 8);
130 P00_UNIQUE_BIT(4, 16);
131 P00_UNIQUE_BIT(5, 32);
132 P00_UNIQUE_BIT(6, 64);
133 
134 
135 #endif /* !P99_CHOICE_H_ */
p99_small_primes
P99_CONST_FUNCTION uint8_t p99_small_primes(size_t p00_x)
Return a small prime number or 0 if x is too big.
Definition: p99_choice.h:55
p99_for.h
A preprocessor for loop implementation and some derived list handling macros.
p99_id.h
Implement a simple FILEID that changes each time this file is included somewhere.
P99_CHOICE_FUNCTION
#define P99_CHOICE_FUNCTION(TYPE, NAME, DEFAULT,...)
Definition: p99_choice.h:37