mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-18 02:43:40 +00:00
Estimated hours taken: 1 runtime/*.h: runtime/*.c: runtime/mercury_conf.h.in: Remove old .h files. Update #includes to refer to mercury_*.h Update #ifdef MODULE_H to be #ifdef MERCURY_MODULE_H
49 lines
1.1 KiB
C
49 lines
1.1 KiB
C
/*
|
|
** Copyright (C) 1997 The University of Melbourne.
|
|
** This file may only be copied under the terms of the GNU Library General
|
|
** Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
*/
|
|
|
|
#include "mercury_imp.h"
|
|
|
|
/*
|
|
** The function `hash_float()' is used by the library predicate `float__hash'
|
|
** and also for hashing floats for `pragma fact_table' indexing.
|
|
** It computes a non-negative Integer hash value for a Float.
|
|
** The exact hash function used depend on the relative sizes of Float and
|
|
** Integer.
|
|
*/
|
|
|
|
union FloatInteger {
|
|
Float f;
|
|
Integer i;
|
|
Integer j[(sizeof(Float)/sizeof(Integer) > 0
|
|
? sizeof(Float)/sizeof(Integer) : 1)];
|
|
char c[sizeof(Float)/sizeof(char)];
|
|
};
|
|
|
|
Integer
|
|
hash_float(Float f)
|
|
{
|
|
union FloatInteger fi;
|
|
size_t i;
|
|
Integer h = 0;
|
|
|
|
fi.i = 0;
|
|
fi.f = f;
|
|
|
|
if (sizeof(Float) <= sizeof(Integer)) {
|
|
h = fi.i;
|
|
} else if (sizeof(Float) % sizeof(Integer) == 0) {
|
|
for (i = 0; i < sizeof(Float)/sizeof(Integer); i++) {
|
|
h ^= fi.j[i];
|
|
}
|
|
} else {
|
|
for (i = 0; i < sizeof(Float)/sizeof(char); i++) {
|
|
h ^= (h << 5);
|
|
h ^= fi.c[i];
|
|
}
|
|
}
|
|
return (h >= 0 ? h : -h);
|
|
}
|