Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
GHC
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
4,320
Issues
4,320
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
360
Merge Requests
360
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Glasgow Haskell Compiler
GHC
Commits
24864ba5
Commit
24864ba5
authored
Apr 23, 2016
by
Simon Marlow
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use __builtin_clz() to implement log_2()
A microoptimisation in the block allocator.
parent
d3969962
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
11 deletions
+21
-11
rts/sm/BlockAlloc.c
rts/sm/BlockAlloc.c
+21
-11
No files found.
rts/sm/BlockAlloc.c
View file @
24864ba5
...
...
@@ -199,31 +199,41 @@ initGroup(bdescr *head)
}
}
// There are quicker non-loopy ways to do log_2, but we expect n to be
// usually small, and MAX_FREE_LIST is also small, so the loop version
// might well be the best choice here.
// log base 2 (floor), needs to support up to 2^MAX_FREE_LIST
STATIC_INLINE
nat
log_2
_ceil
(
W_
n
)
log_2
(
W_
n
)
{
#if defined(__GNUC__)
return
__builtin_clzl
(
n
)
^
(
sizeof
(
StgWord
)
*
8
-
1
);
// generates good code on x86. __builtin_clz() compiles to bsr+xor, but
// we want just bsr, so the xor here cancels out gcc's xor.
#else
W_
i
,
x
;
x
=
1
;
x
=
n
;
for
(
i
=
0
;
i
<
MAX_FREE_LIST
;
i
++
)
{
if
(
x
>=
n
)
return
i
;
x
=
x
<<
1
;
x
=
x
>>
1
;
if
(
x
==
0
)
return
i
;
}
return
MAX_FREE_LIST
;
#endif
}
// log base 2 (ceiling), needs to support up to 2^MAX_FREE_LIST
STATIC_INLINE
nat
log_2
(
W_
n
)
log_2
_ceil
(
W_
n
)
{
#if defined(__GNUC__)
nat
r
=
log_2
(
n
);
return
(
n
&
(
n
-
1
))
?
r
+
1
:
r
;
#else
W_
i
,
x
;
x
=
n
;
x
=
1
;
for
(
i
=
0
;
i
<
MAX_FREE_LIST
;
i
++
)
{
x
=
x
>>
1
;
if
(
x
==
0
)
return
i
;
if
(
x
>=
n
)
return
i
;
x
=
x
<<
1
;
}
return
MAX_FREE_LIST
;
#endif
}
STATIC_INLINE
void
...
...
Simon Peyton Jones
@simonpj
mentioned in commit
546f24e4
·
Apr 28, 2016
mentioned in commit
546f24e4
mentioned in commit 546f24e4f8a7c086b1e5afcdda624176610cbcf8
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment