1 (* BitWordOps.mod provides a Logitech-3.0 compatible library.
3 Copyright (C) 2007-2021 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE BitWordOps ;
29 FROM SYSTEM IMPORT BYTE, ADR, SHIFT, ROTATE, TSIZE ;
33 GetBits - returns the bits firstBit..lastBit from source.
34 Bit 0 of word maps onto the firstBit of source.
37 PROCEDURE GetBits (source: WORD; firstBit, lastBit: CARDINAL) : WORD ;
44 sb := VAL (BITSET, source) ;
46 FOR i := firstBit TO lastBit DO
49 sb := VAL (BITSET, source) * mask ;
55 si := VAL (CARDINAL, sb) ;
56 RETURN VAL (WORD, si DIV i)
61 SetBits - sets bits in, word, starting at, firstBit, and ending at,
62 lastBit, with, pattern. The bit zero of, pattern, will
63 be placed into, word, at position, firstBit.
66 PROCEDURE SetBits (VAR word: WORD; firstBit, lastBit: CARDINAL;
72 pw := VAL (BITSET, word) ;
73 pp := VAL (BITSET, pattern) ;
75 FOR i := firstBit TO lastBit DO
84 word := VAL (WORD, pw)
89 WordAnd - returns a bitwise (left AND right)
92 PROCEDURE WordAnd (left, right: WORD) : WORD ;
94 RETURN VAL (WORD, VAL (BITSET, left) * VAL (BITSET, right))
99 WordOr - returns a bitwise (left OR right)
102 PROCEDURE WordOr (left, right: WORD) : WORD ;
104 RETURN VAL (WORD, VAL (BITSET, left) + VAL (BITSET, right))
109 WordXor - returns a bitwise (left XOR right)
112 PROCEDURE WordXor (left, right: WORD) : WORD ;
114 RETURN VAL (WORD, VAL (BITSET, left) DIV VAL (BITSET, right))
119 WordNot - returns a word with all bits inverted.
122 PROCEDURE WordNot (word: WORD) : WORD ;
124 RETURN VAL (WORD, -VAL (BITSET, word))
129 WordShr - returns a, word, which has been shifted, count
133 PROCEDURE WordShr (word: WORD; count: CARDINAL) : WORD ;
135 RETURN SHIFT (VAL (BITSET, word), count)
140 WordShl - returns a, word, which has been shifted, count
144 PROCEDURE WordShl (word: WORD; count: CARDINAL) : WORD ;
146 RETURN SHIFT (VAL (BITSET, word), -VAL (INTEGER, count))
151 WordSar - shift word arthemetic right. Preserves the top
152 end bit and as the value is shifted right.
155 PROCEDURE WordSar (word: WORD; count: CARDINAL) : WORD ;
159 IF MAX (BITSET) IN VAL (BITSET, word)
161 w := VAL (WORD, SHIFT (VAL (BITSET, word), count)) ;
162 SetBits(w, MAX (BITSET) - count, MAX (BITSET), -BITSET{}) ;
165 RETURN SHIFT(VAL(BITSET, word), count)
171 WordRor - returns a, word, which has been rotated, count
175 PROCEDURE WordRor (word: WORD; count: CARDINAL) : WORD ;
177 RETURN ROTATE (VAL (BITSET, word), count)
182 WordRol - returns a, word, which has been rotated, count
186 PROCEDURE WordRol (word: WORD; count: CARDINAL) : WORD ;
188 RETURN ROTATE (VAL (BITSET, word), -VAL (INTEGER, count))
193 HighByte - returns the top byte only from, word.
194 The byte is returned in the bottom byte
198 PROCEDURE HighByte (word: WORD) : WORD ;
200 p, q : POINTER TO ARRAY [0..TSIZE(WORD)-1] OF BYTE ;
206 q^[0] := p^[TSIZE(WORD)-1] ;
212 LowByte - returns the low byte only from, word.
213 The byte is returned in the bottom byte
217 PROCEDURE LowByte (word: WORD) : WORD ;
219 p, q : POINTER TO ARRAY [0..TSIZE(WORD)-1] OF BYTE ;
231 Swap - byte flips the contents of word.
234 PROCEDURE Swap (word: WORD) : WORD ;
236 p : POINTER TO ARRAY [0..TSIZE(WORD)-1] OF BYTE ;
241 j := TSIZE (WORD)-1 ;
242 FOR i := 0 TO (TSIZE (WORD) DIV 2)-1 DO