|
| 1 | +/******************************************************* |
| 2 | + * Copyright (c) 2014, ArrayFire |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This file is distributed under 3-clause BSD license. |
| 6 | + * The complete license agreement can be obtained at: |
| 7 | + * http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | + ********************************************************/ |
| 9 | + |
| 10 | +#include <af/defines.h> |
| 11 | +#include <af/image.h> |
| 12 | +#include <handle.hpp> |
| 13 | +#include <err_common.hpp> |
| 14 | +#include <scan.hpp> |
| 15 | + |
| 16 | +using af::dim4; |
| 17 | +using namespace detail; |
| 18 | + |
| 19 | +template<typename To, typename Ti> |
| 20 | +static af_array sat(const af_array& in) |
| 21 | +{ |
| 22 | + const Array<To> input = castArray<To>(in); |
| 23 | + |
| 24 | + Array<To> hprefix_scan = scan<af_add_t, To, To>(input, 0); |
| 25 | + Array<To> vprefix_scan = scan<af_add_t, To, To>(hprefix_scan, 1); |
| 26 | + |
| 27 | + return getHandle<To>(vprefix_scan); |
| 28 | +} |
| 29 | + |
| 30 | +af_err af_sat(af_array* out, const af_array in) |
| 31 | +{ |
| 32 | + try{ |
| 33 | + ArrayInfo info = getInfo(in); |
| 34 | + const dim4 dims = info.dims(); |
| 35 | + |
| 36 | + ARG_ASSERT(1, (dims.ndims() >= 2)); |
| 37 | + |
| 38 | + af_dtype inputType = info.getType(); |
| 39 | + |
| 40 | + af_array output = 0; |
| 41 | + switch(inputType) { |
| 42 | + case f64: output = sat<double, double>(in); break; |
| 43 | + case f32: output = sat<float , float >(in); break; |
| 44 | + case s32: output = sat<int , int >(in); break; |
| 45 | + case u32: output = sat<uint , uint >(in); break; |
| 46 | + case b8: output = sat<int , char >(in); break; |
| 47 | + case u8: output = sat<uint , uchar >(in); break; |
| 48 | + case s64: output = sat<intl , intl >(in); break; |
| 49 | + case u64: output = sat<uintl , uintl >(in); break; |
| 50 | + default: TYPE_ERROR(1, inputType); |
| 51 | + } |
| 52 | + std::swap(*out, output); |
| 53 | + } |
| 54 | + CATCHALL; |
| 55 | + |
| 56 | + return AF_SUCCESS; |
| 57 | +} |
0 commit comments