PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/typeinfo/ti_real.d

http://github.com/wilkie/djehuty
D | 63 lines | 43 code | 13 blank | 7 comment | 7 complexity | 63f2ca5cb66302315f6a9182cbddb008 MD5 | raw file
 1/*
 2 * ti_real.d
 3 *
 4 * This module implements the TypeInfo for the real type.
 5 *
 6 */
 7
 8module runtime.typeinfo.ti_real;
 9
10import runtime.util;
11
12class TypeInfo_e : TypeInfo {
13	char[] toString() { return "real"; }
14
15	hash_t getHash(void *p)	{
16		return (cast(uint *)p)[0] + (cast(uint *)p)[1] + (cast(ushort *)p)[4];
17	}
18
19	int equals(void *p1, void *p2) {
20		return _equals(*cast(real *)p1, *cast(real *)p2);
21	}
22
23	int compare(void *p1, void *p2) {
24		return _compare(*cast(real *)p1, *cast(real *)p2);
25	}
26
27	size_t tsize() {
28		return real.sizeof;
29	}
30
31	void swap(void *p1, void *p2) {
32		real t;
33
34		t = *cast(real *)p1;
35		*cast(real *)p1 = *cast(real *)p2;
36		*cast(real *)p2 = t;
37	}
38
39	void[] init() {
40		static real r;
41
42		return (cast(real *)&r)[0 .. 1];
43	}
44
45package:
46	static int _equals(real f1, real f2) {
47		return f1 == f2 || (isnan(f1) && isnan(f2));
48	}
49
50	static int _compare(real d1, real d2) {
51		// if either are NaN
52		if (d1 !<>= d2) {
53			if (isnan(d1)) {
54				if (isnan(d2)) {
55					return 0;
56				}
57				return -1;
58			}
59			return 1;
60		}
61		return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1);
62	}
63}