April 10, 2007

Shift Registers and De Bruijn Sequences

I tell ya' - everything time you think you've got a novel idea, turns out somebody's been there already... doesn't even seem to matter how small it might be.

For example: I have this minor function I wrote ages ago, which I had recently rewritten/had to re-derive. Its purpose was fairly mundane - I wanted to compute the (integer) log base 2 for a power of 2 integer value. Pretty simple and esoteric problem, but it pops up fairly often in graphics/sound programming (especially 3d stuff).

There are lots of solutions, and its not really a significant performance bottleneck or anything, but I always thought my solution was rather novel. The code looks like this : (link)


The idea is pretty simple, but clever (er... if I do say so myself, I suppose :)).

Very (very) briefly: multiplying by a power of 2 is the same as left-shifting by that power (determining that power being the log we're looking for). Therefore, we can construct a bit pattern that uniquely encodes the possible patterns sequence 0 - 31, such that any 5 bit sequence is uniquely of that range. Then multiplying by a power of two number puts a unique sequence into the top 5 bits of the result, which we can then shift down and use a table to "reverse" into our result. We need the table, because of course the encoding isn't linear with respect to the domain.

There are a number of constants you could use to fulfill this criteria, and I always thought of it as a compression/huffman tree encoding problem. The current constant I computed is 0x04ad19df.

Turns out not only is this a well known idea, but, further, its actually a special class of space filling curves (amongst other things) know as De Bruijn sequences. See the great "Bit Twiddling Hacks" page by Sean Eron Anderson for more information. The constant they computed, which of course also uses a different table, is 0x077CB531.

Results are the same.

Ah, well. Time to move on, I think..... :)

7 comments:

Anonymous said...
This comment has been removed by a blog administrator.
runescape gold said...

runescape money runescape gold runescape gold runescape money buy runescape gold buy runescape money runescape money runescape gold wow power leveling wow powerleveling Warcraft Power Leveling Warcraft PowerLeveling buy runescape gold buy runescape money runescape itemsrunescape accounts runescape gp dofus kamas buy dofus kamas Guild Wars Gold buy Guild Wars Gold lotro gold buy lotro gold lotro gold buy lotro gold lotro gold buy lotro gold runescape money runescape power leveling runescape money runescape gold dofus kamas cheap runescape money cheap runescape gold Hellgate Palladium Hellgate London Palladium Hellgate money Tabula Rasa gold tabula rasa money Tabula Rasa Credit Tabula Rasa Credits Hellgate gold Hellgate London gold wow power leveling wow powerleveling Warcraft PowerLeveling Warcraft Power Leveling World of Warcraft PowerLeveling World of Warcraft Power Leveling runescape power leveling runescape powerleveling eve isk eve online isk eve isk eve online isk tibia gold Fiesta Silver Fiesta Gold




runescape money runescape gold wow power leveling


棕榈树
VI设计
画册设计
血管瘤
肝血管瘤
音乐剧
福卡
防静电地板
美国留学
留学美国
电阻器
不锈钢电阻器
频敏电阻器

睡眠呼吸机
伟康呼吸机
呼吸机
无创呼吸机
家用呼吸机
呼吸机的使用

北京消化系统疾病
北京心脑血管疾病
北京肾病
北京中医儿科
北京针灸减肥
针灸减肥
北京糖尿病
北京疼痛病
北京类风湿

aocgold said...

[url=http://buy-aoc-gold.rgtrcredit.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.aocsale.com/][color=#800080][b]Age Of Conan Gold [/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]Age Of Conan Gold[/b][/color][/url][url=http://www.buy-cheap-aoc-gold.com/][color=#800080][b]buy age of conan gold [/b][/color][/url]
[url=http://www.aocsale.com/][color=#800080][b]buy age of conan gold[/b][/color][/url][url=http://buy-aoc-gold.rgtrcredit.com/][color=#800080][b]buy age of conan gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b]aoc gold [/b][/color][/url][url=http://buy-aoc-gold.hellgate-pd.com/][color=#800080][b]aoc gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b]aoc gold[/b][/color][/url]
[url=http://buy-aoc-gold.hellgate-pd.com/][color=#800080][b] buy aoc gold [/b][/color][/url][url=http://www.buyfastgold.com/][color=#800080][b] buy aoc gold[/b][/color][/url]


http://www.buyfastgold.com

http://www.buy-cheap-aoc-gold.com

http://www.aocsale.com

http://buy-aoc-gold.hellgate-pd.com

http://buy-aoc-gold.rgtrcredit.com



Age Of Conan Gold     Age Of Conan Gold       Age Of Conan Gold     buy age of conan gold   buy age of conan gold   buy age of conan gold          aoc gold             aoc gold             aoc gold  buy aoc gold      buy aoc gold

new laptop battery said...

Thanks from specialize in laptop battery,laptop AC adapters. All our products are brand new, with the excellent service from our laptop battery of customer service team.
Thanks for your info. The most convenient and cheap replacement battery online shop in uk. We specialize in laptop batteries, laptop AC adapters.

All our laptop AC adapter are brand new, with the excellent service from our customer service team.

the most convenient and usa battery online shop in usa.
You can find some battery and adapter from here is very cool.

We specialize in laptop batteries,laptop AC adapters. All of batteries are brand new, with the excellent service from our customer service team, you can feel free to purchase on laptop battery!
Here is cheap laptop ac adapter online shop in uk. We specialize in batteries. All our au battery are brand new, with the excellent service from our customer service team.

note1 said...

合宿 免許
不動産
クレジットカード 現金化
toefl
アフィリエイト
幼児教室
個別指導塾
名刺

note1 said...

賃貸の部屋探しならジョイント・ルームピア!賃貸マンション・賃貸アパートなどの賃貸情報が満載。さらにペット可賃貸・デザイナーズマンションなど多数ご紹介をお探しならフロアコーティング

moto said...

不動産
合宿 免許
網頁設計
ショッピング枠 現金化
クレジットカード 現金化
キャッシング
治験
フロント 仕事
幼児教室
名刺