To do a portable hash, follow these rules:

**Only use other portable-hash results** or portably converted primitives as elements in your calculations.

**Store the hash result as a value**; never interpret it as a sequence of bytes.

**All of the primitives that you use must be converted to a portable form**, with precisely the same results on all machines. This means that you convert all primitive values to HashResult (`unsigned`

`long`

), using only 32 bits.

- For all integer values, this is a simple mask and cast (assume
`kMask32`

is a named constant that stands for the masking value 0xFFFFFFFFU):result = (HashResult) (x & kMask32);

- If one operand is
`unsigned`

`long`

, the other is converted to`unsigned`

`long`

.

- If a shorter unsigned value is converted to a longer one, the value is preserved.

- If a signed value is converted to unsigned, it must have the correct value mod 2^
*n*, where*n*is the number of bits in the unsigned type. So, a conversion to`unsigned`

`long`

yields identical values on different machines as long as the number of bits in an`unsigned`

`long`

is the same.

- If the number of bits in an
`unsigned`

`long`

is greater than or equal to 32, the least 32 bits of the result in from the previous step (c.) is the same across all such machines.

- If one operand is
- For
`double`

and`float`

, the Hash supplied by`Numerics.h`

is portable.result = Hash(x);

result = (x + y) & kMask32

In the future, the Taligent Application Environment will define APIs to assist in computing portable hashes. In the meantime, you might want to write your own helpers, such as a masking function.

[Contents] [Previous] [Next]

Click the icon to mail questions or corrections about this material to Taligent personnel.

Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker