From: David Guillen Fandos <david@davidgf.es>
To: musl@lists.openwall.com
Subject: Re: Do not use 64 bit division if possible
Date: Sun, 26 Nov 2017 02:40:24 +0100 [thread overview]
Message-ID: <b495f8b2-c7fe-37d4-5611-0cc8b7664e0b@davidgf.es> (raw)
In-Reply-To: <20171126012311.GT1627@brightrain.aerifal.cx>
On 26/11/17 02:23, Rich Felker wrote:
> On Sun, Nov 26, 2017 at 02:12:58AM +0100, David Guillen Fandos wrote:
>>
>> On 26/11/17 01:59, Rich Felker wrote:
>>> On Sun, Nov 26, 2017 at 01:49:09AM +0100, David Guillen Fandos wrote:
>>>> Hey,
>>>>
>>>> Wow that's an awesome optimization (the a&-a), didn't know gcc was
>>>> smart enough to figure that out by itself :D
>>>
>>> It doesn't seem to be doing any optimizing for me. What it *should* do
>>> is optimize the div to ctz+shift.
>>>
>>> BTW please don't top-reply; it makes threads hard to follow and hard
>>> to meaningfully reply to inline.
>>>
>>> Rich
>>>
>>>
>>>> I just realized that PAGE_SIZE seems indeed to be defined to a
>>>> constant for some architectures, did not notice since I was running
>>>> on MIPS which has a page size different for each uarch.
>>>>
>>>> I'd say the (a&-a) is a very simple optimization and we should use
>>>> it, since it adds almost no complexity and sames some cycles and
>>>> some .text bytes, which is sometimes a bit tight.
>>>>
>>>> Something like this? Doesn't hurt constants, improves some arches :)
>>>>
>>>> diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c
>>>> index b8b761d0..aa9fc9d1 100644
>>>> --- a/src/conf/sysconf.c
>>>> +++ b/src/conf/sysconf.c
>>>> @@ -206,7 +206,7 @@ long sysconf(int name)
>>>> if (name==_SC_PHYS_PAGES) mem = si.totalram;
>>>> else mem = si.freeram + si.bufferram;
>>>> mem *= si.mem_unit;
>>>> - mem /= PAGE_SIZE;
>>>> + mem /= (unsigned)(PAGE_SIZE & -PAGE_SIZE);
>>>> return (mem > LONG_MAX) ? LONG_MAX : mem;
>>>> case JT_ZERO & 255:
>>>> return 0;
>>
>> Sorry for that, default settings you know :)
>>
>> Well the main reason is cause in MIPS it requires adding __divdi3
>> which is around 1KB of code, which hey, it's not much, but why would
>> we need it right? It makes a difference in embedded tools with
>> statically linked musl.
>>
>> Thanks for your interest!
>
> If this is a real problem you're hitting, I'm interested in helping,
> but it seems unlikely. If your program uses printf or other common
> functions it will already be pulling in __divdi3 I think.
>
> Rich
>
Not a real problem really, more than binary size. I'm not using printf
that's why I was chasing all the big-ish functions that seemed
unnecessary in my binary and I was curious about why sysconf actually
needed a 64 bit division on mips.
Also the (a&-a) doesnt seem to help, gcc is not that smart :) It seems
there's no easy way to hint it that a number is a power of two, I guess
that's why the kernel uses PAGE_SHIFT.
Given that page size is not constants in some arches it might be useful
to have page shift, since some operations would be faster maybe? Like
page aligning [ & ~(PAGE_SIZE-1) ]? Not sure if we care that much even
though we use that in malloc.
Thanks for the interest!
David
next prev parent reply other threads:[~2017-11-26 1:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-25 20:52 David Guillen Fandos
2017-11-25 23:15 ` Michael Clark
2017-11-25 23:46 ` David Guillen Fandos
2017-11-25 23:53 ` Rich Felker
2017-11-26 0:10 ` Michael Clark
2017-11-26 0:49 ` David Guillen Fandos
2017-11-26 0:59 ` Rich Felker
2017-11-26 1:12 ` David Guillen Fandos
2017-11-26 1:23 ` Rich Felker
2017-11-26 1:40 ` David Guillen Fandos [this message]
2017-11-26 0:49 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b495f8b2-c7fe-37d4-5611-0cc8b7664e0b@davidgf.es \
--to=david@davidgf.es \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).