[Q. This code for what? :9]
#include <stdio.h>
#include <stdint.h>
#include <string.h>
uint8_t do_csa(uint8_t *pa, uint8_t *pb)
{
uint8_t a_xor_b, a_and_b;
a_xor_b = *pa ^ *pb;
a_and_b = *pa & *pb;
*pa = a_xor_b;
*pb = a_and_b << 1;
return (a_and_b >> 7);
}
char *uint8_to_str(char *pstr, uint8_t q)
{
switch (q & (uint8_t)0xF0) {
case (uint8_t)0x00 : strcpy(&pstr[0], "0000"); break;
case (uint8_t)0x10 : strcpy(&pstr[0], "0001"); break;
case (uint8_t)0x20 : strcpy(&pstr[0], "0010"); break;
case (uint8_t)0x30 : strcpy(&pstr[0], "0011"); break;
case (uint8_t)0x40 : strcpy(&pstr[0], "0100"); break;
case (uint8_t)0x50 : strcpy(&pstr[0], "0101"); break;
case (uint8_t)0x60 : strcpy(&pstr[0], "0110"); break;
case (uint8_t)0x70 : strcpy(&pstr[0], "0111"); break;
case (uint8_t)0x80 : strcpy(&pstr[0], "1000"); break;
case (uint8_t)0x90 : strcpy(&pstr[0], "1001"); break;
case (uint8_t)0xA0 : strcpy(&pstr[0], "1010"); break;
case (uint8_t)0xB0 : strcpy(&pstr[0], "1011"); break;
case (uint8_t)0xC0 : strcpy(&pstr[0], "1100"); break;
case (uint8_t)0xD0 : strcpy(&pstr[0], "1101"); break;
case (uint8_t)0xE0 : strcpy(&pstr[0], "1110"); break;
case (uint8_t)0xF0 : strcpy(&pstr[0], "1111"); break;
default : strcpy(&pstr[0], "????"); break;
}
switch (q & (uint8_t)0x0F) {
case (uint8_t)0x00 : strcpy(&pstr[4], "0000"); break;
case (uint8_t)0x01 : strcpy(&pstr[4], "0001"); break;
case (uint8_t)0x02 : strcpy(&pstr[4], "0010"); break;
case (uint8_t)0x03 : strcpy(&pstr[4], "0011"); break;
case (uint8_t)0x04 : strcpy(&pstr[4], "0100"); break;
case (uint8_t)0x05 : strcpy(&pstr[4], "0101"); break;
case (uint8_t)0x06 : strcpy(&pstr[4], "0110"); break;
case (uint8_t)0x07 : strcpy(&pstr[4], "0111"); break;
case (uint8_t)0x08 : strcpy(&pstr[4], "1000"); break;
case (uint8_t)0x09 : strcpy(&pstr[4], "1001"); break;
case (uint8_t)0x0A : strcpy(&pstr[4], "1010"); break;
case (uint8_t)0x0B : strcpy(&pstr[4], "1011"); break;
case (uint8_t)0x0C : strcpy(&pstr[4], "1100"); break;
case (uint8_t)0x0D : strcpy(&pstr[4], "1101"); break;
case (uint8_t)0x0E : strcpy(&pstr[4], "1110"); break;
case (uint8_t)0x0F : strcpy(&pstr[4], "1111"); break;
default : strcpy(&pstr[4], "????"); break;
}
return pstr;
}
int main(int argc, char *arcv[])
{
uint8_t a, b;
uint8_t a_add_b;
uint8_t a_csa, b_csa;
uint8_t n_csa;
char a_str[] = "00000000";
char b_str[] = "00000000";
for (a = 0; a < (uint8_t)0x80; a++) {
for (b = 0; b < (uint8_t)0x80; b++) {
a_add_b= a + b;
a_csa = a;
b_csa = b;
n_csa = 0;
do {
printf(" CSA(%s, %s)",
uint8_to_str(a_str, a_csa),
uint8_to_str(b_str, b_csa));
do_csa(&a_csa, &b_csa);
n_csa++;
printf(" = (%s, %s) ... %d\n",
uint8_to_str(a_str, a_csa),
uint8_to_str(b_str, b_csa), n_csa);
} while (b_csa != (uint8_t)0x00);
printf("%02X | %02X || %02X | %02X || %s\n",
a, b, a_add_b,
a_csa, (a_add_b != a_csa) ? "NG" : "OK");
}
}
return 0;
}
2 件のコメント:
CSA stand for carry-save adder.
right, keing312.karma++;
# s/stand/stands/
コメントを投稿