linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Linus Torvalds' <torvalds@linux-foundation.org>
Cc: Jules Bashizi Irenge <jbi.octave@gmail.com>,
	"linux-perf-users@vger.kernel.org"
	<linux-perf-users@vger.kernel.org>
Subject: RE: [PATCH] perf/x86/amd/power: Use div64_u64 onstead of do_div()
Date: Sat, 4 May 2024 22:53:19 +0000	[thread overview]
Message-ID: <e909d487c4c3468f8f20c952fe46c89c@AcuMS.aculab.com> (raw)
In-Reply-To: <CAHk-=whCS468impcODViZuWxhQSqi4mqFCo3wbrsb=1SB0CXUg@mail.gmail.com>

From: Linus Torvalds
> Sent: 03 May 2024 00:00
> 
> On Thu, 2 May 2024 at 15:19, David Laight <David.Laight@aculab.com> wrote:
> >
> > Somewhen it must have got changed from a simple asm wrapper for divl.
> 
> It's never been a simple wrapper for divl. For the simple reason that
> 'divl' doesn't do a full 64/32 divide returning a full 64-bit result.

I'd thought (before I'd looked) that it only returned a 32bit result.
After all that is what a lot of code wants.

I was thinking of something like (see https://godbolt.org/z/h8oj7h4sd):

#define divu(a, b) ({ \
    u64 _a = a, _b = b, _q; \
    u32 _alo = _a, _blo = _b; \
    u32 _ahi = _a >> 32, _bhi = _b >> 32; \
    if (_bhi) { \
        _q = div64(_a, _b); \
    } else { \
        u32 _qhi = 0; \
        if (_ahi >= _blo) { \
            _qhi = _ahi / _blo; \
            _ahi = _ahi % _blo; \
        } \
        asm( "divl %2" : "+a" (_alo), "+d" (_ahi) : "r" (_blo)); \
        asm( "" : "=A" (_q) : "a" (_alo), "d" (_qhi)); \
    } \
    _q; \
})

Which will call out for a large divisor, but will do inlined divides
otherwise - so is reasonably sane whatever the types (even 32 by 32).
Modern gcc/clang also tracks values (to some extent) so will detect
_ahi and _bhi being compile-time zero even if the types are 64bit.

I've ignored the remainder for clarity.
But I'd add a 'u64 *rem' parameter and do a compile-time NULL check.

Fortunately the kernel doesn't let gcc use the xmm regsiters.
It has a nasty habit of bloating the code by trying to use them
for 64bit integers.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  reply	other threads:[~2024-05-04 22:54 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-28 16:40 [PATCH] perf/x86/amd/power: Use div64_u64 onstead of do_div() Jules Irenge
2024-05-02 16:25 ` David Laight
2024-05-02 16:34   ` Jules Bashizi Irenge
2024-05-02 22:18     ` David Laight
2024-05-02 22:59       ` Linus Torvalds
2024-05-04 22:53         ` David Laight [this message]
2024-05-05 19:38           ` Linus Torvalds
2024-05-06 16:39             ` David Laight
2024-05-06 16:45               ` Linus Torvalds
2024-05-06 17:26                 ` Linus Torvalds
2024-05-06 20:25                   ` David Laight
2024-05-06 17:43                 ` David Laight

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=e909d487c4c3468f8f20c952fe46c89c@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=jbi.octave@gmail.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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.
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).