]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs-log/BitByteOps.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs-log / BitByteOps.mod
CommitLineData
1eee94d3
GM
1(* BitByteOps.mod provides a Logitech-3.0 compatible library.
2
a945c346 3Copyright (C) 2007-2024 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. *)
26
27IMPLEMENTATION MODULE BitByteOps ;
28
01cca857 29FROM SYSTEM IMPORT ADR, SHIFT, ROTATE, TSIZE, BITSET8, CARDINAL8 ;
1eee94d3
GM
30
31
32(*
33 GetBits - returns the bits firstBit..lastBit from source.
34 Bit 0 of byte maps onto the firstBit of source.
35*)
36
37PROCEDURE GetBits (source: BYTE; firstBit, lastBit: CARDINAL) : BYTE ;
38VAR
39 si : CARDINAL8 ;
40 sb : BITSET8 ;
41 mask: BITSET8 ;
42 i : CARDINAL ;
43BEGIN
44 sb := VAL (BITSET8, source) ;
45 mask := BITSET8 {} ;
46 FOR i := firstBit TO lastBit DO
47 INCL (mask, i)
48 END ;
49 sb := VAL (BITSET8, source) * mask ;
50 i := 1 ;
51 WHILE firstBit > 0 DO
52 DEC (firstBit) ;
53 i := i*2
54 END ;
55 si := VAL (CARDINAL8, sb) ;
56 RETURN VAL (BYTE, si DIV VAL (CARDINAL8, i))
57END GetBits ;
58
59
60(*
61 SetBits - sets bits in, byte, starting at, firstBit, and ending at,
62 lastBit, with, pattern. The bit zero of, pattern, will
63 be placed into, byte, at position, firstBit.
64*)
65
66PROCEDURE SetBits (VAR byte: BYTE; firstBit, lastBit: CARDINAL;
67 pattern: BYTE) ;
68VAR
69 pb, pp: BITSET8 ;
70 i, j : CARDINAL ;
71BEGIN
72 pb := VAL (BITSET8, byte) ;
73 pp := VAL (BITSET8, pattern) ;
74 j := 0 ;
75 FOR i := firstBit TO lastBit DO
76 IF j IN pp
77 THEN
78 INCL (pb, i)
79 ELSE
80 EXCL (pb, i)
81 END ;
82 INC (j)
83 END ;
84 byte := VAL (BYTE, pb)
85END SetBits ;
86
87
88(*
89 ByteAnd - returns a bitwise (left AND right)
90*)
91
92PROCEDURE ByteAnd (left, right: BYTE) : BYTE ;
93BEGIN
94 RETURN VAL (BYTE, VAL (BITSET8, left) * VAL (BITSET8, right))
95END ByteAnd ;
96
97
98(*
99 ByteOr - returns a bitwise (left OR right)
100*)
101
102PROCEDURE ByteOr (left, right: BYTE) : BYTE ;
103BEGIN
104 RETURN VAL (BYTE, VAL (BITSET8, left) + VAL (BITSET8, right))
105END ByteOr ;
106
107
108(*
109 ByteXor - returns a bitwise (left XOR right)
110*)
111
112PROCEDURE ByteXor (left, right: BYTE) : BYTE ;
113BEGIN
114 RETURN VAL (BYTE, VAL (BITSET8, left) DIV VAL (BITSET8, right))
115END ByteXor ;
116
117
118(*
119 ByteNot - returns a byte with all bits inverted.
120*)
121
122PROCEDURE ByteNot (byte: BYTE) : BYTE ;
123BEGIN
124 RETURN VAL (BYTE, -VAL (BITSET8, byte))
125END ByteNot ;
126
127
128(*
129 ByteShr - returns a, byte, which has been shifted, count
130 bits to the right.
131*)
132
133PROCEDURE ByteShr (byte: BYTE; count: CARDINAL) : BYTE ;
134BEGIN
135 RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), count))
136END ByteShr ;
137
138
139(*
140 ByteShl - returns a, byte, which has been shifted, count
141 bits to the left.
142*)
143
144PROCEDURE ByteShl (byte: BYTE; count: CARDINAL) : BYTE ;
145BEGIN
146 RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), -VAL (INTEGER, count)))
147END ByteShl ;
148
149
150(*
151 ByteSar - shift byte arthemetic right. Preserves the top
152 end bit as the value is shifted right.
153*)
154
155PROCEDURE ByteSar (byte: BYTE; count: CARDINAL) : BYTE ;
156VAR
157 b: BYTE ;
158BEGIN
159 IF MAX(BITSET8) IN VAL(BITSET8, byte)
160 THEN
161 b := VAL (BYTE, SHIFT (VAL (BITSET8, byte), count) + BITSET8 {MAX (BITSET8)}) ;
162 RETURN b
163 ELSE
164 RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), count))
165 END
166END ByteSar ;
167
168
169(*
170 ByteRor - returns a, byte, which has been rotated, count
171 bits to the right.
172*)
173
174PROCEDURE ByteRor (byte: BYTE; count: CARDINAL) : BYTE ;
175BEGIN
176 RETURN VAL (BYTE, ROTATE (VAL (BITSET8, byte), count))
177END ByteRor ;
178
179
180(*
181 ByteRol - returns a, byte, which has been rotated, count
182 bits to the left.
183*)
184
185PROCEDURE ByteRol (byte: BYTE; count: CARDINAL) : BYTE ;
186BEGIN
187 RETURN VAL (BYTE, ROTATE (VAL (BITSET8, byte), -VAL (INTEGER, count)))
188END ByteRol ;
189
190
191(*
192 HighHibble - returns the top nibble only from, byte,
193 in the lowest nibble position.
194*)
195
196PROCEDURE HighNibble (byte: BYTE) : BYTE ;
197BEGIN
198 RETURN VAL (BYTE, VAL (CARDINAL8, byte) DIV 16)
199END HighNibble ;
200
201
202(*
203 LowNibble - returns the low nibble only from, byte.
204 The top nibble is replaced by zeros.
205*)
206
207PROCEDURE LowNibble (byte: BYTE) : BYTE ;
208BEGIN
209 RETURN VAL (BYTE, VAL (BITSET8, byte) * BITSET8 {0..3})
210END LowNibble ;
211
212
213(*
214 Swap - swaps the low and high nibbles in the, byte.
215*)
216
217PROCEDURE Swap (byte: BYTE) : BYTE ;
218BEGIN
219 RETURN VAL(BYTE,
220 VAL(BITSET8, VAL (CARDINAL8,
221 VAL (BITSET8, byte) *
222 BITSET8 {4..7}) DIV 16) +
223 VAL(BITSET8, byte) * BITSET8 {0..3})
224END Swap ;
225
226
227END BitByteOps.