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