2020-07-13 13:13:06 +00:00
|
|
|
/* bug #264 - cc65 fails to warn about a function returning struct */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-08-19 20:25:18 +00:00
|
|
|
typedef uint32_t u32;
|
2020-07-13 13:13:06 +00:00
|
|
|
typedef uint16_t u16;
|
|
|
|
|
2020-08-19 20:25:18 +00:00
|
|
|
/* this struct is too large, we can only handle max 4 bytes right now */
|
2020-07-13 13:13:06 +00:00
|
|
|
typedef struct {
|
2020-08-19 20:25:18 +00:00
|
|
|
u32 quot;
|
|
|
|
u32 rem;
|
2020-07-13 13:13:06 +00:00
|
|
|
} udiv_t;
|
|
|
|
|
2020-08-19 20:25:18 +00:00
|
|
|
udiv_t div3(u32 in) {
|
2020-07-13 13:13:06 +00:00
|
|
|
|
|
|
|
udiv_t u;
|
2020-08-19 20:25:18 +00:00
|
|
|
u32 q = 0;
|
2020-07-13 13:13:06 +00:00
|
|
|
|
|
|
|
while (in >= 300) {
|
|
|
|
in -= 300;
|
|
|
|
q += 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (in >= 30) {
|
|
|
|
in -= 30;
|
|
|
|
q += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (in >= 3) {
|
|
|
|
in -= 3;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
|
|
|
|
u.quot = q;
|
|
|
|
u.rem = in;
|
|
|
|
|
2020-08-19 20:25:18 +00:00
|
|
|
return u; /* error */
|
2020-07-13 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
|
2020-08-19 20:25:18 +00:00
|
|
|
u32 i;
|
2020-07-13 13:13:06 +00:00
|
|
|
div_t d;
|
|
|
|
udiv_t u;
|
|
|
|
|
|
|
|
for (i = 1024; i; i--) {
|
2020-08-19 20:25:18 +00:00
|
|
|
d = div((u16)i, 3);
|
2020-07-13 13:13:06 +00:00
|
|
|
u = div3(i);
|
|
|
|
|
|
|
|
if (d.quot != u.quot || d.rem != u.rem) {
|
|
|
|
printf("Mismatch at %u/3, div %u %u, div3 %u %u\n", i,
|
|
|
|
d.quot, d.rem, u.quot, u.rem);
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|