8
8
#include <inttypes.h>
9
9
#include "pico/stdlib.h"
10
10
#include "pico/bit_ops.h"
11
+ #include <stdlib.h>
12
+
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
+ }
11
67
12
68
int main () {
13
69
setup_default_uart ();
14
70
15
- puts ("Hellox, worlxxcd !" );
71
+ puts ("Hellox, world !" );
16
72
printf ("Hello world %d\n" , 2 );
17
73
#if PICO_NO_HARDWARE
18
74
puts ("This is native" );
@@ -29,6 +85,8 @@ int main() {
29
85
(unsigned long long ) __revll (xl ));
30
86
}
31
87
88
+ test_builtin_bitops ();
89
+
32
90
for (int i = 0 ; i < 8 ; i ++ ) {
33
91
sleep_ms (500 );
34
92
printf ( "%" PRIu64 "\n" , to_us_since_boot (get_absolute_time ()));
@@ -42,138 +100,3 @@ int main() {
42
100
}
43
101
puts ("DONE" );
44
102
}
45
-
46
- void test1 () {
47
- uint32_t x = 0 ;
48
- for (int i = 0 ; i < 1000 ; i ++ ) {
49
- x += __builtin_popcount (i );
50
- x += __builtin_popcountl (i );
51
- x += __builtin_popcountll (i * 1234567ll );
52
- x += __builtin_clz (i );
53
- x += __builtin_clzl (i );
54
- x += __builtin_clzll (i * 1234567ll );
55
- x += __builtin_ctz (i );
56
- x += __builtin_ctzl (i );
57
- x += __builtin_ctzll (i * 1234567ll );
58
- }
59
- if (x > 12345677 ) {
60
- puts ("ok" );
61
- }
62
- }
63
-
64
- #if 0
65
- struct event {
66
-
67
- };
68
-
69
- // something might be asyncrhonous.. it communicates the result via the event
70
- void do_something (struct event * e , int a , unsigned int b , char * c ) {
71
- if (a == b ) puts (c );
72
- }
73
-
74
- int32_t event_result_timeout_ms (struct event * e , int32_t timeout_ms );
75
- int32_t event_result_timeout_us (struct event * e , int32_t timeout_us );
76
- bool is_event_done (struct event * e );
77
- // asserts if not done
78
- int32_t event_result (struct event * e );
79
- void event_set_callback (struct event * e , void (* callback )(struct event * e ));
80
- void init_multi_event (struct event * target , struct event * * events , uint event_count );
81
-
82
- #define timeout_ms_result (f , timeout ) ({ \
83
- struct event __event; \
84
- struct event *event = &__event; \
85
- (f); \
86
- event_result_timeout_ms(event, timeout); \
87
- })
88
-
89
- #define blocking_result (f ) timeout_ms_result(f, -1)
90
- #define on_complete (f , cb ) ({ \
91
- static struct event __event; \
92
- struct event *event = &__event; \
93
- (f); \
94
- event_set_callback(event, my_callback); \
95
- })
96
-
97
- void test2 () {
98
- // just playing with blocking syntax
99
- struct event e ;
100
- do_something (& e , 1 , 1 , "Hello" );
101
- uint32_t result = event_result_timeout_ms (& e , -1 );
102
- }
103
-
104
- void test3 () {
105
- uint32_t result = blocking_result (do_something (event , 1 , 1 , "Hello" ));
106
- }
107
-
108
- void test4 () {
109
- struct event e ;
110
- do_something (& e , 1 , 1 , "Hello" );
111
- // this would poll (down to hardware if there is no asynchronous mechanism)
112
- while (!is_event_done (& e )) {
113
- puts ("waiting" );
114
- }
115
- int32_t result = event_result (& e );
116
- }
117
-
118
- void my_callback (struct event * event ) {
119
- puts ("Its done" );
120
- int32_t result = event_result (event );
121
- }
122
-
123
- void test5 () {
124
- static struct event e ;
125
- do_something (& e , 1 , 1 , "Hello" );
126
- event_set_callback (& e , my_callback );
127
- }
128
-
129
- void test6 () {
130
- on_complete (do_something (event , 1 , 1 , "Hello" ), my_callback );
131
- }
132
-
133
- static struct event e1 ;
134
- static struct event e2 ;
135
- static struct event * events [2 ] = {& e1 , & e2 };
136
- static struct event multi ;
137
-
138
- void test7 () {
139
- init_multi_event (& multi ,events , count_of (events ));
140
- do_something (& e1 , 1 , 1 , "Hello" );
141
- do_something (& e2 , 1 , 3 , "Hello" );
142
- // something like this
143
- }
144
-
145
- struct dimpl {
146
- uint8_t type ;
147
- };
148
-
149
- struct doodad {
150
- struct dimpl * type ;
151
- uint32_t param ;
152
- };
153
-
154
- struct dimpl indefinite_waiter = {
155
- .type = 1
156
- };
157
-
158
- extern struct dimpl INDEFINITE_WAIT ;
159
-
160
- struct dimpl ms_waiter = {
161
- .type = 1
162
- };
163
-
164
- struct doodad blocking_with_timeout_ms (uint32_t ms ) {
165
- struct doodad rc = {
166
- .type = & ms_waiter ,
167
- .param = ms
168
- };
169
- return rc ;
170
- }
171
-
172
- struct result {
173
-
174
- };
175
-
176
- struct result my_api_call (int arg , float x , struct doodad behavior ) {
177
-
178
- }
179
- #endif
0 commit comments