Messages in this thread | | | Date | Wed, 25 Apr 2012 15:29:36 +0100 | Subject | Re: [PATCH 1/2] math128: Introduce {mult,add,cmp}_u128 | From | Jay Foad <> |
| |
> +#ifndef shl_u128 > +static inline u128 shl_u128(u128 x, unsigned int n) > +{ > + u128 res; > + > + if (n < 64) { > + res.hi = x.hi << n; > + res.hi |= x.lo >> (64 - n);
This is undefined when n == 0.
> + res.lo = x.lo << n; > + } else { > + res.lo = 0; > + res.hi = x.lo << (n - 64); > + } > + > + return res; > +} > +#endif /* shl_u128 */ > + > +#ifndef shr_u128 > +static inline u128 shr_u128(u128 x, unsigned int n) > +{ > + u128 res; > + > + if (n < 64) { > + res.lo = x.lo >> n; > + res.lo |= x.hi << (64 - n);
Likewise.
> + res.hi = x.hi >> n; > + } else { > + res.hi = 0; > + res.lo = x.hi >> (n - 64); > + } > + > + return res; > +} > +#endif /* shr_u128 */
Jay.
| |