<div dir="rtl"><div dir="ltr">Hi Aaron,</div><div dir="ltr"><br></div><div dir="ltr">Math functions are usually not implemented as simple math identities, due ot loss of accuracy and lower performance.</div><div dir="ltr">For example, see acos(double)</div><div dir="ltr"><br></div><div dir="ltr">  <a href="http://www.netlib.org/fdlibm/e_acos.c">http://www.netlib.org/fdlibm/e_acos.c</a><br></div><div dir="ltr"><br></div><div dir="ltr">openlibm ( <a href="https://github.com/JuliaLang/openlibm">https://github.com/JuliaLang/openlibm</a> ) is a good reference for more math functions.<br></div><div dir="ltr"><br></div><div dir="ltr">I don't know if this approach is appropriate or applicable for libclc OpenCL implementation, though.<br></div><div dir="ltr"><br></div><div dir="ltr">Yaron</div><div dir="ltr"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div dir="ltr">2014-09-10 18:43 GMT+03:00 Aaron Watry <span dir="ltr"><<a href="mailto:awatry@gmail.com" target="_blank">awatry@gmail.com</a>></span>:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: awatry<br>
Date: Wed Sep 10 10:43:29 2014<br>
New Revision: 217509<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=217509&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=217509&view=rev</a><br>
Log:<br>
math: Add acos implementation<br>
<br>
Passes the tests that were submitted to the piglit list<br>
<br>
Tested on R600 (Pitcairn)<br>
<br>
Signed-off-by: Aaron Watry <<a href="mailto:awatry@gmail.com">awatry@gmail.com</a>><br>
Reviewed-by: Jan Vesely <<a href="mailto:jan.vesely@rutgers.edu">jan.vesely@rutgers.edu</a>><br>
<br>
Added:<br>
    libclc/trunk/generic/include/clc/math/acos.h<br>
    libclc/trunk/generic/include/clc/math/acos.inc<br>
    libclc/trunk/generic/lib/math/<a href="http://acos.cl" target="_blank">acos.cl</a><br>
    libclc/trunk/generic/lib/math/acos.inc<br>
Modified:<br>
    libclc/trunk/generic/include/clc/clc.h<br>
    libclc/trunk/generic/lib/SOURCES<br>
<br>
Modified: libclc/trunk/generic/include/clc/clc.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clc.h?rev=217509&r1=217508&r2=217509&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/clc.h?rev=217509&r1=217508&r2=217509&view=diff</a><br>
==============================================================================<br>
--- libclc/trunk/generic/include/clc/clc.h (original)<br>
+++ libclc/trunk/generic/include/clc/clc.h Wed Sep 10 10:43:29 2014<br>
@@ -32,6 +32,7 @@<br>
 #include <clc/workitem/get_group_id.h><br>
<br>
 /* 6.11.2 Math Functions */<br>
+#include <clc/math/acos.h><br>
 #include <clc/math/atan.h><br>
 #include <clc/math/atan2.h><br>
 #include <clc/math/copysign.h><br>
<br>
Added: libclc/trunk/generic/include/clc/math/acos.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.h?rev=217509&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.h?rev=217509&view=auto</a><br>
==============================================================================<br>
--- libclc/trunk/generic/include/clc/math/acos.h (added)<br>
+++ libclc/trunk/generic/include/clc/math/acos.h Wed Sep 10 10:43:29 2014<br>
@@ -0,0 +1,2 @@<br>
+#define __CLC_BODY <clc/math/acos.inc><br>
+#include <clc/math/gentype.inc><br>
<br>
Added: libclc/trunk/generic/include/clc/math/acos.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.inc?rev=217509&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/include/clc/math/acos.inc?rev=217509&view=auto</a><br>
==============================================================================<br>
--- libclc/trunk/generic/include/clc/math/acos.inc (added)<br>
+++ libclc/trunk/generic/include/clc/math/acos.inc Wed Sep 10 10:43:29 2014<br>
@@ -0,0 +1 @@<br>
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE acos(__CLC_GENTYPE x);<br>
<br>
Modified: libclc/trunk/generic/lib/SOURCES<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=217509&r1=217508&r2=217509&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/SOURCES?rev=217509&r1=217508&r2=217509&view=diff</a><br>
==============================================================================<br>
--- libclc/trunk/generic/lib/SOURCES (original)<br>
+++ libclc/trunk/generic/lib/SOURCES Wed Sep 10 10:43:29 2014<br>
@@ -29,6 +29,7 @@ integer/<a href="http://sub_sat.cl" target="_blank">sub_sat.cl</a><br>
 integer/sub_sat_if.ll<br>
 integer/sub_sat_impl.ll<br>
 integer/<a href="http://upsample.cl" target="_blank">upsample.cl</a><br>
+math/<a href="http://acos.cl" target="_blank">acos.cl</a><br>
 math/<a href="http://atan.cl" target="_blank">atan.cl</a><br>
 math/<a href="http://atan2.cl" target="_blank">atan2.cl</a><br>
 math/<a href="http://copysign.cl" target="_blank">copysign.cl</a><br>
<br>
Added: libclc/trunk/generic/lib/math/<a href="http://acos.cl" target="_blank">acos.cl</a><br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.cl?rev=217509&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.cl?rev=217509&view=auto</a><br>
==============================================================================<br>
--- libclc/trunk/generic/lib/math/<a href="http://acos.cl" target="_blank">acos.cl</a> (added)<br>
+++ libclc/trunk/generic/lib/math/<a href="http://acos.cl" target="_blank">acos.cl</a> Wed Sep 10 10:43:29 2014<br>
@@ -0,0 +1,8 @@<br>
+#include <clc/clc.h><br>
+<br>
+#ifdef cl_khr_fp64<br>
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable<br>
+#endif<br>
+<br>
+#define __CLC_BODY <acos.inc><br>
+#include <clc/math/gentype.inc><br>
<br>
Added: libclc/trunk/generic/lib/math/acos.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.inc?rev=217509&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/libclc/trunk/generic/lib/math/acos.inc?rev=217509&view=auto</a><br>
==============================================================================<br>
--- libclc/trunk/generic/lib/math/acos.inc (added)<br>
+++ libclc/trunk/generic/lib/math/acos.inc Wed Sep 10 10:43:29 2014<br>
@@ -0,0 +1,21 @@<br>
+/*<br>
+ * There are multiple formulas for calculating arccosine of x:<br>
+ * 1) acos(x) = (1/2*pi) + i * ln(i*x + sqrt(1-x^2)) (notice the 'i'...)<br>
+ * 2) acos(x) = pi/2 + asin(-x) (asin isn't implemented yet)<br>
+ * 3) acos(x) = pi/2 - asin(x) (ditto)<br>
+ * 4) acos(x) = 2*atan2(sqrt(1-x), sqrt(1+x))<br>
+ * 5) acos(x) = pi/2 - atan2(x, ( sqrt(1-x^2) ) )<br>
+ *<br>
+ * Options 1-3 are not currently usable, #5 generates more concise radeonsi<br>
+ * bitcode and assembly than #4 (134 vs 132 instructions on radeonsi), but<br>
+ * precision of #4 may be better.<br>
+ */<br>
+<br>
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE acos(__CLC_GENTYPE x) {<br>
+  return (<br>
+    (__CLC_GENTYPE) 2.0 * atan2(<br>
+      sqrt((__CLC_GENTYPE) 1.0 - x),<br>
+      sqrt((__CLC_GENTYPE) 1.0 + x)<br>
+    )<br>
+  );<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>