2
0

util.c 949 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include "nmrpd.h"
  5. #ifdef NMRPFLASH_OSX
  6. #include <mach/mach_time.h>
  7. #endif
  8. time_t time_monotonic()
  9. {
  10. #ifndef NMRPFLASH_WINDOWS
  11. #ifndef NMRPFLASH_OSX
  12. struct timespec ts;
  13. clock_gettime(CLOCK_MONOTONIC, &ts);
  14. return ts.tv_sec;
  15. #else
  16. static double factor = 0.0;
  17. mach_timebase_info_data_t timebase;
  18. if (factor == 0.0) {
  19. mach_timebase_info(&timebase);
  20. factor = (double)timebase.numer / timebase.denom;
  21. }
  22. return round(mach_absolute_time() * factor / 1e9);
  23. #endif
  24. #else
  25. return round(GetTickCount() / 1000.0);
  26. #endif
  27. }
  28. char *lltostr(long long ll, int base)
  29. {
  30. static char buf[32];
  31. snprintf(buf, sizeof(buf) - 1, (base == 16 ? "%llx" : (base == 8 ? "%llo" : "%lld")), ll);
  32. return buf;
  33. }
  34. uint32_t bitcount(uint32_t n)
  35. {
  36. uint32_t c;
  37. for (c = 0; n; ++c) {
  38. n &= n - 1;
  39. }
  40. return c;
  41. }
  42. uint32_t netmask(uint32_t count)
  43. {
  44. return htonl(count <= 32 ? 0xffffffff << (32 - count) : 0);
  45. }