pico_stdlib_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdio.h>
  7. // Include sys/types.h before inttypes.h to work around issue with
  8. // certain versions of GCC and newlib which causes omission of PRIu64
  9. #include <sys/types.h>
  10. #include <inttypes.h>
  11. #include "pico/stdlib.h"
  12. #include "pico/bit_ops.h"
  13. #include <stdlib.h>
  14. void test_builtin_bitops() {
  15. int32_t x = 0;
  16. for (uint32_t i = 0; i < 10000; i++) {
  17. uint32_t vals32[] = {
  18. i,
  19. 1u << (i & 31u),
  20. i * 12355821u,
  21. };
  22. uint64_t vals64[] = {
  23. i,
  24. 1ull << (i & 63u),
  25. i * 12345678123125ull,
  26. };
  27. for(int j=0; j<count_of(vals32); j++) {
  28. x += __builtin_popcount(vals32[j]);
  29. x += __builtin_popcountl(vals32[j]);
  30. x += (int32_t)__rev(vals32[j]);
  31. #if !PICO_ON_DEVICE
  32. // the following functions are undefined on host mode, but on RP2040 we return 32
  33. if (vals32[j]) {
  34. x += __builtin_clz(vals32[j]);
  35. x += __builtin_ctz(vals32[j]);
  36. } else {
  37. x += 64;
  38. }
  39. #else
  40. x += __builtin_clz(vals32[j]);
  41. x += __builtin_ctz(vals32[j]);
  42. // check l variants are the same
  43. if (__builtin_clz(vals32[j]) != __builtin_clzl(vals32[j])) x += 17;
  44. if (__builtin_ctz(vals32[j]) != __builtin_ctzl(vals32[j])) x += 23;
  45. #endif
  46. }
  47. for(int j=0; j<count_of(vals64); j++) {
  48. x += __builtin_popcountll(vals64[j]);
  49. x += (int32_t)__revll(vals64[j]);
  50. #if !PICO_ON_DEVICE
  51. // the following functions are undefined on host mode, but on RP2040 we return 64
  52. if (!vals64[j]) {
  53. x += 128;
  54. continue;
  55. }
  56. #endif
  57. x += __builtin_clzll(vals64[j]);
  58. x += __builtin_ctzll(vals64[j]);
  59. }
  60. }
  61. printf("Count is %d\n", (int)x);
  62. int32_t expected = 1475508680;
  63. if (x != expected) {
  64. printf("FAILED (expected count %d\n", (int) expected);
  65. exit(1);
  66. }
  67. }
  68. int main() {
  69. setup_default_uart();
  70. puts("Hellox, world!");
  71. printf("Hello world %d\n", 2);
  72. #if PICO_NO_HARDWARE
  73. puts("This is native");
  74. #endif
  75. #if PICO_NO_FLASH
  76. puts("This is no flash");
  77. #endif
  78. for (int i = 0; i < 64; i++) {
  79. uint32_t x = 1 << i;
  80. uint64_t xl = 1ull << i;
  81. // printf("%d %u %u %u %u \n", i, (uint)(x%10u), (uint)(x%16u), (uint)(xl %10u), (uint)(xl%16u));
  82. printf("%08x %08x %016llx %016llx\n", (uint) x, (uint) __rev(x), (unsigned long long) xl,
  83. (unsigned long long) __revll(xl));
  84. }
  85. test_builtin_bitops();
  86. for (int i = 0; i < 8; i++) {
  87. sleep_ms(500);
  88. printf( "%" PRIu64 "\n", to_us_since_boot(get_absolute_time()));
  89. }
  90. absolute_time_t until = delayed_by_us(get_absolute_time(), 500000);
  91. printf("\n");
  92. for (int i = 0; i < 8; i++) {
  93. sleep_until(until);
  94. printf("%" PRIu64 "\n", to_us_since_boot(get_absolute_time()));
  95. until = delayed_by_us(until, 500000);
  96. }
  97. puts("DONE");
  98. }