1. ordinary c language level #define avg2(a,b) ((a+b+1)>>1) #define avg4(a,b,c,d) ((a+b+c+d+2)>>2) 显而易见.注意a,b宏表达式可能引出的副作用
2.SIMD by software 实现方法1: inline static uint64_t BYTE_VEC(uint64_t x) { x |= x << 8; x |= x << 16; x |= x << 32; return x; } static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b) { return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1); } static inline uint64_t avg2(uint64_t a, uint64_t b) { return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1); }