hp-timing.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* High precision, low overhead timing functions. x86 version.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _HP_TIMING_H
  16. #define _HP_TIMING_H 1
  17. #include <isa.h>
  18. #if MINIMUM_ISA == 686 || MINIMUM_ISA == 8664
  19. /* We always assume having the timestamp register. */
  20. # define HP_TIMING_AVAIL (1)
  21. # define HP_SMALL_TIMING_AVAIL (1)
  22. /* We indeed have inlined functions. */
  23. # define HP_TIMING_INLINE (1)
  24. /* We use 64bit values for the times. */
  25. typedef unsigned long long int hp_timing_t;
  26. /* That's quite simple. Use the `rdtsc' instruction. Note that the value
  27. might not be 100% accurate since there might be some more instructions
  28. running in this moment. This could be changed by using a barrier like
  29. 'cpuid' right before the `rdtsc' instruciton. But we are not interested
  30. in accurate clock cycles here so we don't do this.
  31. NB: Use __builtin_ia32_rdtsc directly since including <x86intrin.h>
  32. makes building glibc very slow. */
  33. # ifdef USE_RDTSCP
  34. /* RDTSCP waits until all previous instructions have executed and all
  35. previous loads are globally visible before reading the counter.
  36. RDTSC doesn't wait until all previous instructions have been executed
  37. before reading the counter. */
  38. # define HP_TIMING_NOW(Var) \
  39. (__extension__ ({ \
  40. unsigned int __aux; \
  41. (Var) = __builtin_ia32_rdtscp (&__aux); \
  42. }))
  43. # else
  44. # define HP_TIMING_NOW(Var) ((Var) = __builtin_ia32_rdtsc ())
  45. # endif
  46. # include <hp-timing-common.h>
  47. #else
  48. /* NB: Undefine _HP_TIMING_H so that <sysdeps/generic/hp-timing.h> will
  49. be included. */
  50. # undef _HP_TIMING_H
  51. # include <sysdeps/generic/hp-timing.h>
  52. #endif
  53. #endif /* hp-timing.h */