2 * Copyright 2008-2012 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <thrust/detail/type_traits.h>
22 //#include <stdint.h> // for intmax_t (not provided on MSVS 2005)
30 // XXX good enough for the platforms we care about
31 typedef long long intmax_t;
33 template<typename Number>
35 : integral_constant<bool, std::numeric_limits<Number>::is_signed>
42 std::numeric_limits<T>::is_specialized,
45 std::numeric_limits<T>::digits
49 sizeof(T) * std::numeric_limits<unsigned char>::digits - (is_signed<T>::value ? 1 : 0)
55 template<typename Integer>
56 struct integer_difference
58 // sizeof(Integer) >= sizeof(intmax_t),
60 // is_signed<Integer>::value,
61 // identity_<Integer>,
62 // identity_<intmax_t>
65 // sizeof(Integer) < sizeof(std::ptrdiff_t),
66 // identity_<std::ptrdiff_t>,
67 // identity_<intmax_t>
72 // XXX workaround a pedantic warning in old versions of g++
73 // which complains about &&ing with a constant value
74 template<bool x, bool y>
77 static const bool value = false;
83 static const bool value = y;
90 std::numeric_limits<Integer>::is_signed,
91 // digits is the number of no-sign bits
92 (!std::numeric_limits<Integer>::is_bounded || (int(std::numeric_limits<Integer>::digits) + 1 >= num_digits<intmax_t>::value))
96 int(std::numeric_limits<Integer>::digits) + 1 < num_digits<signed int>::value,
97 identity_<signed int>,
99 int(std::numeric_limits<Integer>::digits) + 1 < num_digits<signed long>::value,
100 identity_<signed long>,
105 }; // end integer_difference
108 template<typename Number>
109 struct numeric_difference
111 is_integral<Number>::value,
112 integer_difference<Number>,
115 {}; // end numeric_difference
118 template<typename Number>
120 typename numeric_difference<Number>::type
121 numeric_distance(Number x, Number y)
123 typedef typename numeric_difference<Number>::type difference_type;
124 return difference_type(y) - difference_type(x);
125 } // end numeric_distance