converter.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // (c) Copyright Fernando Luis Cacciola Carballal 2000-2004
  2. // Use, modification, and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See library home page at http://www.boost.org/libs/numeric/conversion
  6. //
  7. // Contact the author at: fernando_cacciola@hotmail.com
  8. //
  9. #ifndef BOOST_NUMERIC_CONVERSION_DETAIL_CONVERTER_FLC_12NOV2002_HPP
  10. #define BOOST_NUMERIC_CONVERSION_DETAIL_CONVERTER_FLC_12NOV2002_HPP
  11. #include <functional>
  12. #include "boost/numeric/conversion/detail/meta.hpp"
  13. #include "boost/numeric/conversion/detail/conversion_traits.hpp"
  14. #include "boost/numeric/conversion/bounds.hpp"
  15. #include "boost/type_traits/is_same.hpp"
  16. #include "boost/mpl/integral_c.hpp"
  17. namespace boost { namespace numeric { namespace convdetail
  18. {
  19. // Integral Constants representing rounding modes
  20. typedef mpl::integral_c<std::float_round_style, std::round_toward_zero> round2zero_c ;
  21. typedef mpl::integral_c<std::float_round_style, std::round_to_nearest> round2nearest_c ;
  22. typedef mpl::integral_c<std::float_round_style, std::round_toward_infinity> round2inf_c ;
  23. typedef mpl::integral_c<std::float_round_style, std::round_toward_neg_infinity> round2neg_inf_c ;
  24. // Metafunction:
  25. //
  26. // for_round_style<RoundStyle,RoundToZero,RoundToNearest,RoundToInf,RoundToNegInf>::type
  27. //
  28. // {RoundStyle} Integral Constant specifying a round style as declared above.
  29. // {RoundToZero,RoundToNearest,RoundToInf,RoundToNegInf} arbitrary types.
  30. //
  31. // Selects one of the 4 types according to the value of RoundStyle.
  32. //
  33. template<class RoundStyle,class RoundToZero,class RoundToNearest,class RoundToInf,class RoundToNegInf>
  34. struct for_round_style
  35. {
  36. typedef ct_switch4<RoundStyle
  37. , round2zero_c, round2nearest_c, round2inf_c // round2neg_inf_c
  38. , RoundToZero , RoundToNearest , RoundToInf , RoundToNegInf
  39. > selector ;
  40. typedef typename selector::type type ;
  41. } ;
  42. //--------------------------------------------------------------------------
  43. // Range Checking Logic.
  44. //
  45. // The range checking logic is built up by combining 1 or 2 predicates.
  46. // Each predicate is encapsulated in a template class and exposes
  47. // the static member function 'apply'.
  48. //
  49. //--------------------------------------------------------------------------
  50. // Because a particular logic can combine either 1 or two predicates, the following
  51. // tags are used to allow the predicate applier to receive 2 preds, but optimize away
  52. // one of them if it is 'non-applicable'
  53. struct non_applicable { typedef mpl::false_ do_apply ; } ;
  54. struct applicable { typedef mpl::true_ do_apply ; } ;
  55. //--------------------------------------------------------------------------
  56. //
  57. // Range Checking Logic implementations.
  58. //
  59. // The following classes, collectivelly named 'Predicates', are instantiated within
  60. // the corresponding range checkers.
  61. // Their static member function 'apply' is called to perform the actual range checking logic.
  62. //--------------------------------------------------------------------------
  63. // s < Lowest(T) ? cNegOverflow : cInRange
  64. //
  65. template<class Traits>
  66. struct LT_LoT : applicable
  67. {
  68. typedef typename Traits::target_type T ;
  69. typedef typename Traits::source_type S ;
  70. typedef typename Traits::argument_type argument_type ;
  71. static range_check_result apply ( argument_type s )
  72. {
  73. return s < static_cast<S>(bounds<T>::lowest()) ? cNegOverflow : cInRange ;
  74. }
  75. } ;
  76. // s < 0 ? cNegOverflow : cInRange
  77. //
  78. template<class Traits>
  79. struct LT_Zero : applicable
  80. {
  81. typedef typename Traits::source_type S ;
  82. typedef typename Traits::argument_type argument_type ;
  83. static range_check_result apply ( argument_type s )
  84. {
  85. return s < static_cast<S>(0) ? cNegOverflow : cInRange ;
  86. }
  87. } ;
  88. // s <= Lowest(T)-1 ? cNegOverflow : cInRange
  89. //
  90. template<class Traits>
  91. struct LE_PrevLoT : applicable
  92. {
  93. typedef typename Traits::target_type T ;
  94. typedef typename Traits::source_type S ;
  95. typedef typename Traits::argument_type argument_type ;
  96. static range_check_result apply ( argument_type s )
  97. {
  98. return s <= static_cast<S>(bounds<T>::lowest()) - static_cast<S>(1.0)
  99. ? cNegOverflow : cInRange ;
  100. }
  101. } ;
  102. // s < Lowest(T)-0.5 ? cNegOverflow : cInRange
  103. //
  104. template<class Traits>
  105. struct LT_HalfPrevLoT : applicable
  106. {
  107. typedef typename Traits::target_type T ;
  108. typedef typename Traits::source_type S ;
  109. typedef typename Traits::argument_type argument_type ;
  110. static range_check_result apply ( argument_type s )
  111. {
  112. return s < static_cast<S>(bounds<T>::lowest()) - static_cast<S>(0.5)
  113. ? cNegOverflow : cInRange ;
  114. }
  115. } ;
  116. // s > Highest(T) ? cPosOverflow : cInRange
  117. //
  118. template<class Traits>
  119. struct GT_HiT : applicable
  120. {
  121. typedef typename Traits::target_type T ;
  122. typedef typename Traits::source_type S ;
  123. typedef typename Traits::argument_type argument_type ;
  124. static range_check_result apply ( argument_type s )
  125. {
  126. return s > static_cast<S>(bounds<T>::highest())
  127. ? cPosOverflow : cInRange ;
  128. }
  129. } ;
  130. // s >= Lowest(T) + 1 ? cPosOverflow : cInRange
  131. //
  132. template<class Traits>
  133. struct GE_SuccHiT : applicable
  134. {
  135. typedef typename Traits::target_type T ;
  136. typedef typename Traits::source_type S ;
  137. typedef typename Traits::argument_type argument_type ;
  138. static range_check_result apply ( argument_type s )
  139. {
  140. return s >= static_cast<S>(bounds<T>::highest()) + static_cast<S>(1.0)
  141. ? cPosOverflow : cInRange ;
  142. }
  143. } ;
  144. // s >= Lowest(T) + 0.5 ? cPosgOverflow : cInRange
  145. //
  146. template<class Traits>
  147. struct GT_HalfSuccHiT : applicable
  148. {
  149. typedef typename Traits::target_type T ;
  150. typedef typename Traits::source_type S ;
  151. typedef typename Traits::argument_type argument_type ;
  152. static range_check_result apply ( argument_type s )
  153. {
  154. return s >= static_cast<S>(bounds<T>::highest()) + static_cast<S>(0.5)
  155. ? cPosOverflow : cInRange ;
  156. }
  157. } ;
  158. //--------------------------------------------------------------------------
  159. //
  160. // Predicate Combiner.
  161. //
  162. // This helper classes are used to possibly combine the range checking logic
  163. // individually performed by the predicates
  164. //
  165. //--------------------------------------------------------------------------
  166. // Applies both predicates: first 'PredA', and if it equals 'cInRange', 'PredB'
  167. template<class PredA, class PredB>
  168. struct applyBoth
  169. {
  170. typedef typename PredA::argument_type argument_type ;
  171. static range_check_result apply ( argument_type s )
  172. {
  173. range_check_result r = PredA::apply(s) ;
  174. if ( r == cInRange )
  175. r = PredB::apply(s);
  176. return r ;
  177. }
  178. } ;
  179. template<class PredA, class PredB>
  180. struct combine
  181. {
  182. typedef applyBoth<PredA,PredB> Both ;
  183. typedef void NNone ; // 'None' is defined as a macro in (/usr/X11R6/include/X11/X.h)
  184. typedef typename PredA::do_apply do_applyA ;
  185. typedef typename PredB::do_apply do_applyB ;
  186. typedef typename for_both<do_applyA, do_applyB, Both, PredA, PredB, NNone>::type type ;
  187. } ;
  188. //--------------------------------------------------------------------------
  189. // Range Checker classes.
  190. //
  191. // The following classes are VISIBLE base classes of the user-level converter<> class.
  192. // They supply the optimized 'out_of_range()' and 'validate_range()' static member functions
  193. // visible in the user interface.
  194. //
  195. //--------------------------------------------------------------------------
  196. // Dummy range checker.
  197. template<class Traits>
  198. struct dummy_range_checker
  199. {
  200. typedef typename Traits::argument_type argument_type ;
  201. static range_check_result out_of_range ( argument_type ) { return cInRange ; }
  202. static void validate_range ( argument_type ) {}
  203. } ;
  204. // Generic range checker.
  205. //
  206. // All the range checking logic for all possible combinations of source and target
  207. // can be arranged in terms of one or two predicates, which test overflow on both neg/pos 'sides'
  208. // of the ranges.
  209. //
  210. // These predicates are given here as IsNegOverflow and IsPosOverflow.
  211. //
  212. template<class Traits, class IsNegOverflow, class IsPosOverflow, class OverflowHandler>
  213. struct generic_range_checker
  214. {
  215. typedef OverflowHandler overflow_handler ;
  216. typedef typename Traits::argument_type argument_type ;
  217. static range_check_result out_of_range ( argument_type s )
  218. {
  219. typedef typename combine<IsNegOverflow,IsPosOverflow>::type Predicate ;
  220. return Predicate::apply(s);
  221. }
  222. static void validate_range ( argument_type s )
  223. { OverflowHandler()( out_of_range(s) ) ; }
  224. } ;
  225. //--------------------------------------------------------------------------
  226. //
  227. // Selectors for the optimized Range Checker class.
  228. //
  229. //--------------------------------------------------------------------------
  230. template<class Traits,class OverflowHandler>
  231. struct GetRC_Sig2Sig_or_Unsig2Unsig
  232. {
  233. typedef dummy_range_checker<Traits> Dummy ;
  234. typedef LT_LoT<Traits> Pred1 ;
  235. typedef GT_HiT<Traits> Pred2 ;
  236. typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> Normal ;
  237. typedef typename Traits::subranged subranged ;
  238. typedef typename mpl::if_<subranged,Normal,Dummy>::type type ;
  239. } ;
  240. template<class Traits, class OverflowHandler>
  241. struct GetRC_Sig2Unsig
  242. {
  243. typedef LT_Zero<Traits> Pred1 ;
  244. typedef GT_HiT <Traits> Pred2 ;
  245. typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> ChoiceA ;
  246. typedef generic_range_checker<Traits,Pred1,non_applicable,OverflowHandler> ChoiceB ;
  247. typedef typename Traits::target_type T ;
  248. typedef typename Traits::source_type S ;
  249. typedef typename subranged_Unsig2Sig<S,T>::type oposite_subranged ;
  250. typedef typename mpl::not_<oposite_subranged>::type positively_subranged ;
  251. typedef typename mpl::if_<positively_subranged,ChoiceA,ChoiceB>::type type ;
  252. } ;
  253. template<class Traits, class OverflowHandler>
  254. struct GetRC_Unsig2Sig
  255. {
  256. typedef GT_HiT<Traits> Pred1 ;
  257. typedef generic_range_checker<Traits,non_applicable,Pred1,OverflowHandler> type ;
  258. } ;
  259. template<class Traits,class OverflowHandler>
  260. struct GetRC_Int2Int
  261. {
  262. typedef GetRC_Sig2Sig_or_Unsig2Unsig<Traits,OverflowHandler> Sig2SigQ ;
  263. typedef GetRC_Sig2Unsig <Traits,OverflowHandler> Sig2UnsigQ ;
  264. typedef GetRC_Unsig2Sig <Traits,OverflowHandler> Unsig2SigQ ;
  265. typedef Sig2SigQ Unsig2UnsigQ ;
  266. typedef typename Traits::sign_mixture sign_mixture ;
  267. typedef typename
  268. for_sign_mixture<sign_mixture,Sig2SigQ,Sig2UnsigQ,Unsig2SigQ,Unsig2UnsigQ>::type
  269. selector ;
  270. typedef typename selector::type type ;
  271. } ;
  272. template<class Traits>
  273. struct GetRC_Int2Float
  274. {
  275. typedef dummy_range_checker<Traits> type ;
  276. } ;
  277. template<class Traits, class OverflowHandler, class Float2IntRounder>
  278. struct GetRC_Float2Int
  279. {
  280. typedef LE_PrevLoT <Traits> Pred1 ;
  281. typedef GE_SuccHiT <Traits> Pred2 ;
  282. typedef LT_HalfPrevLoT<Traits> Pred3 ;
  283. typedef GT_HalfSuccHiT<Traits> Pred4 ;
  284. typedef GT_HiT <Traits> Pred5 ;
  285. typedef LT_LoT <Traits> Pred6 ;
  286. typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> ToZero ;
  287. typedef generic_range_checker<Traits,Pred3,Pred4,OverflowHandler> ToNearest ;
  288. typedef generic_range_checker<Traits,Pred1,Pred5,OverflowHandler> ToInf ;
  289. typedef generic_range_checker<Traits,Pred6,Pred2,OverflowHandler> ToNegInf ;
  290. typedef typename Float2IntRounder::round_style round_style ;
  291. typedef typename for_round_style<round_style,ToZero,ToNearest,ToInf,ToNegInf>::type type ;
  292. } ;
  293. template<class Traits, class OverflowHandler>
  294. struct GetRC_Float2Float
  295. {
  296. typedef dummy_range_checker<Traits> Dummy ;
  297. typedef LT_LoT<Traits> Pred1 ;
  298. typedef GT_HiT<Traits> Pred2 ;
  299. typedef generic_range_checker<Traits,Pred1,Pred2,OverflowHandler> Normal ;
  300. typedef typename Traits::subranged subranged ;
  301. typedef typename mpl::if_<subranged,Normal,Dummy>::type type ;
  302. } ;
  303. template<class Traits, class OverflowHandler, class Float2IntRounder>
  304. struct GetRC_BuiltIn2BuiltIn
  305. {
  306. typedef GetRC_Int2Int<Traits,OverflowHandler> Int2IntQ ;
  307. typedef GetRC_Int2Float<Traits> Int2FloatQ ;
  308. typedef GetRC_Float2Int<Traits,OverflowHandler,Float2IntRounder> Float2IntQ ;
  309. typedef GetRC_Float2Float<Traits,OverflowHandler> Float2FloatQ ;
  310. typedef typename Traits::int_float_mixture int_float_mixture ;
  311. typedef typename for_int_float_mixture<int_float_mixture, Int2IntQ, Int2FloatQ, Float2IntQ, Float2FloatQ>::type selector ;
  312. typedef typename selector::type type ;
  313. } ;
  314. template<class Traits, class OverflowHandler, class Float2IntRounder>
  315. struct GetRC
  316. {
  317. typedef GetRC_BuiltIn2BuiltIn<Traits,OverflowHandler,Float2IntRounder> BuiltIn2BuiltInQ ;
  318. typedef dummy_range_checker<Traits> Dummy ;
  319. typedef mpl::identity<Dummy> DummyQ ;
  320. typedef typename Traits::udt_builtin_mixture udt_builtin_mixture ;
  321. typedef typename for_udt_builtin_mixture<udt_builtin_mixture,BuiltIn2BuiltInQ,DummyQ,DummyQ,DummyQ>::type selector ;
  322. typedef typename selector::type type ;
  323. } ;
  324. //--------------------------------------------------------------------------
  325. // Converter classes.
  326. //
  327. // The following classes are VISIBLE base classes of the user-level converter<> class.
  328. // They supply the optimized 'nearbyint()' and 'convert()' static member functions
  329. // visible in the user interface.
  330. //
  331. //--------------------------------------------------------------------------
  332. //
  333. // Trivial Converter : used when (cv-unqualified) T == (cv-unqualified) S
  334. //
  335. template<class Traits>
  336. struct trivial_converter_impl : public dummy_range_checker<Traits>
  337. {
  338. typedef Traits traits ;
  339. typedef typename Traits::source_type source_type ;
  340. typedef typename Traits::argument_type argument_type ;
  341. typedef typename Traits::result_type result_type ;
  342. static result_type low_level_convert ( argument_type s ) { return s ; }
  343. static source_type nearbyint ( argument_type s ) { return s ; }
  344. static result_type convert ( argument_type s ) { return s ; }
  345. } ;
  346. //
  347. // Rounding Converter : used for float to integral conversions.
  348. //
  349. template<class Traits,class RangeChecker,class RawConverter,class Float2IntRounder>
  350. struct rounding_converter : public RangeChecker
  351. ,public Float2IntRounder
  352. ,public RawConverter
  353. {
  354. typedef RangeChecker RangeCheckerBase ;
  355. typedef Float2IntRounder Float2IntRounderBase ;
  356. typedef RawConverter RawConverterBase ;
  357. typedef Traits traits ;
  358. typedef typename Traits::source_type source_type ;
  359. typedef typename Traits::argument_type argument_type ;
  360. typedef typename Traits::result_type result_type ;
  361. static result_type convert ( argument_type s )
  362. {
  363. RangeCheckerBase::validate_range(s);
  364. source_type s1 = Float2IntRounderBase::nearbyint(s);
  365. return RawConverterBase::low_level_convert(s1);
  366. }
  367. } ;
  368. //
  369. // Non-Rounding Converter : used for all other conversions.
  370. //
  371. template<class Traits,class RangeChecker,class RawConverter>
  372. struct non_rounding_converter : public RangeChecker
  373. ,public RawConverter
  374. {
  375. typedef RangeChecker RangeCheckerBase ;
  376. typedef RawConverter RawConverterBase ;
  377. typedef Traits traits ;
  378. typedef typename Traits::source_type source_type ;
  379. typedef typename Traits::argument_type argument_type ;
  380. typedef typename Traits::result_type result_type ;
  381. static source_type nearbyint ( argument_type s ) { return s ; }
  382. static result_type convert ( argument_type s )
  383. {
  384. RangeCheckerBase::validate_range(s);
  385. return RawConverterBase::low_level_convert(s);
  386. }
  387. } ;
  388. //--------------------------------------------------------------------------
  389. //
  390. // Selectors for the optimized Converter class.
  391. //
  392. //--------------------------------------------------------------------------
  393. template<class Traits,class OverflowHandler,class Float2IntRounder,class RawConverter, class UserRangeChecker>
  394. struct get_non_trivial_converter
  395. {
  396. typedef GetRC<Traits,OverflowHandler,Float2IntRounder> InternalRangeCheckerQ ;
  397. typedef is_same<UserRangeChecker,UseInternalRangeChecker> use_internal_RC ;
  398. typedef mpl::identity<UserRangeChecker> UserRangeCheckerQ ;
  399. typedef typename
  400. mpl::eval_if<use_internal_RC,InternalRangeCheckerQ,UserRangeCheckerQ>::type
  401. RangeChecker ;
  402. typedef non_rounding_converter<Traits,RangeChecker,RawConverter> NonRounding ;
  403. typedef rounding_converter<Traits,RangeChecker,RawConverter,Float2IntRounder> Rounding ;
  404. typedef mpl::identity<NonRounding> NonRoundingQ ;
  405. typedef mpl::identity<Rounding> RoundingQ ;
  406. typedef typename Traits::int_float_mixture int_float_mixture ;
  407. typedef typename
  408. for_int_float_mixture<int_float_mixture, NonRoundingQ, NonRoundingQ, RoundingQ, NonRoundingQ>::type
  409. selector ;
  410. typedef typename selector::type type ;
  411. } ;
  412. template< class Traits
  413. ,class OverflowHandler
  414. ,class Float2IntRounder
  415. ,class RawConverter
  416. ,class UserRangeChecker
  417. >
  418. struct get_converter_impl
  419. {
  420. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT( 0x0561 ) )
  421. // bcc55 prefers sometimes template parameters to be explicit local types.
  422. // (notice that is is illegal to reuse the names like this)
  423. typedef Traits Traits ;
  424. typedef OverflowHandler OverflowHandler ;
  425. typedef Float2IntRounder Float2IntRounder ;
  426. typedef RawConverter RawConverter ;
  427. typedef UserRangeChecker UserRangeChecker ;
  428. #endif
  429. typedef trivial_converter_impl<Traits> Trivial ;
  430. typedef mpl::identity <Trivial> TrivialQ ;
  431. typedef get_non_trivial_converter< Traits
  432. ,OverflowHandler
  433. ,Float2IntRounder
  434. ,RawConverter
  435. ,UserRangeChecker
  436. > NonTrivialQ ;
  437. typedef typename Traits::trivial trivial ;
  438. typedef typename mpl::eval_if<trivial,TrivialQ,NonTrivialQ>::type type ;
  439. } ;
  440. } } } // namespace boost::numeric::convdetail
  441. #endif