1 /*
2  * By downloading, copying, installing or using the software you agree to this license.
3  * If you do not agree to this license, do not download, install,
4  * copy or use the software.
5  *
6  *
7  *                           License Agreement
8  *                For Open Source Computer Vision Library
9  *                        (3-clause BSD License)
10  *
11  * Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved.
12  * Third party copyrights are property of their respective owners.
13  *
14  * Redistribution and use in source and binary forms, with or without modification,
15  * are permitted provided that the following conditions are met:
16  *
17  *   * Redistributions of source code must retain the above copyright notice,
18  *     this list of conditions and the following disclaimer.
19  *
20  *   * Redistributions in binary form must reproduce the above copyright notice,
21  *     this list of conditions and the following disclaimer in the documentation
22  *     and/or other materials provided with the distribution.
23  *
24  *   * Neither the names of the copyright holders nor the names of the contributors
25  *     may be used to endorse or promote products derived from this software
26  *     without specific prior written permission.
27  *
28  * This software is provided by the copyright holders and contributors "as is" and
29  * any express or implied warranties, including, but not limited to, the implied
30  * warranties of merchantability and fitness for a particular purpose are disclaimed.
31  * In no event shall copyright holders or contributors be liable for any direct,
32  * indirect, incidental, special, exemplary, or consequential damages
33  * (including, but not limited to, procurement of substitute goods or services;
34  * loss of use, data, or profits; or business interruption) however caused
35  * and on any theory of liability, whether in contract, strict liability,
36  * or tort (including negligence or otherwise) arising in any way out of
37  * the use of this software, even if advised of the possibility of such damage.
38  */
39 
40 #ifndef CAROTENE_SRC_COMMON_HPP
41 #define CAROTENE_SRC_COMMON_HPP
42 
43 #include <cstddef>
44 #include <cstdlib>
45 #include <algorithm>
46 
47 #if defined WITH_NEON && (defined __ARM_NEON__ || defined __ARM_NEON)
48 #define CAROTENE_NEON
49 #endif
50 
51 #ifdef CAROTENE_NEON
52 #include <arm_neon.h>
53 #include "intrinsics.hpp"
54 #endif
55 
56 #include <carotene/functions.hpp>
57 #include "saturate_cast.hpp"
58 
59 namespace CAROTENE_NS { namespace internal {
60 
prefetch(const void * ptr,size_t offset=32* 10)61 inline void prefetch(const void *ptr, size_t offset = 32*10)
62 {
63 #if defined __GNUC__
64     __builtin_prefetch(reinterpret_cast<const char*>(ptr) + offset);
65 #elif defined _MSC_VER && defined CAROTENE_NEON
66     __prefetch(reinterpret_cast<const char*>(ptr) + offset);
67 #else
68     (void)ptr;
69     (void)offset;
70 #endif
71 }
72 
73 template <typename T>
getRowPtr(T * base,ptrdiff_t stride,size_t row)74 inline T *getRowPtr(T *base, ptrdiff_t stride, size_t row)
75 {
76     char *baseRaw = const_cast<char *>(reinterpret_cast<const char *>(base));
77     return reinterpret_cast<T *>(baseRaw + ptrdiff_t(row) * stride);
78 }
79 
80 void assertSupportedConfiguration(bool parametersSupported = true);
81 
82 ptrdiff_t borderInterpolate(ptrdiff_t _p, size_t _len, BORDER_MODE borderType, size_t startMargin = 0, size_t endMargin = 0);
83 
84 /*!
85  *  Aligns pointer by the certain number of bytes
86  *
87  *  This small inline function aligns the pointer by the certain number of bytes by shifting
88  *  it forward by 0 or a positive offset.
89  */
alignPtr(T * ptr,size_t n=sizeof (T))90 template<typename T> inline T* alignPtr(T* ptr, size_t n=sizeof(T))
91 {
92     return (T*)(((size_t)ptr + n-1) & -n);
93 }
94 
95 }}
96 
97 #endif
98