Re: [Sbcl-devel] [Sbcl-commits] master: Micro-optimize integer-length on fixnums on x86-64.

8 views
Skip to first unread message

Paul Khuong

unread,
Apr 30, 2013, 4:16:59 PM4/30/13
to Stas Boukarev, sbcl-...@lists.sourceforge.net
stassats wrote:
> +(define-vop (fixnum-len)
[...]
> + (:args (arg :scs (any-reg) :target res))
> + (:arg-types tagged-num)
> + (:results (res :scs (unsigned-reg)))
> + (:result-types unsigned-num)
> + (:generator 25
> + (move res arg)
> + (when (> n-fixnum-tag-bits 1)
> + (inst shr res (1- n-fixnum-tag-bits)))
Shouldn't that be an arithmetic shift?
> + (if (sc-is res unsigned-reg)
> + (inst test res res)
> + (inst cmp res 0))
> + (inst jmp :ge POS)
What's the SC test for?
> + (inst not res)
That's probably more quickly computed as:

mov mask, res
sar mask, (1- n-word-bits)
xor res, mask

Paul Khuong

------------------------------------------------------------------------------
Introducing AppDynamics Lite, a free troubleshooting tool for Java/.NET
Get 100% visibility into your production application - at no cost.
Code-level diagnostics for performance bottlenecks with <2% overhead
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap1
_______________________________________________
Sbcl-devel mailing list
Sbcl-...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sbcl-devel

Stas Boukarev

unread,
May 1, 2013, 4:46:10 AM5/1/13
to Paul Khuong, sbcl-...@lists.sourceforge.net
Paul Khuong <p...@pvk.ca> writes:

> stassats wrote:
>> +(define-vop (fixnum-len)
> [...]
>> + (:args (arg :scs (any-reg) :target res))
>> + (:arg-types tagged-num)
>> + (:results (res :scs (unsigned-reg)))
>> + (:result-types unsigned-num)
>> + (:generator 25
>> + (move res arg)
>> + (when (> n-fixnum-tag-bits 1)
>> + (inst shr res (1- n-fixnum-tag-bits)))
> Shouldn't that be an arithmetic shift?
Right, I should get a build where n-fixnum-tag-bits > 1.

>> + (if (sc-is res unsigned-reg)
>> + (inst test res res)
>> + (inst cmp res 0))
>> + (inst jmp :ge POS)
> What's the SC test for?
That was copied from the original instruction.

>> + (inst not res)
> That's probably more quickly computed as:
>
> mov mask, res
> sar mask, (1- n-word-bits)
> xor res, mask
That uses an additional register, is 2 bytes larger and is only slightly
faster in my tests, not worth it.

--
With best regards, Stas.

Lutz Euler

unread,
May 1, 2013, 7:31:22 AM5/1/13
to sbcl-...@lists.sourceforge.net
Hi,

Stas wrote:

> Paul Khuong <p...@pvk.ca> writes:
>
> > stassats wrote:
[...]
> >> + (if (sc-is res unsigned-reg)
> >> + (inst test res res)
> >> + (inst cmp res 0))
> >> + (inst jmp :ge POS)
> > What's the SC test for?
> That was copied from the original instruction.

this kind of SC test is quite common: to test if a value is 0, if it is
in a register, "test" can be used, which has a shorter encoding than
"cmp". "cmp" on the other hand can take one operand in memory, too.

Just grep for "smaller instruction" in src/compiler/x86-64/*.lisp.

It seemed so far not important or advantageous to me (as a person who
added a few of these) to abstract that pattern into a macro or function.

Regards,

Lutz

Paul Khuong

unread,
May 1, 2013, 7:52:49 AM5/1/13
to sbcl-...@lists.sourceforge.net
Lutz Euler wrote:
> Stas wrote:
>
>> Paul Khuong<p...@pvk.ca> writes:
>>
>>> stassats wrote:
> [...]
>>>> + (if (sc-is res unsigned-reg)
>>>> + (inst test res res)
>>>> + (inst cmp res 0))
>>>> + (inst jmp :ge POS)
>>> What's the SC test for?
>> That was copied from the original instruction.
>
> this kind of SC test is quite common: to test if a value is 0, if it is
> in a register, "test" can be used, which has a shorter encoding than
> "cmp". "cmp" on the other hand can take one operand in memory, too.

Right, but the VOP wants an unsigned-reg for res (without any load-if
condition).

Paul Khuong

Lutz Euler

unread,
May 1, 2013, 8:43:32 AM5/1/13
to sbcl-...@lists.sourceforge.net
Paul Khuong wrote:
> Lutz Euler wrote:
> > Stas wrote:
> >
> >> Paul Khuong<p...@pvk.ca> writes:
> >>
> >>> stassats wrote:
> > [...]
> >>>> + (if (sc-is res unsigned-reg)
> >>>> + (inst test res res)
> >>>> + (inst cmp res 0))
> >>>> + (inst jmp :ge POS)
> >>> What's the SC test for?
> >> That was copied from the original instruction.
> >
> > this kind of SC test is quite common: to test if a value is 0, if it is
> > in a register, "test" can be used, which has a shorter encoding than
> > "cmp". "cmp" on the other hand can take one operand in memory, too.
>
> Right, but the VOP wants an unsigned-reg for res (without any load-if
> condition).

You are right. Sorry, I didn't expect anything wrong in that direction
so didn't look hard enough.
The SC-test is unnecessary; one can always use the TEST instruction.
The original VOP SIGNED-BYTE-64-LEN has the same unnecessary test.
The change from "always use cmp" to "test the sc" there was commit
16028d14234d2acd0e6a3145a7364f2a52eabf63, which affects x86, too.

Greetings,

Lutz
Reply all
Reply to author
Forward
0 new messages