[GHC] #9340: Implement new `clz` inline primop

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: Type: feature request | Status: new Priority: normal | Milestone: 7.10.1 Component: Compiler | Version: 7.8.2 (CodeGen) | Differential Revisions: Keywords: primop clz | Architecture: Operating System: Unknown/Multiple | Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------- From a draft for #9281, I had to use a C FFI and using a gcc/clang `__builtin` to get access to the [http://en.wikipedia.org/wiki/Find_first_set CLZ instruction]: {{{#!hs -- | Compute base-2 log of 'Word#' -- -- This is internally implemented as count-leading-zeros machine instruction. foreign import ccall unsafe "integer_gmp_word_log2" wordLog2# :: Word# -> Int# }}} {{{#!c HsWord integer_gmp_word_log2(HsWord x) { #if (SIZEOF_HSWORD) == (SIZEOF_INT) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clz(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzl(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzll(x) : -1; #else # error unsupported SIZEOF_HSWORD #endif } }}} Since a `clz`-like operation should be available on most cpus GHC should expose that as a primop ({{{clz# :: Word# -> Int#}}}). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: Type: feature | Status: new request | Milestone: 7.10.1 Priority: normal | Version: 7.8.2 Component: Compiler | Keywords: primop clz (CodeGen) | Operating System: Unknown/Multiple Resolution: | Type of failure: None/Unknown Differential Revisions: | Test Case: Architecture: | Blocking: Unknown/Multiple | Difficulty: Unknown | Blocked By: | Related Tickets: | -------------------------------------+------------------------------------- Old description:
From a draft for #9281, I had to use a C FFI and using a gcc/clang `__builtin` to get access to the [http://en.wikipedia.org/wiki/Find_first_set CLZ instruction]:
{{{#!hs -- | Compute base-2 log of 'Word#' -- -- This is internally implemented as count-leading-zeros machine instruction. foreign import ccall unsafe "integer_gmp_word_log2" wordLog2# :: Word# -> Int# }}}
{{{#!c HsWord integer_gmp_word_log2(HsWord x) { #if (SIZEOF_HSWORD) == (SIZEOF_INT) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clz(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzl(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzll(x) : -1; #else # error unsupported SIZEOF_HSWORD #endif } }}}
Since a `clz`-like operation should be available on most cpus GHC should expose that as a primop ({{{clz# :: Word# -> Int#}}}).
New description: From a draft for #9281, I had to use a C FFI and using a gcc/clang `__builtin` to get access to the [http://en.wikipedia.org/wiki/Find_first_set CLZ instruction]: {{{#!hs -- | Compute base-2 log of 'Word#' -- -- This is internally implemented as count-leading-zeros machine instruction. foreign import ccall unsafe "integer_gmp_word_log2" wordLog2# :: Word# -> Int# }}} {{{#!c HsWord integer_gmp_word_log2(HsWord x) { #if (SIZEOF_HSWORD) == (SIZEOF_INT) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clz(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzl(x) : -1; #elif (SIZEOF_HSWORD) == (SIZEOF_LONG_LONG) return x ? (WORD_SIZE_IN_BITS-1) - __builtin_clzll(x) : -1; #else # error unsupported SIZEOF_HSWORD #endif } }}} Since a `clz`-like operation should be available on most cpus GHC should expose that as a primop ({{{clz# :: Word# -> Int#}}}). -- Comment (by hvr): (remove hidden Unicode characters in code blocks) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: hvr Type: feature | Status: new request | Milestone: 7.10.1 Priority: normal | Version: 7.8.2 Component: Compiler | Keywords: primop clz (CodeGen) | Architecture: Unknown/Multiple Resolution: | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by hvr): * owner: => hvr Comment: I'm now actively working on this one. And I've already got semi-working code that I'll submit to Phab soon (some pre-requisite code is already submitted as Phab:D141) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: hvr Type: feature | Status: patch request | Milestone: 7.10.1 Priority: normal | Version: 7.8.2 Component: Compiler | Keywords: primop clz (CodeGen) | Architecture: Unknown/Multiple Resolution: | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: Phab:D144 | -------------------------------------+------------------------------------- Changes (by hvr): * status: new => patch * differential: => Phab:D144 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: patch
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets:
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: patch
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets:
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: patch
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets:
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: hvr Type: feature | Status: closed request | Milestone: 7.10.1 Priority: normal | Version: 7.8.2 Component: Compiler | Keywords: primop clz ctz (CodeGen) | Architecture: Unknown/Multiple Resolution: fixed | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: Phab:D144 | -------------------------------------+------------------------------------- Changes (by hvr): * keywords: primop clz => primop clz ctz * status: patch => closed * resolution: => fixed Comment: I consider this ticket resolved. The next step will be to expose those primops via `Data.Bits` (see Phab:D158 for a draft-patch which still needs to go through the library proposal process) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: closed
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz ctz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: fixed | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets:
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel

#9340: Implement new `clz` inline primop -------------------------------------+------------------------------------- Reporter: hvr | Owner: hvr Type: feature | Status: closed request | Milestone: 7.10.1 Priority: normal | Version: 7.8.2 Component: Compiler | Keywords: primop clz ctz (CodeGen) | Architecture: Unknown/Multiple Resolution: fixed | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: #9532 Type of failure: | None/Unknown | Test Case: | Blocking: | Differential Revisions: Phab:D144 | -------------------------------------+------------------------------------- Changes (by hvr): * related: => #9532 Comment: See #9532 for the related `Data.Bits` CLZ/CTZ interface ticket -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9340#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: closed
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz ctz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: fixed | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets: #9532
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel

#9340: Implement new `clz` inline primop
-------------------------------------+-------------------------------------
Reporter: hvr | Owner: hvr
Type: feature | Status: closed
request | Milestone: 7.10.1
Priority: normal | Version: 7.8.2
Component: Compiler | Keywords: primop clz ctz
(CodeGen) | Architecture: Unknown/Multiple
Resolution: fixed | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets: #9532
Type of failure: |
None/Unknown |
Test Case: |
Blocking: |
Differential Revisions: Phab:D144 |
-------------------------------------+-------------------------------------
Comment (by Herbert Valerio Riedel
participants (1)
-
GHC