rc_word.h

Go to the documentation of this file.
00001 /*  Copyright (C) 2005-2010, Axis Communications AB, LUND, SWEDEN
00002  *
00003  *  This file is part of RAPP.
00004  *
00005  *  RAPP is free software: you can redistribute it and/or modify
00006  *  it under the terms of the GNU Lesser General Public License as published
00007  *  by the Free Software Foundation, either version 3 of the License, or
00008  *  (at your option) any later version.
00009  *  You can use the comments under either the terms of the GNU Lesser General
00010  *  Public License version 3 as published by the Free Software Foundation,
00011  *  either version 3 of the License or (at your option) any later version, or
00012  *  the GNU Free Documentation License version 1.3 or any later version
00013  *  published by the Free Software Foundation; with no Invariant Sections, no
00014  *  Front-Cover Texts, and no Back-Cover Texts.
00015  *  A copy of the license is included in the documentation section entitled
00016  *  "GNU Free Documentation License".
00017  *
00018  *  RAPP is distributed in the hope that it will be useful,
00019  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00021  *  GNU Lesser General Public License for more details.
00022  *
00023  *  You should have received a copy of the GNU Lesser General Public
00024  *  License and a copy of the GNU Free Documentation License along
00025  *  with RAPP. If not, see <http://www.gnu.org/licenses/>.
00026  */
00027 
00033 #ifndef RC_WORD_H
00034 #define RC_WORD_H
00035 
00036 #include <stdint.h>      /* Fixed-size integers           */
00037 #include "rc_platform.h" /* Platform-specific definitions */
00038 #include "rc_table.h"    /* Lookup tables                 */
00039 
00040 /*
00041  * -------------------------------------------------------------
00042  *  The word type
00043  * -------------------------------------------------------------
00044  */
00045 
00050 #ifndef RC_WORD_SIZE
00051 #define RC_WORD_SIZE RC_NATIVE_SIZE
00052 #endif
00053 
00057 #if   RC_WORD_SIZE == 8
00058 typedef uint64_t rc_word_t;
00059 #elif RC_WORD_SIZE == 4
00060 typedef uint32_t rc_word_t;
00061 #elif RC_WORD_SIZE == 2
00062 typedef uint16_t rc_word_t;
00063 #else
00064 #error "Unsupported word size."
00065 #endif
00066 
00067 
00068 /*
00069  * -------------------------------------------------------------
00070  *  Constants
00071  * -------------------------------------------------------------
00072  */
00073 
00077 #define RC_WORD_ZERO ((rc_word_t)0)
00078 
00082 #define RC_WORD_ONE  ((rc_word_t)~RC_WORD_ZERO)
00083 
00084 
00085 /*
00086  * -------------------------------------------------------------
00087  *  Memory access
00088  * -------------------------------------------------------------
00089  */
00090 
00094 #define RC_WORD_LOAD(ptr) \
00095     (*(const rc_word_t*)(ptr))
00096 
00100 #define RC_WORD_STORE(ptr, word) \
00101     (*(rc_word_t*)(ptr) = (word))
00102 
00103 
00104 /*
00105  * -------------------------------------------------------------
00106  *  Constants
00107  * -------------------------------------------------------------
00108  */
00109 
00113 #if RC_WORD_SIZE == 8
00114 #define RC_WORD_C8(field) \
00115     RC_WORD_HEX_8__(field, UINT64_C)
00116 #define RC_WORD_C16(field) \
00117     RC_WORD_HEX_4__(field, UINT64_C)
00118 #define RC_WORD_C32(field) \
00119     RC_WORD_HEX_2__(field, UINT64_C)
00120 #define RC_WORD_C64(field) \
00121     RC_WORD_HEX_1__(field, UINT64_C)
00122 
00123 #elif RC_WORD_SIZE == 4
00124 #define RC_WORD_C8(field) \
00125     RC_WORD_HEX_4__(field, UINT32_C)
00126 #define RC_WORD_C16(field) \
00127     RC_WORD_HEX_2__(field, UINT32_C)
00128 #define RC_WORD_C32(field) \
00129     RC_WORD_HEX_1__(field, UINT32_C)
00130 
00131 #elif RC_WORD_SIZE == 2
00132 #define RC_WORD_C8(field) \
00133     RC_WORD_HEX_2__(field, UINT16_C)
00134 #define RC_WORD_C16(field) \
00135     RC_WORD_HEX_1__(field, UINT16_C)
00136 #endif
00137 
00138 
00139 /*
00140  * -------------------------------------------------------------
00141  *  Field selection and relocation
00142  * -------------------------------------------------------------
00143  */
00144 
00148 #ifdef  RC_BIG_ENDIAN
00149 #define RC_WORD_BIT(pos) \
00150     ((rc_word_t)1 << (8*RC_WORD_SIZE - (pos) - 1))
00151 #else
00152 #define RC_WORD_BIT(pos) \
00153     ((rc_word_t)1 << (pos))
00154 #endif
00155 
00159 #ifdef  RC_BIG_ENDIAN
00160 #define RC_WORD_SHR(word, bits) \
00161     ((rc_word_t)(word) >> (bits))
00162 #else
00163 #define RC_WORD_SHR(word, bits) \
00164     ((rc_word_t)(word) << (bits))
00165 #endif
00166 
00170 #ifdef  RC_BIG_ENDIAN
00171 #define RC_WORD_SHL(word, bits) \
00172     ((rc_word_t)(word) << (bits))
00173 #else
00174 #define RC_WORD_SHL(word, bits) \
00175     ((rc_word_t)(word) >> (bits))
00176 #endif
00177 
00182 #define RC_WORD_ALIGN(word1, word2, bits) \
00183     (RC_WORD_SHL(word1, bits) |           \
00184      RC_WORD_SHR(word2, 8*RC_WORD_SIZE - (bits)))
00185 
00190 #ifdef  RC_BIG_ENDIAN
00191 #define RC_WORD_INSERT(value, pos, bits) \
00192     ((rc_word_t)(value) << (8*RC_WORD_SIZE - (pos) - (bits)))
00193 #define RC_32_INSERT(value, pos, bits) \
00194     ((uint32_t)(value) << (32 - (pos) - (bits)))
00195 #else
00196 #define RC_WORD_INSERT(value, pos, bits) \
00197     ((rc_word_t)(value) << (pos))
00198 #define RC_32_INSERT(value, pos, bits) \
00199     ((uint32_t)(value) << (pos))
00200 #endif
00201 
00206 #ifdef  RC_BIG_ENDIAN
00207 #define RC_WORD_EXTRACT(word, pos, bits)                        \
00208     (((rc_word_t)(word) >> (8*RC_WORD_SIZE - (pos) - (bits))) & \
00209       RC_WORD_MASK__(bits))
00210 #define RC_32_EXTRACT(word, pos, bits) \
00211     (((uint32_t)(word) >> (32 - (pos) - (bits))) & RC_32_MASK__(bits))
00212 #else
00213 #define RC_WORD_EXTRACT(word, pos, bits) \
00214     (((rc_word_t)(word) >> (pos)) & RC_WORD_MASK__(bits))
00215 #define RC_32_EXTRACT(word, pos, bits) \
00216     (((uint32_t)(word) >> (pos)) & RC_32_MASK__(bits))
00217 #endif
00218 
00222 #if defined __GNUC__ && RC_GCC_VERSION > 3004 && \
00223     RC_WORD_SIZE == RC_NATIVE_SIZE
00224 #if RC_WORD_SIZE == 8
00225 #define RC_WORD_BITCOUNT(cnt, word) \
00226     ((cnt) = __builtin_popcountl(word))
00227 
00228 #else /* RC_WORD_SIZE <= 4 */
00229 #define RC_WORD_BITCOUNT(cnt, word) \
00230     ((cnt) = __builtin_popcount(word))
00231 #endif
00232 
00233 #else /* Fall back on table lookups */
00234 #define RC_WORD_BITCOUNT(cnt, word)                        \
00235 do {                                                       \
00236     rc_word_t w__ = (word);                                \
00237     (cnt)  = rc_table_bitcount[w__ & 0xff], w__ >>= 8;     \
00238     (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8;     \
00239     if (RC_WORD_SIZE >= 4) {                               \
00240         (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8; \
00241         (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8; \
00242     }                                                      \
00243     if (RC_WORD_SIZE == 8) {                               \
00244         (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8; \
00245         (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8; \
00246         (cnt) += rc_table_bitcount[w__ & 0xff], w__ >>= 8; \
00247         (cnt) += rc_table_bitcount[w__ & 0xff];            \
00248     }                                                      \
00249 } while (0)
00250 #endif
00251 
00252 
00253 /*
00254  * -------------------------------------------------------------
00255  *  Internal support macros
00256  * -------------------------------------------------------------
00257  */
00258 
00262 #define RC_WORD_MASK__(bits) \
00263     (RC_WORD_ONE >> (8*RC_WORD_SIZE - (bits)))
00264 
00265 #define RC_32_MASK__(bits) \
00266     (UINT32_MAX >> (32 - (bits)))
00267 
00271 #define RC_WORD_HEX_8__(digs, suff) \
00272     suff(0x ## digs ## digs ## digs ## digs ## digs ## digs ## digs ## digs)
00273 
00277 #define RC_WORD_HEX_4__(digs, suff) \
00278     suff(0x ## digs ## digs ## digs ## digs)
00279 
00283 #define RC_WORD_HEX_2__(digs, suff) \
00284     suff(0x ## digs ## digs)
00285 
00289 #define RC_WORD_HEX_1__(digs, suff) \
00290     suff(0x ## digs)
00291 
00292 
00293 #endif /* RC_WORD_H */

Generated on 1 Jun 2016 for RAPP Compute by  doxygen 1.6.1