[PATCH] Implement log1p builtin

---
generic/include/clc/clc.h | 1 +
generic/include/clc/math/log1p.h | 24 +++
generic/include/clc/math/log1p.inc | 23 +++
generic/lib/SOURCES | 2 +
generic/lib/math/log1p.cl | 178 ++++++++++++++++++
generic/lib/math/math.h | 26 +++
generic/lib/math/tables.cl | 366 +++++++++++++++++++++++++++++++++++++
generic/lib/math/tables.h | 50 +++++
8 files changed, 670 insertions(+)
create mode 100644 generic/include/clc/math/log1p.h
create mode 100644 generic/include/clc/math/log1p.inc
create mode 100644 generic/lib/math/log1p.cl
create mode 100644 generic/lib/math/tables.cl
create mode 100644 generic/lib/math/tables.h

diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
index 490893b..5948a2a 100644
--- a/generic/include/clc/clc.h
+++ b/generic/include/clc/clc.h
@@ -48,6 +48,7 @@
#include <clc/math/fmin.h>
#include <clc/math/hypot.h>
#include <clc/math/log.h>
+#include <clc/math/log1p.h>
#include <clc/math/log2.h>
#include <clc/math/mad.h>
#include <clc/math/mix.h>
diff --git a/generic/include/clc/math/log1p.h b/generic/include/clc/math/log1p.h
new file mode 100644
index 0000000..4d716dd
--- /dev/null
+++ b/generic/include/clc/math/log1p.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2014 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#define __CLC_BODY <clc/math/log1p.inc>
+#include <clc/math/gentype.inc>
diff --git a/generic/include/clc/math/log1p.inc b/generic/include/clc/math/log1p.inc
new file mode 100644
index 0000000..4cbfbf3
--- /dev/null
+++ b/generic/include/clc/math/log1p.inc
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2014 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE log1p(__CLC_GENTYPE a);
diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
index 8eaaa61..65e30a8 100644
--- a/generic/lib/SOURCES
+++ b/generic/lib/SOURCES
@@ -39,8 +39,10 @@ math/exp10.cl
math/fmax.cl
math/fmin.cl
math/hypot.cl
+math/log1p.cl
math/mad.cl
math/mix.cl
+math/tables.cl
math/clc_nextafter.cl
math/nextafter.cl
math/pown.cl
diff --git a/generic/lib/math/log1p.cl b/generic/lib/math/log1p.cl
new file mode 100644
index 0000000..de385f7
--- /dev/null
+++ b/generic/lib/math/log1p.cl
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2014 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <clc/clc.h>
+
+#include "math.h"
+#include "tables.h"
+#include "../clcmacro.h"
+
+_CLC_OVERLOAD _CLC_DEF float log1p(float x)
+{
+ float w = x;
+ uint ux = as_uint(x);
+ uint ax = ux & EXSIGNBIT_SP32;
+
+ // |x| < 2^-4
+ float u2 = MATH_DIVIDE(x, 2.0f + x);
+ float u = u2 + u2;
+ float v = u * u;
+ // 2/(5 * 2^5), 2/(3 * 2^3)
+ float zsmall = mad(-u2, x, mad(v, 0x1.99999ap-7f, 0x1.555556p-4f) * v * u) + x;
+
+ // |x| >= 2^-4
+ //x = x + 1.0f;

Is the previous line there on purpose?

With that removed (or with an argument to keep it), this patch looks
good to me. I won't pretend to know everything about how the
algorithm works, but the rest seems fine.

I've just submitted a basic piglit test to that list so that we can
verify at least that the implementation runs and produces results in
line with what we expect for a small selection of input values.

--Aaron

> ---
> generic/include/clc/clc.h | 1 +
> generic/include/clc/math/log1p.h | 24 +++
> generic/include/clc/math/log1p.inc | 23 +++
> generic/lib/SOURCES | 2 +
> generic/lib/math/log1p.cl | 178 ++++++++++++++++++
> generic/lib/math/math.h | 26 +++
> generic/lib/math/tables.cl | 366 +++++++++++++++++++++++++++++++++++++
> generic/lib/math/tables.h | 50 +++++
> 8 files changed, 670 insertions(+)
> create mode 100644 generic/include/clc/math/log1p.h
> create mode 100644 generic/include/clc/math/log1p.inc
> create mode 100644 generic/lib/math/log1p.cl
> create mode 100644 generic/lib/math/tables.cl
> create mode 100644 generic/lib/math/tables.h
>
> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h
> index 490893b..5948a2a 100644
> --- a/generic/include/clc/clc.h
> +++ b/generic/include/clc/clc.h
> @@ -48,6 +48,7 @@
> #include <clc/math/fmin.h>
> #include <clc/math/hypot.h>
> #include <clc/math/log.h>
> +#include <clc/math/log1p.h>
> #include <clc/math/log2.h>
> #include <clc/math/mad.h>
> #include <clc/math/mix.h>
> diff --git a/generic/include/clc/math/log1p.h b/generic/include/clc/math/log1p.h
> new file mode 100644
> index 0000000..4d716dd
> --- /dev/null
> +++ b/generic/include/clc/math/log1p.h
> @@ -0,0 +1,24 @@
> +/*
> + * Copyright (c) 2014 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#define __CLC_BODY <clc/math/log1p.inc>
> +#include <clc/math/gentype.inc>
> diff --git a/generic/include/clc/math/log1p.inc b/generic/include/clc/math/log1p.inc
> new file mode 100644
> index 0000000..4cbfbf3
> --- /dev/null
> +++ b/generic/include/clc/math/log1p.inc
> @@ -0,0 +1,23 @@
> +/*
> + * Copyright (c) 2014 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE log1p(__CLC_GENTYPE a);
> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES
> index 8eaaa61..65e30a8 100644
> --- a/generic/lib/SOURCES
> +++ b/generic/lib/SOURCES
> @@ -39,8 +39,10 @@ math/exp10.cl
> math/fmax.cl
> math/fmin.cl
> math/hypot.cl
> +math/log1p.cl
> math/mad.cl
> math/mix.cl
> +math/tables.cl
> math/clc_nextafter.cl
> math/nextafter.cl
> math/pown.cl
> diff --git a/generic/lib/math/log1p.cl b/generic/lib/math/log1p.cl
> new file mode 100644
> index 0000000..de385f7
> --- /dev/null
> +++ b/generic/lib/math/log1p.cl
> @@ -0,0 +1,178 @@
> +/*
> + * Copyright (c) 2014 Advanced Micro Devices, Inc.
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#include <clc/clc.h>
> +
> +#include "math.h"
> +#include "tables.h"
> +#include "../clcmacro.h"
> +
> +_CLC_OVERLOAD _CLC_DEF float log1p(float x)
> +{
> + float w = x;
> + uint ux = as_uint(x);
> + uint ax = ux & EXSIGNBIT_SP32;
> +
> + // |x| < 2^-4
> + float u2 = MATH_DIVIDE(x, 2.0f + x);
> + float u = u2 + u2;
> + float v = u * u;
> + // 2/(5 * 2^5), 2/(3 * 2^3)
> + float zsmall = mad(-u2, x, mad(v, 0x1.99999ap-7f, 0x1.555556p-4f) * v * u) + x;
> +
> + // |x| >= 2^-4
> + //x = x + 1.0f;

Is the previous line there on purpose?

I just pushed this patch with the line removed.

-Tom