perfmodel_nan.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015-2017 CNRS
  4. * Copyright (C) 2014-2015 Université de Bordeaux
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <common/config.h>
  22. #include <core/perfmodel/perfmodel.h>
  23. #include <ctype.h>
  24. /** Some systems cannot read NAN values, yes, it is really bad ... */
  25. #if defined(STARPU_HAVE_WINDOWS) || defined(STARPU_OPENBSD_SYS)
  26. # define _STARPU_OWN_NAN 1
  27. #else
  28. # define _STARPU_OWN_NAN 0
  29. #endif
  30. #if _STARPU_OWN_NAN == 1
  31. static
  32. void _starpu_read_spaces(FILE *f)
  33. {
  34. int c = getc(f);
  35. if (isspace(c))
  36. {
  37. while (isspace(c)) c = getc(f);
  38. ungetc(c, f);
  39. }
  40. else
  41. {
  42. ungetc(c, f);
  43. }
  44. }
  45. #endif /* _STARPU_OWN_NAN */
  46. void _starpu_write_double(FILE *f, const char *format, double val)
  47. {
  48. #if _STARPU_OWN_NAN == 1
  49. if (isnan(val))
  50. {
  51. fprintf(f, "NaN");
  52. }
  53. else
  54. {
  55. fprintf(f, format, val);
  56. }
  57. #else
  58. fprintf(f, format, val);
  59. #endif
  60. }
  61. int _starpu_read_double(FILE *f, char *format, double *val)
  62. {
  63. #if _STARPU_OWN_NAN == 1
  64. _starpu_read_spaces(f);
  65. int x1 = getc(f);
  66. if (x1 == 'N')
  67. {
  68. int x2 = getc(f);
  69. int x3 = getc(f);
  70. if (x2 == 'a' && x3 == 'N')
  71. {
  72. #ifdef _MSC_VER
  73. unsigned long long _mynan = 0x7fffffffffffffffull;
  74. double mynan = *(double*)&_mynan;
  75. #else
  76. double mynan = NAN;
  77. #endif
  78. *val = mynan;
  79. return 1;
  80. }
  81. else
  82. {
  83. return 0;
  84. }
  85. }
  86. else
  87. {
  88. ungetc(x1, f);
  89. return fscanf(f, format, val);
  90. }
  91. #else
  92. return fscanf(f, format, val);
  93. #endif
  94. }