PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/typeinfo/ti_double.d

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