Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Sylvia Else <sylvia@not.at.this.address> |
|---|---|
| Newsgroups | comp.misc |
| Subject | Re: The 8-Byte Two-Step |
| Date | 2014-09-21 13:42 +1000 |
| Message-ID | <c86voiFgprmU1@mid.individual.net> (permalink) |
| References | <01T8OImfoeUtayhTm1xJJ0vS@dont-email.me> |
On 21/09/2014 6:41 AM, Rich wrote: > http://zinascii.com/2014/the-8-byte-two-step.html > > While studying the Illumos queue implementation I came across a line of > code that left me quite perplexed. > > #define MQ_ALIGNSIZE 8 /* 64-bit alignment */ ... temp = (mqhp->mq_maxsz > + MQ_ALIGNSIZE - 1) & ~(MQ_ALIGNSIZE - 1); > > I knew it had something to do with alignment on a 64-bit boundary, but > just how the hell was it doing it? Why is it subtracting one from the > alignment size? Why is it negating the expression on the right? Why is > it adding to the max message size? What the hell is going on? > > At first I attempted to derive the pattern in my head but kept losing my > place; forced to start over multiple times. After placing my pride on > the bookshelf next to me, I wrote some C to help decode the mystery. The > program breaks the expression into its three parts, printing the base-2 > representation of each step. Here are some test runs with different > sizes: > > ... > The thinking behind the caculation is just this: Add an amount that is guaranteed to take you to or beyond the next multiple of the alignment size, and then throw away the remainder by subtracting the result modulo the alignment size. In this case, since the alignment size is a power of two, the latter step can be achieved using a mask rather than the much more expensive division. Sylvia
Back to comp.misc | Previous | Next — Previous in thread | Find similar | Unroll thread
The 8-Byte Two-Step Rich <rich@example.invalid> - 2014-09-20 20:41 +0000
Re: The 8-Byte Two-Step Bob Eager <news0002@eager.cx> - 2014-09-20 21:14 +0000
Re: The 8-Byte Two-Step Marko Rauhamaa <marko@pacujo.net> - 2014-09-21 00:48 +0300
Re: The 8-Byte Two-Step Bob Eager <news0002@eager.cx> - 2014-09-21 08:27 +0000
Re: The 8-Byte Two-Step Marko Rauhamaa <marko@pacujo.net> - 2014-09-21 14:14 +0300
Re: The 8-Byte Two-Step Marko Rauhamaa <marko@pacujo.net> - 2014-09-23 11:19 +0300
Re: The 8-Byte Two-Step Christian Neukirchen <chneukirchen@gmail.com> - 2014-09-23 18:26 +0200
Re: The 8-Byte Two-Step mm0fmf <none@mailinator.com> - 2014-09-21 20:45 +0100
Re: The 8-Byte Two-Step Sylvia Else <sylvia@not.at.this.address> - 2014-09-21 13:42 +1000
csiph-web