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