… | |
… | |
71 | 0..n-1, then you could use this inline function in a header file: |
71 | 0..n-1, then you could use this inline function in a header file: |
72 | |
72 | |
73 | ecb_inline uint32_t |
73 | ecb_inline uint32_t |
74 | rndm (uint32_t n) |
74 | rndm (uint32_t n) |
75 | { |
75 | { |
76 | return n * (uint32_t)rndm16 ()) >> 16; |
76 | return (n * (uint32_t)rndm16 ()) >> 16; |
77 | } |
77 | } |
78 | |
78 | |
79 | However, for powers of two, you could use a normal mask, but that is only |
79 | However, for powers of two, you could use a normal mask, but that is only |
80 | worth it if, at compile time, you can detect this case. This is the case |
80 | worth it if, at compile time, you can detect this case. This is the case |
81 | when the passed number is a constant and also a power of two (C<n & (n - |
81 | when the passed number is a constant and also a power of two (C<n & (n - |
… | |
… | |
84 | ecb_inline uint32_t |
84 | ecb_inline uint32_t |
85 | rndm (uint32_t n) |
85 | rndm (uint32_t n) |
86 | { |
86 | { |
87 | return is_constant (n) && !(n & (n - 1)) |
87 | return is_constant (n) && !(n & (n - 1)) |
88 | ? rndm16 () & (num - 1) |
88 | ? rndm16 () & (num - 1) |
89 | : (uint32_t)rndm16 ()) >> 16; |
89 | : (n * (uint32_t)rndm16 ()) >> 16; |
90 | } |
90 | } |
91 | |
91 | |
92 | =item bool ecb_expect(expr,value) |
92 | =item bool ecb_expect(expr,value) |
93 | |
93 | |
94 | =item bool ecb_unlikely(bool) |
94 | =item bool ecb_unlikely(bool) |