Debian Codegraph @

cuneiform_src/Kern/lns32/src/bit_func.h

raw
1 /*
2 Copyright (c) 1993-2008, Cognitive Technologies
3 All rights reserved.
4
5 Разрешается повторное распространение и использование как в виде исходного кода,
6 так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:
7
8 * При повторном распространении исходного кода должны оставаться указанное
9 выше уведомление об авторском праве, этот список условий и последующий
10 отказ от гарантий.
11 * При повторном распространении двоичного кода в документации и/или в
12 других материалах, поставляемых при распространении, должны сохраняться
13 указанная выше информация об авторском праве, этот список условий и
14 последующий отказ от гарантий.
15 * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
16 быть использованы в качестве средства поддержки и/или продвижения
17 продуктов, основанных на этом ПО, без предварительного письменного
18 разрешения.
19
20 ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
21 ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
22 ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
23 ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
24 МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
25 НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
26 ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
27 НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
28 СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
29 ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
30 НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
31 ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.
32
33 Redistribution and use in source and binary forms, with or without modification,
34 are permitted provided that the following conditions are met:
35
36 * Redistributions of source code must retain the above copyright notice,
37 this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright notice,
39 this list of conditions and the following disclaimer in the documentation
40 and/or other materials provided with the distribution.
41 * Neither the name of the Cognitive Technologies nor the names of its
42 contributors may be used to endorse or promote products derived from this
43 software without specific prior written permission.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
46 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
49 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 #ifndef __BIT_FUNC_H
58 # define __BIT_FUNC_H
59
60 #ifndef __LNSLANG_H
61 # include "lnslang.h"
62 #endif
63
64 extern const uchar bit_byte[8];
65 extern const uchar lbit_byte[8];
66 extern const uint16_t bit_word[16];
67 extern const uint16_t lbit_word[16];
68 extern const uchar right_0[256];
69 extern const uchar left_0[256];
70 extern const uchar bit0_cnt[256];
71 extern const uchar bit1_cnt[256];
72 extern const uchar first_bit0_pos[256];
73 extern const uchar first_bit1_pos[256];
74 /**
75 #ifndef BIG_ENDIAN
76 #define hi_byte( some_word ) ( *( ((uchar*)&some_word) +1 ) )
77 #define low_byte( some_word ) ( (uchar)some_word )
78 #else
79 #define hi_byte( some_word ) ( (uchar)some_word )
80 #define low_byte( some_word ) ( *( ((uchar*)&some_word) +1 ) )
81 #endif
82 **/
83 #define hi_byte( some_word ) ( ((uint16_t)some_word)>>8 )
84 #define low_byte( some_word ) ( (uchar)some_word )
85
86 #define left_byte( some_word ) ( low_byte( some_word ) )
87 #define right_byte( some_word ) ( hi_byte( some_word ) )
88
89 #define lr_word( le_byte, ri_byte ) ( (ri_byte << 8) + le_byte )
90 #define hl_word( hi_byte, lo_byte ) ( (hi_byte << 8) + lo_byte )
91
92 #define FIRST_BIT_IN_BYTE( bb ) ( first_bit1_pos[ bb ] ) // 0..7; 8 if none
93
94 #define set_bit_to_1( to_word, bit_num ) ( (to_word) |= bit_word[ bit_num ] )
95 #define set_bit_to_0( to_word, bit_num ) ( (to_word) &= ~bit_word[ bit_num ] )
96 #define is_bit( in_word, bit_num ) ( (in_word) & bit_word[ bit_num ] )
97
98 // for reversed data images (TIFF 'II')
99 #define set_bit_to_1_inv( to_word, bit_num ) ( (to_word) |= lbit_word[ bit_num ] )
100 #define set_bit_to_0_inv( to_word, bit_num ) ( (to_word) &= ~lbit_word[ bit_num ] )
101 #define is_bit_inv( in_word, bit_num ) ( (in_word) & lbit_word[ bit_num ] )
102 // returns 0x0000 if bit "i" is 0 in "in_word"
103
104 #define bit0_count( in_word ) \
105 ( bit0_cnt[ hi_byte(in_word) ] + bit0_cnt[ low_byte(in_word) ] )
106 #define bit1_count( in_word ) \
107 ( bit1_cnt[ hi_byte(in_word) ] + bit1_cnt[ low_byte(in_word) ] )
108
109 #define left_part_0( of_word ) \
110 ( left_byte( of_word ) ? \
111 lr_word( left_0[ left_byte(of_word) ], 0xFF ) \
112 : lr_word( 0x00, left_0[ right_byte(of_word) ] ) \
113 )
114
115 #define right_part_0( of_word ) \
116 ( right_byte( of_word ) ? \
117 lr_word( 0xFF, right_0[ right_byte(of_word) ]) \
118 : lr_word( right_0[ left_byte(of_word) ], 0x00 ) \
119 )
120
121 #endif // __BIT_FUNC_H