Skip to content

Add lagmma#3215

Open
RaviTriv wants to merge 3 commits into
google:masterfrom
RaviTriv:lgamma
Open

Add lagmma#3215
RaviTriv wants to merge 3 commits into
google:masterfrom
RaviTriv:lgamma

Conversation

@RaviTriv

Copy link
Copy Markdown
Contributor

Implementing lgamma from wishlist.

  • Also addressing following up from tgamma PR to use MaskedXorOr.

Thanks for taking a look!

@jan-wassenberg jan-wassenberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Just one tiny optimization, and a question: should we maybe call the user-facing function LogGamma, and the implementation Lgamma or LogGamma+Suffix? This might be more obvious. The comment can still mention that it corresponds to lgamma.

Comment thread hwy/contrib/math/math-inl.h Outdated

// [1, 2): logGamma = (y-1)*(y-2)*MidPoly(y - 1.5) + acc.
const V t = Sub(y, kMidCenter);
const V zero_factors = Mul(Sub(y, kOne), Sub(y, kTwo));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the sub could be fused away into MulSub, or preferably MulAdd(f, m, -a) which is more widely natively supported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

  • updated to use MulAdd on FMA targets
    • used fallback on non FMA targets as the intermediate rounding error was relatively large where lgamma is around 0

@RaviTriv RaviTriv marked this pull request as ready for review July 14, 2026 01:43

@jan-wassenberg jan-wassenberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :) Got one more optimization suggestion:

const V low_poly = impl.LowPoly(d, Sub(w, kLowCenter));
V low;
if constexpr (HWY_NATIVE_FMA) {
low = MulAdd(w, low_poly, Neg(low_poly));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than MulAdd + Neg, we are better off using MulSub because that is actually native on some platforms (x86). Same below.
Even better, we can change to q = Sub(kTwo, y), then NegMulAdd(y, q, q) which is native on more platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants