New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable abs and sqrt to return a param in more cases #24127
Conversation
Not urgent |
modules/standard/AutoMath.chpl
Outdated
proc abs(param x: real(32)) param : real(32) { | ||
return if x < 0 then -x else x; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Floating points are weird, and this worries me a little. Consider the following Python:
>>> float("-0")
-0.0
>>> float("-0") < 0
False
>>> abs(float("-0"))
0.0
In other words, numerically this differs from the behavior of Python's abs
:
>>> def myabs(x): return -x if x < 0 else x
...
>>> myabs(float("-0"))
-0.0
>>> abs(float("-0"))
0.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can wire this up to call an abs
primitive in the compiler to run fabs
. Do you feel that would address the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do -- fabs
seems ideal.
@DanilaFe - OK, I've added the primitive. From this point it would be trivial to also add param |
Sorry for the delayed response. I think you shouldn't worry about sqrt and complex here -- it can be left for future work (a "good first issue" perhaps?). |
frontend/lib/resolution/prims.cpp
Outdated
primAbsGetType(Context* context, const CallInfo& ci) { | ||
QualifiedType ret = QualifiedType(); | ||
|
||
if (ci.numActuals() != 1) return ret; | ||
|
||
QualifiedType actualType = ci.actual(0).type(); | ||
if (auto comp = actualType.type()->toComplexType()) { | ||
int w = comp->componentBitwidth(); | ||
ret = QualifiedType(QualifiedType::CONST_VAR, RealType::get(context, w)); | ||
} else if (auto img = actualType.type()->toImagType()) { | ||
int w = img->bitwidth(); | ||
ret = QualifiedType(QualifiedType::CONST_VAR, RealType::get(context, w)); | ||
} else { | ||
// otherwise just use the original type | ||
ret = QualifiedType(QualifiedType::CONST_VAR, actualType.type()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the production compiler treats ABS and SQRT as param-only primitives. That leads me to think that Dyno should also implement the param logic, and not support the non-param case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I added some comments along these lines, and will note it in Future Work.
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
The C++ overloading handles the size --- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
--- Signed-off-by: Michael Ferguson <mppf@users.noreply.github.com>
Nice, thanks! |
Signed-off-by: Vassily Litvinov <vasslitvinov@users.noreply.github.com>
This PR reacts to #24127 analogously to #24157, following the same argument in favor of changing the line numbers vs. introducing a prediff. Additionally, it reflects a reduction in the allocation/freeing size of `[domain(1,int(64),one)] int(64)` from 168 to 160 bytes. I did not investigate where that comes from. reviewed, although favored the prediff solution: @mppf
Follow-up to PR #24127. After #24127, the new `sqrtparams` test started to fail on Mac OS X system. Upon investigating, I identified that the difference in behavior from what the test expects is due to differences in how `std::sqrt` is implemented for complex numbers in libc++ vs stdlibc++. Also, I noticed that the issue is limited to the C++ implementation of the complex square root. This PR changes the implementation to use a C implementation of complex square root since it appears to have more reliable precision. Note that this C implementation required some workarounds for `CMPLX` not being available on all systems (as with PR #24184 and #24211). In particular, `CMPLX` does not seem to be available when using `clang` on Ubuntu 23.10, even though it is available with `gcc` on that system. I filed an issue against libc++ about the problem, but in discussion there it sounds like complex `sqrt` isn't required to have any particular precision. * llvm/llvm-project#78738 Reviewed by @DanilaFe - thanks! - [x] full comm=none testing
For issue #24119.
Resolves #24125.
This PR adds overloads to AutoMath to implement
param
versions of:abs
for all numeric typessqrt
for floating-point types.Additionally, while testing, I realized that
complex.re
andcomplex.im
did not haveparam
returning versions, so this PR also adds such param-returning versions to ChapelBase.To implement these, I:
PRIM_SQRT
andPRIM_ABS
. For now, these are just used for computing these functions onparam
s and they will cause the compiler to error out one makes it to code-generation time.PRIM_ABS
,PRIM_SQRT
,PRIM_GET_REAL
, andPRIM_GET_IMAG
in num.cpppostFoldPrimop
to foldPRIM_ABS
,PRIM_SQRT
,PRIM_GET_REAL
, andPRIM_GET_IMAG
This PR also updates the spec description of
complex
to make it clearer. While there, it removesproc complex.bits
from the spec, since this method does not exist other than in the spec.Reviewed by @DanilaFe - thanks!
Future Work:
PRIM_ABS
,PRIM_SQRT
are only used onparam
valuesPRIM_GET_REAL
andPRIM_GET_IMAG
onparam
values