#ifdef VECTREX #ifdef BUT_HOSTED_ON_LINUX #ifndef int8_t #define int8_t char #endif #ifndef int16_t #define int16_t short #endif #ifndef int32_t #define int32_t long #endif #ifndef int64_t #define int64_t long long #endif #else #ifndef int8_t #define int8_t int #endif #ifndef int16_t #define int16_t long #endif #ifndef int32_t #define int32_t long long #endif #endif #else // not VECTREX... #include <stdio.h> #include <stdlib.h> #ifndef int8_t #define int8_t char #endif #ifndef int16_t #define int16_t short #endif #ifndef int32_t #define int32_t long #endif #ifndef int64_t #define int64_t long long #endif #endif // ===================================================================================== // DIVIDING // ===================================================================================== /* Macros for division of an UNSIGNED 8-bit integer by constants 2 through 127 */ #define div2(x) (unsigned int8_t)((unsigned int16_t)(x) >> 1UL) #define div3(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 85UL) >> 8UL) #define div4(x) (unsigned int8_t)((unsigned int16_t)(x) >> 2UL) #define div5(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 51UL) >> 8UL) #define div6(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 42UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*6UL), (unsigned int8_t)temp+(rem >= 6U ? 1U : 0U))) #define div7(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 36UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*7UL), (unsigned int8_t)temp+(rem >= 7U ? 1U : 0U))) #define div8(x) (unsigned int8_t)((unsigned int16_t)(x) >> 3UL) #define div9(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 28UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*9UL), (unsigned int8_t)temp+(rem >= 9U ? 1U : 0U))) #define div10(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 25UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*10UL), (unsigned int8_t)temp+(rem >= 10U ? 1U : 0U))) #define div11(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 23UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*11UL), (unsigned int8_t)temp+(rem >= 11U ? 1U : 0U))) #define div12(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 21UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*12UL), (unsigned int8_t)temp+(rem >= 12U ? 1U : 0U))) #define div13(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 19UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*13UL), (unsigned int8_t)temp+(rem >= 13U ? 1U : 0U))) #define div14(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 18UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*14UL), (unsigned int8_t)temp+(rem >= 14U ? 1U : 0U))) #define div15(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 17UL) >> 8UL) #define div16(x) (unsigned int8_t)((unsigned int16_t)(x) >> 4UL) #define div17(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 15UL) >> 8UL) #define div18(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 14UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*18UL), (unsigned int8_t)temp+(rem >= 18U ? 1U : 0U))) #define div19(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 13UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*19UL), (unsigned int8_t)temp+(rem >= 19U ? 1U : 0U))) #define div20(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 12UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*20UL), (unsigned int8_t)temp+(rem >= 20U ? 1U : 0U))) #define div21(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 12UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*21UL), (unsigned int8_t)temp+(rem >= 21U ? 1U : 0U))) #define div22(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 11UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*22UL), (unsigned int8_t)temp+(rem >= 22U ? 1U : 0U))) #define div23(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 11UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*23UL), (unsigned int8_t)temp+(rem >= 23U ? 1U : 0U))) #define div24(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 10UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*24UL), (unsigned int8_t)temp+(rem >= 24U ? 1U : 0U))) #define div25(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 10UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*25UL), (unsigned int8_t)temp+(rem >= 25U ? 1U : 0U))) #define div26(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 9UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*26UL), (unsigned int8_t)temp+(rem >= 26U ? 1U : 0U))) #define div27(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 9UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*27UL), (unsigned int8_t)temp+(rem >= 27U ? 1U : 0U))) #define div28(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 9UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*28UL), (unsigned int8_t)temp+(rem >= 28U ? 1U : 0U))) #define div29(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 5UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*29UL), (unsigned int8_t)temp+(rem >= 29U ? 1U : 0U))) #define div30(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 5UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*30UL), (unsigned int8_t)temp+(rem >= 30U ? 1U : 0U))) #define div31(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 5UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*31UL), (unsigned int8_t)temp+(rem >= 31U ? 1U : 0U))) #define div32(x) (unsigned int8_t)((unsigned int16_t)(x) >> 5UL) #define div33(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 7UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*33UL), (unsigned int8_t)temp+(rem >= 33U ? 1U : 0U))) #define div34(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 7UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*34UL), (unsigned int8_t)temp+(rem >= 34U ? 1U : 0U))) #define div35(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 7UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*35UL), (unsigned int8_t)temp+(rem >= 35U ? 1U : 0U))) #define div36(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 7UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*36UL), (unsigned int8_t)temp+(rem >= 36U ? 1U : 0U))) #define div37(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*37UL), (unsigned int8_t)temp+(rem >= 37U ? 1U : 0U))) #define div38(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*38UL), (unsigned int8_t)temp+(rem >= 38U ? 1U : 0U))) #define div39(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*39UL), (unsigned int8_t)temp+(rem >= 39U ? 1U : 0U))) #define div40(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*40UL), (unsigned int8_t)temp+(rem >= 40U ? 1U : 0U))) #define div41(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*41UL), (unsigned int8_t)temp+(rem >= 41U ? 1U : 0U))) #define div42(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 6UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*42UL), (unsigned int8_t)temp+(rem >= 42U ? 1U : 0U))) #define div43(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*43UL), (unsigned int8_t)temp+(rem >= 43U ? 1U : 0U))) #define div44(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*44UL), (unsigned int8_t)temp+(rem >= 44U ? 1U : 0U))) #define div45(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*45UL), (unsigned int8_t)temp+(rem >= 45U ? 1U : 0U))) #define div46(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*46UL), (unsigned int8_t)temp+(rem >= 46U ? 1U : 0U))) #define div47(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*47UL), (unsigned int8_t)temp+(rem >= 47U ? 1U : 0U))) #define div48(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*48UL), (unsigned int8_t)temp+(rem >= 48U ? 1U : 0U))) #define div49(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*49UL), (unsigned int8_t)temp+(rem >= 49U ? 1U : 0U))) #define div50(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 5UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*50UL), (unsigned int8_t)temp+(rem >= 50U ? 1U : 0U))) #define div51(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 5UL) >> 8UL) #define div52(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*52UL), (unsigned int8_t)temp+(rem >= 52U ? 1U : 0U))) #define div53(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*53UL), (unsigned int8_t)temp+(rem >= 53U ? 1U : 0U))) #define div54(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*54UL), (unsigned int8_t)temp+(rem >= 54U ? 1U : 0U))) #define div55(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*55UL), (unsigned int8_t)temp+(rem >= 55U ? 1U : 0U))) #define div56(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*56UL), (unsigned int8_t)temp+(rem >= 56U ? 1U : 0U))) #define div57(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*57UL), (unsigned int8_t)temp+(rem >= 57U ? 1U : 0U))) #define div58(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*58UL), (unsigned int8_t)temp+(rem >= 58U ? 1U : 0U))) #define div59(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*59UL), (unsigned int8_t)temp+(rem >= 59U ? 1U : 0U))) #define div60(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*60UL), (unsigned int8_t)temp+(rem >= 60U ? 1U : 0U))) #define div61(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*61UL), (unsigned int8_t)temp+(rem >= 61U ? 1U : 0U))) #define div62(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*62UL), (unsigned int8_t)temp+(rem >= 62U ? 1U : 0U))) #define div63(x) (unsigned int8_t)((temp = (unsigned int16_t)((unsigned int16_t)(x) >> 6UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*63UL), (unsigned int8_t)temp+(rem >= 63U ? 1U : 0U))) #define div64(x) (unsigned int8_t)((unsigned int16_t)(x) >> 6UL) #define div65(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*65UL), (unsigned int8_t)temp+(rem >= 65U ? 1U : 0U))) #define div66(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*66UL), (unsigned int8_t)temp+(rem >= 66U ? 1U : 0U))) #define div67(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*67UL), (unsigned int8_t)temp+(rem >= 67U ? 1U : 0U))) #define div68(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*68UL), (unsigned int8_t)temp+(rem >= 68U ? 1U : 0U))) #define div69(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*69UL), (unsigned int8_t)temp+(rem >= 69U ? 1U : 0U))) #define div70(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*70UL), (unsigned int8_t)temp+(rem >= 70U ? 1U : 0U))) #define div71(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*71UL), (unsigned int8_t)temp+(rem >= 71U ? 1U : 0U))) #define div72(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*72UL), (unsigned int8_t)temp+(rem >= 72U ? 1U : 0U))) #define div73(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*73UL), (unsigned int8_t)temp+(rem >= 73U ? 1U : 0U))) #define div74(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*74UL), (unsigned int8_t)temp+(rem >= 74U ? 1U : 0U))) #define div75(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*75UL), (unsigned int8_t)temp+(rem >= 75U ? 1U : 0U))) #define div76(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*76UL), (unsigned int8_t)temp+(rem >= 76U ? 1U : 0U))) #define div77(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*77UL), (unsigned int8_t)temp+(rem >= 77U ? 1U : 0U))) #define div78(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*78UL), (unsigned int8_t)temp+(rem >= 78U ? 1U : 0U))) #define div79(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*79UL), (unsigned int8_t)temp+(rem >= 79U ? 1U : 0U))) #define div80(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*80UL), (unsigned int8_t)temp+(rem >= 80U ? 1U : 0U))) #define div81(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*81UL), (unsigned int8_t)temp+(rem >= 81U ? 1U : 0U))) #define div82(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*82UL), (unsigned int8_t)temp+(rem >= 82U ? 1U : 0U))) #define div83(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*83UL), (unsigned int8_t)temp+(rem >= 83U ? 1U : 0U))) #define div84(x) (unsigned int8_t)((temp = (unsigned int16_t)(((unsigned int16_t)(x) * 3UL) >> 8UL), rem = (unsigned int8_t)((unsigned int16_t)(x) - temp*84UL), (unsigned int8_t)temp+(rem >= 84U ? 1U : 0U))) #define div85(x) (unsigned int8_t)((((unsigned int16_t)(x) + 1UL) * 3UL) >> 8UL) #define div86(x) ((unsigned int8_t)(x < 86U ? 0U : (x < 172U ? 1U : 2U))) #define div87(x) ((unsigned int8_t)(x < 87U ? 0U : (x < 174U ? 1U : 2U))) #define div88(x) ((unsigned int8_t)(x < 88U ? 0U : (x < 176U ? 1U : 2U))) #define div89(x) ((unsigned int8_t)(x < 89U ? 0U : (x < 178U ? 1U : 2U))) #define div90(x) ((unsigned int8_t)(x < 90U ? 0U : (x < 180U ? 1U : 2U))) #define div91(x) ((unsigned int8_t)(x < 91U ? 0U : (x < 182U ? 1U : 2U))) #define div92(x) ((unsigned int8_t)(x < 92U ? 0U : (x < 184U ? 1U : 2U))) #define div93(x) ((unsigned int8_t)(x < 93U ? 0U : (x < 186U ? 1U : 2U))) #define div94(x) ((unsigned int8_t)(x < 94U ? 0U : (x < 188U ? 1U : 2U))) #define div95(x) ((unsigned int8_t)(x < 95U ? 0U : (x < 190U ? 1U : 2U))) #define div96(x) ((unsigned int8_t)(x < 96U ? 0U : (x < 192U ? 1U : 2U))) #define div97(x) ((unsigned int8_t)(x < 97U ? 0U : (x < 194U ? 1U : 2U))) #define div98(x) ((unsigned int8_t)(x < 98U ? 0U : (x < 196U ? 1U : 2U))) #define div99(x) ((unsigned int8_t)(x < 99U ? 0U : (x < 198U ? 1U : 2U))) #define div100(x) ((unsigned int8_t)(x < 100U ? 0U : (x < 200U ? 1U : 2U))) #define div101(x) ((unsigned int8_t)(x < 101U ? 0U : (x < 202U ? 1U : 2U))) #define div102(x) ((unsigned int8_t)(x < 102U ? 0U : (x < 204U ? 1U : 2U))) #define div103(x) ((unsigned int8_t)(x < 103U ? 0U : (x < 206U ? 1U : 2U))) #define div104(x) ((unsigned int8_t)(x < 104U ? 0U : (x < 208U ? 1U : 2U))) #define div105(x) ((unsigned int8_t)(x < 105U ? 0U : (x < 210U ? 1U : 2U))) #define div106(x) ((unsigned int8_t)(x < 106U ? 0U : (x < 212U ? 1U : 2U))) #define div107(x) ((unsigned int8_t)(x < 107U ? 0U : (x < 214U ? 1U : 2U))) #define div108(x) ((unsigned int8_t)(x < 108U ? 0U : (x < 216U ? 1U : 2U))) #define div109(x) ((unsigned int8_t)(x < 109U ? 0U : (x < 218U ? 1U : 2U))) #define div110(x) ((unsigned int8_t)(x < 110U ? 0U : (x < 220U ? 1U : 2U))) #define div111(x) ((unsigned int8_t)(x < 111U ? 0U : (x < 222U ? 1U : 2U))) #define div112(x) ((unsigned int8_t)(x < 112U ? 0U : (x < 224U ? 1U : 2U))) #define div113(x) ((unsigned int8_t)(x < 113U ? 0U : (x < 226U ? 1U : 2U))) #define div114(x) ((unsigned int8_t)(x < 114U ? 0U : (x < 228U ? 1U : 2U))) #define div115(x) ((unsigned int8_t)(x < 115U ? 0U : (x < 230U ? 1U : 2U))) #define div116(x) ((unsigned int8_t)(x < 116U ? 0U : (x < 232U ? 1U : 2U))) #define div117(x) ((unsigned int8_t)(x < 117U ? 0U : (x < 234U ? 1U : 2U))) #define div118(x) ((unsigned int8_t)(x < 118U ? 0U : (x < 236U ? 1U : 2U))) #define div119(x) ((unsigned int8_t)(x < 119U ? 0U : (x < 238U ? 1U : 2U))) #define div120(x) ((unsigned int8_t)(x < 120U ? 0U : (x < 240U ? 1U : 2U))) #define div121(x) ((unsigned int8_t)(x < 121U ? 0U : (x < 242U ? 1U : 2U))) #define div122(x) ((unsigned int8_t)(x < 122U ? 0U : (x < 244U ? 1U : 2U))) #define div123(x) ((unsigned int8_t)(x < 123U ? 0U : (x < 246U ? 1U : 2U))) #define div124(x) ((unsigned int8_t)(x < 124U ? 0U : (x < 248U ? 1U : 2U))) #define div125(x) ((unsigned int8_t)(x < 125U ? 0U : (x < 250U ? 1U : 2U))) #define div126(x) ((unsigned int8_t)(x < 126U ? 0U : (x < 252U ? 1U : 2U))) #define div127(x) ((unsigned int8_t)(x < 127U ? 0U : (x < 254U ? 1U : 2U))) // rather contorted macros are in the hope of being able to compile both on X86 linux and gcc6809 with 8 bit ints. // there's a problem when bits==32 and unsigned int32_t is defined as unsigned long long on linux. // It's OK on the vectrex where that type is 32 bits. The problem comes from a check to see if a // shift will cause overflow. If the type is 32 bits, you shift left then shift right by the same // amount and see if the end value is what you started with. However if the type is larger than // 32 bits, the value is preserved and you need to do an 'and' test instead to detect the shift // going farther left than bit32. /* this complex expression is the same as "(1<<bits)-1" except that 1<<bits would overflow */ //#define MAX_UNSIGNED(type, bits) ((type)((((1ULL<<((unsigned int32_t)bits-1ULL))-1ULL) | (1ULL<<((unsigned int32_t)bits-1ULL))))) // (warning: brittle code. changing 1ULL above to 1 causes compile-time overflow...) //#define MAX_SIGNED(type, bits) ((type)((1LL<<((int32_t)bits-1LL))-1LL)) // MIN_UNSIGNED is always 00 //#define MIN_SIGNED(type, bits) ((type)((-MAX_SIGNED(type, bits)-1LL))) extern unsigned int32_t div_core32(unsigned int32_t numerator, unsigned int32_t denominator, int8_t max_shift); extern unsigned int16_t div_core16(unsigned int16_t numerator, unsigned int16_t denominator, int8_t max_shift); static inline unsigned int32_t udiv32_by_32(unsigned int32_t numerator, unsigned int32_t denominator) { if (denominator == 0) return 0xFFFFFFFFUL; // MAX_UNSIGNED(unsigned int32_t, 32); // remainder = 0; return div_core32(numerator,denominator,32); // remainder = numerator; } static inline unsigned int32_t udiv32_by_16(unsigned int32_t numerator, unsigned int16_t denominator) { if (denominator == 0) return 0xFFFFFFFFUL; // MAX_UNSIGNED(unsigned int32_t, 32); // remainder = 0; return div_core32(numerator,denominator,32); // remainder = numerator; } static inline unsigned int32_t udiv32_by_8(unsigned int32_t numerator, unsigned int8_t denominator) { if (denominator == 0) return 0xFFFFFFFFUL; //MAX_UNSIGNED(unsigned int32_t, 32); // remainder = 0; return div_core32(numerator,denominator,32); // remainder = numerator; } static inline unsigned int16_t udiv16_by_16(unsigned int16_t numerator, unsigned int16_t denominator) { if (denominator == 0) return (0xFFFFU);// MAX_UNSIGNED(unsigned int16_t, 16); // remainder = 0; return (unsigned int16_t)div_core16(numerator,denominator,16); // remainder = numerator; } static inline unsigned int16_t udiv16_by_8(unsigned int16_t numerator, unsigned int8_t denominator) { if (denominator == 0) return 0xFFFU; // MAX_UNSIGNED(unsigned int16_t, 16); // remainder = 0; return (unsigned int16_t)div_core16(numerator,denominator,16); // remainder = numerator; } /* function to divide arbitrary unsigned 8 bit numerator by unsigned 8 bit divisor */ extern unsigned int8_t udiv8_by_8(unsigned int8_t num, unsigned int8_t denom); extern int32_t sdiv32_by_32(int32_t numerator, int32_t denominator); extern int32_t sdiv32_by_16(int32_t numerator, int16_t denominator); extern int32_t sdiv32_by_8(int32_t numerator, int8_t denominator); extern int16_t sdiv16_by_16(int16_t numerator, int16_t denominator); extern int16_t sdiv16_by_8(int16_t numerator, int8_t denominator); extern int8_t sdiv8_by_8(int8_t numerator, int8_t denominator); // ===================================================================================== // MULTIPLYING // ===================================================================================== #define umul8(x,y) ((x)*(y)) // This first implementation is based on: // https://stackoverflow.com/questions/22845801/32-bit-signed-integer-multiplication-without-using-64-bit-data-type/22847373#22847373 // with some minimal tweaking to supply two different word sizes... /* compute the full 32-bit product of two signed 16-bit integers */ extern unsigned int32_t umultiply16 (unsigned int16_t a, unsigned int16_t b); extern int32_t multiply16 (int16_t a, int16_t b); // The next implementation is based on: // https://www.techiedelight.com/multiply-16-bit-integers-using-8-bit-multiplier/ // although it required a lot more hacking to fit our use-case... extern unsigned int32_t umultiply16bit(unsigned int16_t m, unsigned int16_t n); extern int32_t multiply16bit(int16_t m, int16_t n); // ----------- // ===================================================================================== // EXTRACT MSB // ===================================================================================== static inline unsigned int8_t msb8(register unsigned int8_t x) { x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); return(x & ~(x >> 1)); } static inline unsigned int16_t msb16(register unsigned int16_t x) { x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); return(x & ~(x >> 1)); } static inline unsigned int32_t msb32(register unsigned int32_t x) { x |= (x >> 1); x |= (x >> 2); x |= (x >> 4); x |= (x >> 8); x |= (x >> 16); return(x & ~(x >> 1)); }