c - Most efficient method of adding 16-bit numbers, lookup table, binary addition or simple addition? -
i attempting code add function msp-430 emulator attempting build.
i wondering if lookup table addition of 2 16-bit numbers viable solution efficient retrieval of results. alternative methods have thought of implementing binary addition loop, adds bits 1 one or simple decimal addition. need able determine sign , carry (if there one).
any specs on micro-controller can found here.
you can use simple 32-bit, two's-complement arithmetic. assuming src , dst stored in lower 16-bits of uint32_t, following code sequence emulates add instruction:
uint32_t xor = src ^ dst; dst += src; flags.n = (dst >> 15) & 1; flags.c = (dst >> 16) & 1; flags.v = ((~xor & (src ^ dst)) >> 15) & 1; dst &= 0xffff; flags.z = (dst == 0); the result of addition can obtained taking lower 16 bits of 32-bit addition. n, z, , c flags easy compute. (signed) overflow flag v more tricky. code tests whether signs of src , dst equal before addition , different afterwards.
Comments
Post a Comment