4.4. Packed Data

There are two systems for referring to packed data, one in which as unnamed field is selected from any computer word which holds data (see Section, and one in which the data format is declared in advance. In the latter system, with which this section is concerned, the format is replicated to form a table. A group of n words is arbitrarily partitioned into bit-fields (with no fields crossing a word boundary), and the same partitioning is applied to as many such groups (m say) as are required. The total data-space for a table is thus nm words. Each group is known as a table-entry. The fields are named, so that a combination of field identifier and entry index selects data from all or part of one computer word, known as a table-element. The elements in a table may occupy overlapping fields, and need not together fill all the available space in the entry.

4.4.1. Table Declaration

A table declaration serves two purposes. The first is to provide the table with an identifier, and to associate this identifier with an allocation of word-storage sufficient for the width and number of entries specified. For example

TABLE april [3, 30]

is the beginning of a declaration for the table "april" with 30 entries each 3 words wide, requiring an allocation of 90 words in all. The second purpose of the declaration is to specify the structure of an entry by declaring the elements contained within it, as defined in Section 4.4.2 below. Data-packing is implementation dependent, and the examples will be found to assume a word length of 24 bits. The syntax for a table declaration is

Tabledec ::= 
     TABLE  Id [  Width ,  Length ] 
        [Elementdeclist Elementpresetlist ] Presetlist 

Elementdeclist ::= 
     Elementdec ;   Elementdeclist  

Width ::= Integer 

Length ::= Integer 

Details of the two presetting mechanisms are given in Section 4.6.2.

4.4.2. Table-Element Declaration

A table element declaration associates an element name with a numeric type and with a particular field of each and every entry in the table. The field may be whole or part of a computer word, and the form of a declaration differs accordingly. The syntax for an element declaration, more fully developed in Section, is

Elementdec ::= 
     Id  Numbertype  Wordposition
     Id  Partwordtype  Wordposition ,  Bitposition 

Wordposition ::=  Signedinteger 

Bitposition ::=  Integer

Word-position and bit-position are numbered from zero upward, and the least significant digits of a word occupies bit-position zero. Normally, table-elements will be located so that they fall within the declared width of the table, but a Coral 66 compiler does not check the limits. To improve program legibility, it is suggested that the word BIT be permitted as an alternative to the comma in the above text. The meaning of Bitposition is given in see Section Whole-Word Table-elements

As shown in the syntax of the previous section, the form of declaration for whole-word table-elements is

Id Numbertype Wordposition

For example,

tickets INTEGER 0

declares a pseudo-array of elements named "tickets". (True array elements are located consecutively in store, see Section 4.5.) Each element refers to a (signed) integer occupying a word-position zero in an entry. Similarly,

weight FIXED(16,-4) 1

locates "weight" in word-position 1 with a significance of 16 bits, stopping 4 bits short of the binary point. Floating-point elements are similarly permitted. Part-Word Table Elements

Elements which occupy fields narrower than a computer word (and only such elements) are declared in forms such as

rain UNSIGNED(4, 2) 2,0;
humidity UNSIGNED(6,6) 2,8;
temperature (10,2) 2,14;

for fixed-point elements. The fixed-point scaling is given in brackets (total bits and fraction bits), followed by the word- and bit-position of the field within the entry. Word-position is the word-position within which the field is located, and bit-position is the bit at the least significant end of the field. The word UNSIGNED increases the capacity of the field for positive numbers at the expense of eliminating negative numbers. For example, (4,2) allows numbers from -2.0 to 1.75 whilst UNSIGNED(4,2) allows them from 0.0 to 3.75. If the scale contains only a single integer, e.g.

sunshine UNSIGNED(4) 2,4;

the number in brackets represents the total number of bits for a part-word integer. Though (4,0) and (4) have essentially the same significance, the fact that (4,0) indicates fixed-point type and (4) indicates an integer, should be borne in mind when such references are used in expressions. The syntax of Partwordtype, for substitution in the syntax of Section 4.4.2, is

Partwordtype ::= 
     UNSIGNED  Elementscale 

Elementscale ::= 
    (  Totalbits ,  Fractionbits )
    (  Totalbits ) 

The rules for Totalbits and Fractionbits are in Section 4.1.The number of fraction bits may be negative, zero, or positive, and it is permissable for the binary point to lie outside the declared field.

4.4.3. Example of a Table Declaration

TABLE april [3, 30]
      [ tickets INTEGER 0;
        weight FIXED(16, -4) 1;
        rain UNSIGNED(4, 2) 2, 0;
        sunshine UNSIGNED (4) 2, 4;
        humidity UNSIGNED(6, 6) 2, 8;
        temperature (10, 2) 2, 14]

It should be noted that all the numbers used to describe and locate fields must be constants.

4.4.4. Reference to Tables and Table Elements

A table element is selected by indexing its field identifier. To continue from the example in Section 4.4.3, the rain for april 6th would be written rain[5], for it should be noted than an entry always has the conventional lower bound of zero. In use, the names of table-elements are always indexed. On the other hand, a table identifier such as "april" may stand on its own when a table reference is passed to a procedure. The use of an index with a table-identifier does not (other than accidentally) select an entry of the table. It selects a computer word from the table data regarded as a conventional array of single computer words, with lower index bound zero. Thus the implied bounds of the array "april" are 0 : 89. A word so selected is treated as a signed integer, from which it follows that april[6] in the example would be equivalent to tickets[2]. A table name is normally indexed only for the purpose of running through the table systematically, for example to set all data to zero, or to form a base for overlaying (see Section 4.8).