|
| 1 | +--- |
| 2 | +title: intrinsics |
| 3 | +--- |
| 4 | + |
| 5 | +# The `stdlib_intrinsics` module |
| 6 | + |
| 7 | +[TOC] |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +The `stdlib_intrinsics` module provides replacements for some of the well known intrinsic functions found in Fortran compilers for which either a faster and/or more accurate implementation is found which has also proven of interest to the Fortran community. |
| 12 | + |
| 13 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 14 | +### `stdlib_sum` function |
| 15 | + |
| 16 | +#### Description |
| 17 | + |
| 18 | +The `stdlib_sum` function can replace the intrinsic `sum` for `real`, `complex` or `integer` arrays. It follows a chunked implementation which maximizes vectorization potential as well as reducing the round-off error. This procedure is recommended when summing large (e..g, >2**10 elements) arrays, for repetitive summation of smaller arrays consider the classical `sum`. |
| 19 | + |
| 20 | +#### Syntax |
| 21 | + |
| 22 | +`res = ` [[stdlib_intrinsics(module):stdlib_sum(interface)]] ` (x [,mask] )` |
| 23 | + |
| 24 | +`res = ` [[stdlib_intrinsics(module):stdlib_sum(interface)]] ` (x, dim [,mask] )` |
| 25 | + |
| 26 | +#### Status |
| 27 | + |
| 28 | +Experimental |
| 29 | + |
| 30 | +#### Class |
| 31 | + |
| 32 | +Pure function. |
| 33 | + |
| 34 | +#### Argument(s) |
| 35 | + |
| 36 | +`x`: N-D array of either `real`, `complex` or `integer` type. This argument is `intent(in)`. |
| 37 | + |
| 38 | +`dim` (optional): scalar of type `integer` with a value in the range from 1 to n, where n equals the rank of `x`. |
| 39 | + |
| 40 | +`mask` (optional): N-D array of `logical` values, with the same shape as `x`. This argument is `intent(in)`. |
| 41 | + |
| 42 | +#### Output value or Result value |
| 43 | + |
| 44 | +If `dim` is absent, the output is a scalar of the same `type` and `kind` as to that of `x`. Otherwise, an array of rank n-1, where n equals the rank of `x`, and a shape similar to that of `x` with dimension `dim` dropped is returned. |
| 45 | + |
| 46 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 47 | +### `stdlib_sum_kahan` function |
| 48 | + |
| 49 | +#### Description |
| 50 | + |
| 51 | +The `stdlib_sum_kahan` function can replace the intrinsic `sum` for `real` or `complex` arrays. It follows a chunked implementation which maximizes vectorization potential complemented by an `elemental` kernel based on the [kahan summation](https://doi.org/10.1145%2F363707.363723) strategy to reduce the round-off error: |
| 52 | + |
| 53 | +```fortran |
| 54 | +elemental subroutine kahan_kernel_<kind>(a,s,c) |
| 55 | + type(<kind>), intent(in) :: a |
| 56 | + type(<kind>), intent(inout) :: s |
| 57 | + type(<kind>), intent(inout) :: c |
| 58 | + type(<kind>) :: t, y |
| 59 | + y = a - c |
| 60 | + t = s + y |
| 61 | + c = (t - s) - y |
| 62 | + s = t |
| 63 | +end subroutine |
| 64 | +``` |
| 65 | + |
| 66 | +#### Syntax |
| 67 | + |
| 68 | +`res = ` [[stdlib_intrinsics(module):stdlib_sum_kahan(interface)]] ` (x [,mask] )` |
| 69 | + |
| 70 | +`res = ` [[stdlib_intrinsics(module):stdlib_sum_kahan(interface)]] ` (x, dim [,mask] )` |
| 71 | + |
| 72 | +#### Status |
| 73 | + |
| 74 | +Experimental |
| 75 | + |
| 76 | +#### Class |
| 77 | + |
| 78 | +Pure function. |
| 79 | + |
| 80 | +#### Argument(s) |
| 81 | + |
| 82 | +`x`: 1D array of either `real` or `complex` type. This argument is `intent(in)`. |
| 83 | + |
| 84 | +`dim` (optional): scalar of type `integer` with a value in the range from 1 to n, where n equals the rank of `x`. |
| 85 | + |
| 86 | +`mask` (optional): N-D array of `logical` values, with the same shape as `x`. This argument is `intent(in)`. |
| 87 | + |
| 88 | +#### Output value or Result value |
| 89 | + |
| 90 | +If `dim` is absent, the output is a scalar of the same type and kind as to that of `x`. Otherwise, an array of rank n-1, where n equals the rank of `x`, and a shape similar to that of `x` with dimension `dim` dropped is returned. |
| 91 | + |
| 92 | +#### Example |
| 93 | + |
| 94 | +```fortran |
| 95 | +{!example/intrinsics/example_sum.f90!} |
| 96 | +``` |
| 97 | + |
| 98 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 99 | +### `stdlib_dot_product` function |
| 100 | + |
| 101 | +#### Description |
| 102 | + |
| 103 | +The `stdlib_dot_product` function can replace the intrinsic `dot_product` for 1D `real`, `complex` or `integer` arrays. It follows a chunked implementation which maximizes vectorization potential as well as reducing the round-off error. This procedure is recommended when crunching large arrays, for repetitive products of smaller arrays consider the classical `dot_product`. |
| 104 | + |
| 105 | +#### Syntax |
| 106 | + |
| 107 | +`res = ` [[stdlib_intrinsics(module):stdlib_dot_product(interface)]] ` (x, y)` |
| 108 | + |
| 109 | +#### Status |
| 110 | + |
| 111 | +Experimental |
| 112 | + |
| 113 | +#### Class |
| 114 | + |
| 115 | +Pure function. |
| 116 | + |
| 117 | +#### Argument(s) |
| 118 | + |
| 119 | +`x`: 1D array of either `real`, `complex` or `integer` type. This argument is `intent(in)`. |
| 120 | + |
| 121 | +`y`: 1D array of the same type and kind as `x`. This argument is `intent(in)`. |
| 122 | + |
| 123 | +#### Output value or Result value |
| 124 | + |
| 125 | +The output is a scalar of `type` and `kind` same as to that of `x` and `y`. |
| 126 | + |
| 127 | +<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> |
| 128 | +### `stdlib_dot_product_kahan` function |
| 129 | + |
| 130 | +#### Description |
| 131 | + |
| 132 | +The `stdlib_dot_product_kahan` function can replace the intrinsic `dot_product` for 1D `real` or `complex` arrays. It follows a chunked implementation which maximizes vectorization potential, complemented by the same `elemental` kernel based on the [kahan summation](https://doi.org/10.1145%2F363707.363723) used for `stdlib_sum` to reduce the round-off error. |
| 133 | + |
| 134 | +#### Syntax |
| 135 | + |
| 136 | +`res = ` [[stdlib_intrinsics(module):stdlib_dot_product_kahan(interface)]] ` (x, y)` |
| 137 | + |
| 138 | +#### Status |
| 139 | + |
| 140 | +Experimental |
| 141 | + |
| 142 | +#### Class |
| 143 | + |
| 144 | +Pure function. |
| 145 | + |
| 146 | +#### Argument(s) |
| 147 | + |
| 148 | +`x`: 1D array of either `real` or `complex` type. This argument is `intent(in)`. |
| 149 | + |
| 150 | +`y`: 1D array of the same type and kind as `x`. This argument is `intent(in)`. |
| 151 | + |
| 152 | +#### Output value or Result value |
| 153 | + |
| 154 | +The output is a scalar of the same type and kind as to that of `x` and `y`. |
| 155 | + |
| 156 | +```fortran |
| 157 | +{!example/intrinsics/example_dot_product.f90!} |
| 158 | +``` |
0 commit comments