saransk 03.11.2011 22:31 QIP

/*izvrat.h*/

/* bin to hex */
#define b_0000 0
#define b_0001 1
#define b_0010 2
#define b_0011 3
#define b_0100 4
#define b_0101 5
#define b_0110 6
#define b_0111 7
#define b_1000 8
#define b_1001 9
#define b_1010 A
#define b_1011 B
#define b_1100 C
#define b_1101 D
#define b_1110 E
#define b_1111 F
#define b_(a,b,c,d) b_##a##b##c##d
#define hex_(a7,a6,a5,a4,a3,a2,a1,a0) b_(a7,a6,a5,a4),b_(a3,a2,a1,a0)
#define hex(a) hex_(a)
#define cat_(h,l) h##l
#define cat(a) cat_(a)

/* hex to bin */
#define h_0 0,0,0,0
#define h_1 0,0,0,1
#define h_2 0,0,1,0
#define h_3 0,0,1,1
#define h_4 0,1,0,0
#define h_5 0,1,0,1
#define h_6 0,1,1,0
#define h_7 0,1,1,1
#define h_8 1,0,0,0
#define h_9 1,0,0,1
#define h_A 1,0,1,0
#define h_B 1,0,1,1
#define h_C 1,1,0,0
#define h_D 1,1,0,1
#define h_E 1,1,1,0
#define h_F 1,1,1,1
#define h_(a) h_##a
#define bin(h,l) h_(h),h_(l)

/* Табличный not */
#define not_0 1
#define not_1 0
#define not_(a) not_##a
#define not(a) not_(a)

/* Табличный and */
#define and_00 0
#define and_01 0
#define and_10 0
#define and_11 1
#define and_(a,b) and_##a##b
#define and(a,b) and_(a,b)

#define or(a,b) not(and(not(a),not(b)))

#define xor(a,b) or(and(a,not(b)),and(not(a),b))

/* Полусумматор */
#define hsum(a,b) and(a,b),xor(a,b)

#define C_(c,s) c
#define C(a) C_(a)

#define S_(c,s) s
#define S(a) S_(a)

/* Сумматор */
#define sum2(a,b,c) hsum(S(hsum(a,b)),c)
#define sum(a,b,c) or(C(hsum(a,b)),C(sum2(a,b,c))),S(sum2(a,b,c))

#define adc0(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) sum(a0,b0,c)
#define adc1(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a1,b1,C(adc0(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc2(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a2,b2,C(adc1(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc3(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a3,b3,C(adc2(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc4(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a4,b4,C(adc3(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc5(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a5,b5,C(adc4(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc6(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a6,b6,C(adc5(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))
#define adc7(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
sum(a7,b7,C(adc6(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)))

/* Сложение с переносом */
#define adc_(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) \
S(adc7(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc6(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc5(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc4(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc3(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc2(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc1(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c)),\
S(adc0(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c))
#define adc(a,b,c) adc_(a,b,c)

/* Сложение */
#define add_(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0) \
adc_(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, 0)
#define add(a,b) add_(a,b)

#define pack(a7,a6,a5,a4,a3,a2,a1,a0) a7,a6,a5,a4,a3,a2,a1,a0

/* Инвертирование */
#define inv_(a7,a6,a5,a4,a3,a2,a1,a0) \
not(a7),not(a6),not(a5),not(a4),not(a3),not(a2),not(a1),not(a0)
#define inv(a) inv_(a)

/* Обращение знака */
#define neg(a) add(inv_(a),bin(0,1))

/* Вычитание */
#define sub(a,b) add_(a,add(inv_(a),pack(0,0,0,0,0,0,0,1)))

1. rtsome 03.11.2011 22:34

типикал си: макросы без макросов ради макросов.

Do you really want to delete ?