/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. module runtime.typeinfo.ti_real;
  8. import runtime.util;
  9. class TypeInfo_e : TypeInfo {
  10. char[] toString() { return "real"; }
  11. hash_t getHash(void *p) {
  12. return (cast(uint *)p)[0] + (cast(uint *)p)[1] + (cast(ushort *)p)[4];
  13. }
  14. int equals(void *p1, void *p2) {
  15. return _equals(*cast(real *)p1, *cast(real *)p2);
  16. }
  17. int compare(void *p1, void *p2) {
  18. return _compare(*cast(real *)p1, *cast(real *)p2);
  19. }
  20. size_t tsize() {
  21. return real.sizeof;
  22. }
  23. void swap(void *p1, void *p2) {
  24. real t;
  25. t = *cast(real *)p1;
  26. *cast(real *)p1 = *cast(real *)p2;
  27. *cast(real *)p2 = t;
  28. }
  29. void[] init() {
  30. static real r;
  31. return (cast(real *)&r)[0 .. 1];
  32. }
  33. package:
  34. static int _equals(real f1, real f2) {
  35. return f1 == f2 || (isnan(f1) && isnan(f2));
  36. }
  37. static int _compare(real d1, real d2) {
  38. // if either are NaN
  39. if (d1 !<>= d2) {
  40. if (isnan(d1)) {
  41. if (isnan(d2)) {
  42. return 0;
  43. }
  44. return -1;
  45. }
  46. return 1;
  47. }
  48. return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1);
  49. }
  50. }