Skip to content

Instantly share code, notes, and snippets.

@boydgreenfield
Last active August 29, 2015 14:01
Show Gist options
  • Save boydgreenfield/7fbd6d84747711420977 to your computer and use it in GitHub Desktop.
Save boydgreenfield/7fbd6d84747711420977 to your computer and use it in GitHub Desktop.
{.compile: "popcount.c".}
{.passC: "-Wall -Werror".}
{.passL: "-v".}
proc popcount(x: uint64): uint64 {.importc: "count_1bits".}
when isMainModule:
echo "Testing..."
echo popcount(1)
# Ubuntu / gcc
gcc -o /vagrant/count1s /vagrant/nimcache/stdlib_system.o /vagrant/nimcache/count1s.o /vagrant/nimcache/popcount.o -ldl -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' '/vagrant/count1s' '-v' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o /vagrant/count1s /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /vagrant/nimcache/stdlib_system.o /vagrant/nimcache/count1s.o /vagrant/nimcache/popcount.o -ldl -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o
/vagrant/nimcache/count1s.o: In function `count1sInit':
count1s.c:(.text+0x15b): undefined reference to `count_1bits'
collect2: error: ld returned 1 exit status
Error: execution of an external program failed
# OS X / clang
clang -c -w -Wall -Werror -I/Users/boyd/Applications/Nimrod/lib -o /Users/boyd/Projects/temp2/nimcache/count1s.o /Users/boyd/Projects/temp2/nimcache/count1s.c
clang -o /Users/boyd/Projects/temp2/count1s /Users/boyd/Projects/temp2/nimcache/stdlib_system.o /Users/boyd/Projects/temp2/nimcache/count1s.o /Users/boyd/Projects/temp2/nimcache/popcount.o -ldl -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o /Users/boyd/Projects/temp2/count1s /Users/boyd/Projects/temp2/nimcache/stdlib_system.o /Users/boyd/Projects/temp2/nimcache/count1s.o /Users/boyd/Projects/temp2/nimcache/popcount.o -ldl -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"_count_1bits", referenced from:
_count1sInit in count1s.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of an external program failed
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "popcount.h"
static inline unsigned int count_1bits(uint64_t x)
{
return __builtin_popcountll(x);
// /* This portable implementation is the fastest one we know of for 64
// * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */
// const uint64_t h55 = UINT64_C(0x5555555555555555);
// const uint64_t h33 = UINT64_C(0x3333333333333333);
// const uint64_t h0F = UINT64_C(0x0F0F0F0F0F0F0F0F);
// const uint64_t h01 = UINT64_C(0x0101010101010101);
// x -= (x >> 1) & h55; /* Count of each 2 bits in-place. */
// x = (x & h33) + ((x >> 2) & h33); /* Count of each 4 bits in-place. */
// x = (x + (x >> 4)) & h0F; /* Count of each 8 bits in-place. */
// return (x * h01) >> 56; /* Sum of all bytes. */
}
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
static inline unsigned int count_1bits(uint64_t x);
@boydgreenfield
Copy link
Author

Note – the C code works and compiles fine on both machines – but I'm having a lot of trouble debugging the linking error. Passing the -m64 flag with .passL doesn't get me anything either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment