Калькулятор на препроцессоре [фанатам дефайнов]

izvrat.h/* bin to hex */
  1. define b_0000 0
  2. define b_0001 1
  3. define b_0010 2
  4. define b_0011 3
  5. define b_0100 4
  6. define b_0101 5
  7. define b_0110 6
  8. define b_0111 7
  9. define b_1000 8
  10. define b_1001 9
  11. define b_1010 A
  12. define b_1011 B
  13. define b_1100 C
  14. define b_1101 D
  15. define b_1110 E
  16. define b_1111 F
  17. define b_(a,b,c,d) b_####
  18. define hex_(a7,a6,a5,a4,a3,a2,a1,a0) b_(a7,a6,a5,a4),b_(a3,a2,a1,a0)
  19. define hex(a) hex_(a)
  20. define cat_(h,l) h#
  21. define cat(a) cat_(a)
/* hex to bin */
  1. define h_0 0,0,0,0
  2. define h_1 0,0,0,1
  3. define h_2 0,0,1,0
  4. define h_3 0,0,1,1
  5. define h_4 0,1,0,0
  6. define h_5 0,1,0,1
  7. define h_6 0,1,1,0
  8. define h_7 0,1,1,1
  9. define h_8 1,0,0,0
  10. define h_9 1,0,0,1
  11. define h_A 1,0,1,0
  12. define h_B 1,0,1,1
  13. define h_C 1,1,0,0
  14. define h_D 1,1,0,1
  15. define h_E 1,1,1,0
  16. define h_F 1,1,1,1
  17. define h_(a) h_#
  18. define bin(h,l) h_(h),h_(l)
/* Табличный not */
  1. define not_0 1
  2. define not_1 0
  3. define not_(a) not_#
  4. define not(a) not_(a)
/* Табличный and */
  1. define and_00 0
  2. define and_01 0
  3. define and_10 0
  4. define and_11 1
  5. define and_(a,b) and_##
  6. define and(a,b) and_(a,b)
  7. define or(a,b) not(and(not(a),not(b)))
  8. define xor(a,b) or(and(a,not(b)),and(not(a),b))
/* Полусумматор */
  1. define hsum(a,b) and(a,b),xor(a,b)
  2. define C_(c,s) c#define C(a) C_(a)
  3. define S_(c,s) s#define S(a) S_(a)
/* Сумматор */
  1. define sum2(a,b,c) hsum(S(hsum(a,b)),c)
  2. define sum(a,b,c) or(C(hsum(a,b)),C(sum2(a,b,c))),S(sum2(a,b,c))
  3. define adc0(a7,a6,a5,a4,a3,a2,a1,a0, b7,b6,b5,b4,b3,b2,b1,b0, c) sum(a0,b0,c)
  4. 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)))
  5. 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)))
  6. 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)))
  7. 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)))
  8. 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)))
  9. 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)))
  10. 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)))
/* Сложение с переносом */
  1. 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))
  2. define adc(a,b,c) adc_(a,b,c)
/* Сложение */
  1. 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)
  2. define add(a,b) add_(a,b)
  3. define pack(a7,a6,a5,a4,a3,a2,a1,a0) a7,a6,a5,a4,a3,a2,a1,a0
/* Инвертирование */
  1. 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)
  2. define inv(a) inv_(a)
/* Обращение знака */
  1. define neg(a) add(inv_(a),bin(0,1))
/* Вычитание */
  1. define sub(a,b) add_(a,add(inv_(a),pack(0,0,0,0,0,0,0,1)))
Даллее код :)<izvrat.h>

0x21+0x3f=cat(hex(add(bin(2,1), bin(3,F))))

Последняя правка: пн, 12/11/2007 - 05:50
Submitted by Relyer on

GameDev.by