Created
September 26, 2014 18:50
-
-
Save anonymous/c1c788af92abc1d9f2e2 to your computer and use it in GitHub Desktop.
Basic integer arithmetic implemented exclusively using the HLSL preprocessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//numbers are specified by the count of braces around 8 | |
#define C0 8 | |
#define C1 (C0) | |
#define C2 (C1) | |
#define C3 (C2) | |
#define C4 (C3) | |
#define C5 (C4) | |
#define C6 (C5) | |
#define C7 (C6) | |
#define C8 (C7) | |
#define C9 (C8) | |
#define NUM(x) C##x | |
//------------------// | |
#define COUNT8 -1 | |
#define COUNT(x) (1 + C##OUNT##x) | |
//------------------// | |
#define ID8 8 | |
#define ID(x) x | |
#define DEC(x) ID##x | |
#define INC(x) (x) | |
#define NOT_0 1 | |
#define NOT_1 0 | |
#define NOT(x) NOT_##x | |
#define ISZERO_8 1 | |
#define ISZERO_(x) 0 | |
#define ISZERO(x) ISZERO_##x | |
#define ISNZERO_8 0 | |
#define ISNZERO_(x) 1 | |
#define ISNZERO(x) ISNZERO_##x | |
#define CONC(x, y) x##y | |
#define CONC2(x, y, z) x##y##z | |
#define GREATER_11(x, y) 0 | |
#define GREATER_10(x, y) 0 | |
#define GREATER_01(x, y) 1 | |
#define GREATER_00(x, y) GREATER(DEC(x), DEC(y)) | |
#define GREATER(x, y) CONC2(GREATER_, ISZERO(x), ISZERO(y))(x, y) | |
#define SMALLER_11(x, y) 0 | |
#define SMALLER_10(x, y) 1 | |
#define SMALLER_01(x, y) 0 | |
#define SMALLER_00(x, y) SMALLER(DEC(x), DEC(y)) | |
#define SMALLER(x, y) CONC2(SMALLER_, ISZERO(x), ISZERO(y))(x, y) | |
#define EQUALS_11(x, y) 1 | |
#define EQUALS_10(x, y) 0 | |
#define EQUALS_01(x, y) 0 | |
#define EQUALS_00(x, y) EQUALS(DEC(x), DEC(y)) | |
#define EQUALS(x, y) CONC2(EQUALS_, ISZERO(x), ISZERO(y))(x, y) | |
#define ADD_1(x, y) x | |
#define ADD_0(x, y) ADD(INC(x), DEC(y)) | |
#define ADD(x, y) CONC(ADD_, ISZERO(y))(x, y) | |
#define SUB_1(x, y) x | |
#define SUB_0(x, y) SUB(DEC(x), DEC(y)) | |
#define SUB(x, y) CONC(SUB_, ISZERO(y))(x, y) | |
#define MUL_1(x, y, w) w | |
#define MUL_0(x, y, w) MUL_S(x, DEC(y), ADD(w, x)) | |
#define MUL_S(x, y, w) CONC(MUL_, ISZERO(y))(x, y, w) | |
#define MUL(x, y) MUL_S(x, y, C0) | |
#define DIV_1(x, y, w) w | |
#define DIV_0_1(x, y, w) w | |
#define DIV_0_0(x, y, w) DIV_S(SUB(x, y), y, INC(w)) | |
#define DIV_0(x, y, w) CONC(DIV_0_, SMALLER(x, y))(x, y, w) | |
#define DIV_S(x, y, w) CONC(DIV_, ISZERO(x))(x, y, w) | |
#define DIV_S_1(x, y) C0 | |
#define DIV_S_0(x, y) DIV_S(x, y, C0) | |
#define DIV(x, y) CONC(DIV_S_, ISZERO(y))(x, y) | |
// examples: | |
return COUNT(C4);// 1+1+1+1+1-1 (4) | |
return EQUALS(C1, C1);// 1 | |
return GREATER(C2, C3);// 0 | |
return COUNT(ADD(C3, C2));// 1+1+1+1+1+1-1 (5) | |
return EQUALS(SUB(C5, C8), C0);// 1 | |
return COUNT(DIV(C6, C3));// 1+1+1-1 (2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment