Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate populationCount const at compile time #7583

Merged
merged 1 commit into from
Apr 16, 2018

Conversation

GULPF
Copy link
Member

@GULPF GULPF commented Apr 12, 2018

As requested in #7558

@cooldome
Copy link
Member

Looks great, but isn’t it easier to use popcount function from bitops module

@GULPF
Copy link
Member Author

GULPF commented Apr 12, 2018

Great idea, I didn't know that module existed. However, it doesn't seem to work for int8:

import bitops
echo popcount(0b11111111'i8) # 32

@Araq
Copy link
Member

Araq commented Apr 12, 2018

import bitops
echo popcount(cast[int](0b11111111'i8)) # force zero extend

@GULPF
Copy link
Member Author

GULPF commented Apr 12, 2018

@Araq Still doesn't work. Doesn't look like cast[int](...) does zero extend:

import bitops
echo popcount(cast[int](0b11111111'i8)) # 64
doAssert cast[int32](0b11111111'i8) == 0b11111111111111111111111111111111'i32

@Araq
Copy link
Member

Araq commented Apr 12, 2018

It has to, that's what the spec says. :-/

@cooldome
Copy link
Member

cooldome commented Apr 12, 2018

This seems to work:

import bitops
const populationCount = block:
  var arr: array[low(int8)..high(int8), int8]
  for it in low(int8)..high(int8):
    arr[it] = popcount(cast[uint8](it)).int8
  arr

I'll submit echo popcount(0b11111111'i8) # 32 as bug report

@Araq Araq merged commit c4d7cc3 into nim-lang:devel Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants