timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

misc intrinsics, builtins #537

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

here's how to find correct declaration for intrinsics/builtins

https://github.com/llvm-mirror/clang/blob/master/include/clang/Basic/Builtins.def

BUILTIN(__builtin_rotateright64, "UWiUWiUWi", "nc")

see definitions on top for meaning of UWiUWiUWi

here's how to check for errors in type declarations: -Wconversion

clang -o /tmp/z01 -Wconversion $timn_D/tests/nim/all/t11720.c /Users/timothee/git_clone//nim//timn//tests/nim/all/t11720.c:14:57: warning: implicit conversion changes signedness: 'unsigned long long' to 'long long' [-Wsign-conversion] unsigned long long x3 = __builtin_rotateright64(x1, x2);


```c
#include <stdio.h>
int main (int argc, char *argv[]) {
  unsigned long long x1 = 12;
  {
    long long x2 = 3;
    unsigned long long x3 = __builtin_rotateright64(x1, x2);
  }
  {
    unsigned long long x2 = 3;
    unsigned long long x3 = __builtin_rotateright64(x1, x2);
  }
  return 0;
}

intrinsic guide

https://software.intel.com/sites/landingpage/IntrinsicsGuide/

links

https://gitlab.itwm.fraunhofer.de/hellerm/spu-llvm-src/-/blob/af074a2a3d8912f22b1e14a1464226af8ee9bcf4/clang/docs/LanguageExtensions.rst Notifications TITLE: add a builtin function for every llvm C library intrinsic and bit manipulation instrinsics · Issue #767 · ziglang/zig Refactoring bitops.rotateLeftBits() and bitops.rotateRightBits(); adding builtins and intrinsics. by rockcavera · Pull Request #16622 · nim-lang/Nim https://llvm.org/docs/LangRef.html#standard-c-c-library-intrinsics C++ Using Intrinsics in a Cross-platform Manner - Software Engineering Stack Exchange TITLE: c - Testing for builtins/intrinsics - Stack Overflow Understanding GCC Builtins to Develop Better Tools

timotheecour commented 3 years ago

explanation for type mismatch:

answer: see https://github.com/nim-lang/Nim/pull/16622#discussion_r560465795

details:

refs https://github.com/nim-lang/Nim/pull/16622

conflicting types, eg:

$git_clone_D/llvm-project/clang/test/CodeGen/builtin-rotate.c

unsigned long long rotl64(unsigned long long x, long long y) {
// CHECK-LABEL: rotl64
// CHECK: [[F:%.*]] = call i64 @llvm.fshl.i64(i64 [[X:%.*]], i64 [[X]], i64 [[Y:%.*]])
// CHECK-NEXT: ret i64 [[F]]

  return __builtin_rotateleft64(x, y);
}

...
long long rotr64(long long x, unsigned long long y) {
// CHECK-LABEL: rotr64
// CHECK: [[F:%.*]] = call i64 @llvm.fshr.i64(i64 [[X:%.*]], i64 [[X]], i64 [[Y:%.*]])
// CHECK-NEXT: ret i64 [[F]]

  return __builtin_rotateright64(x, y);
}

/Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include/ia32intrin.h

#ifdef __x86_64__
static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__rolq(unsigned long long __X, int __C) {
  return __builtin_rotateleft64(__X, __C);
}

static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
__rorq(unsigned long long __X, int __C) {
  return __builtin_rotateright64(__X, __C);
}
#endif /* __x86_64__ */

furthermore, I got this after changing your code:

/Users/timothee/.cache/nim/bitops_d/runnableExamples/@mbitops_examples36.nim.c:52:31: note: '__builtin_rotateleft64' is a builtin with type
      'unsigned long long (unsigned long long, unsigned long long)'
so __builtin_rotateleft64 uses 'unsigned long long (unsigned long long, unsigned long long)'
   __builtin_rotateright64' uses 'unsigned long long (unsigned long long, long long)'

What C compiler is used by the OSX CI? If it's not Clang than this is a matter of concern

https://pipelines.actions.githubusercontent.com/ZRinn1OrR0LWxU3iWy1StaQcZRN2kXW9lHWwDJ3esfasrmfdRn/_apis/pipelines/1/runs/12847/signedlogcontent/5?urlExpires=2021-01-19T18%3A24%3A15.0758912Z&urlSigningMethod=HMACV1&urlSignature=3MnjmPZgT%2BI2gb2f5pCPNOzi6%2FWw99MIvEI1%2F6JEkbU%3D

bin/nim doc --errormax:3 --hint:Conf:off --hint:Path:off --hint:Processing:off --hint:XDeclaredButNotUsed:off --warning:UnusedImport:off -d:boot --putenv:nimversion=1.5.1 --git.commit:devel --git.url:https://github.com/nim-lang/Nim --outdir:web/upload/1.5.1 --index:on lib/pure/httpcore.nim

https://dev.azure.com/nim-lang/255dfe86-e590-40bb-a8a2-3c0295ebdeb1/_apis/build/builds/12253/logs/93 2021-01-19T17:14:43.8960280Z runCI: 2021-01-19T17:14:43.8964510Z hostOS: macosx, hostCPU: amd64, int: 8, float: 8, cpuEndian: littleEndian, cwd: /Users/runner/work/1/s

https://github.com/actions/virtual-environments/blob/macOS-10.15/20201212.1/images/macos/macos-10.15-Readme.md

timotheecour commented 3 years ago

ifunc resolver

https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-ifunc-function-attribute

ifunc ("resolver")

allows the resolution of the symbol value to be determined dynamically at load time, and an optimized version of the routine to be selected for the particular processor or other system characteristics determined then