pico_stdlib_test.c 3.0 KB

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