]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/qvm/gen/gen.cpp
1 //Copyright (c) 2008-2017 Emil Dotchevski and Reverge Studios, Inc.
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include "boost/throw_exception.hpp"
7 #include "boost/exception/info.hpp"
8 #include "boost/exception/diagnostic_information.hpp"
9 #include "boost/bind.hpp"
25 #define TAB3 TAB TAB TAB
26 #define TAB4 TAB TAB TAB TAB
27 #define TAB5 TAB TAB TAB TAB TAB
28 #define INCLUDE_MAT_ASSIGN "boost/qvm/gen/mat_assign%d.hpp"
29 #define INCLUDE_VEC_ASSIGN "boost/qvm/gen/vec_assign%d.hpp"
30 #define INCLUDE_STATIC_ASSERT "boost/qvm/static_assert.hpp"
31 #define INCLUDE_MATH "boost/qvm/math.hpp"
32 #define INCLUDE_THROW_EXCEPTION "boost/qvm/throw_exception.hpp"
33 #define INCLUDE_ERROR "boost/qvm/error.hpp"
34 #define INCLUDE_INLINE "boost/qvm/inline.hpp"
35 #define INCLUDE_M_TRAITS "boost/qvm/mat_traits.hpp"
36 #define INCLUDE_V_TRAITS "boost/qvm/vec_traits.hpp"
37 #define INCLUDE_Q_TRAITS "boost/qvm/quat_traits.hpp"
38 #define INCLUDE_S_TRAITS "boost/qvm/scalar_traits.hpp"
39 #define INCLUDE_DEDUCE_M "boost/qvm/deduce_mat.hpp"
40 #define INCLUDE_DEDUCE_V "boost/qvm/deduce_vec.hpp"
41 #define INCLUDE_DEDUCE_Q "boost/qvm/deduce_quat.hpp"
42 #define INCLUDE_DEDUCE_S "boost/qvm/deduce_scalar.hpp"
43 #define INCLUDE_SWIZZLE_TRAITS "boost/qvm/detail/swizzle_traits.hpp"
44 #define INCLUDE_ENABLE_IF "boost/qvm/enable_if.hpp"
45 #define INCLUDE_ASSERT "boost/qvm/assert.hpp"
49 struct exception_base
: virtual std::exception
, virtual boost::exception
{ };
50 struct bad_command_line
: virtual exception_base
{ };
51 typedef boost::error_info
<struct cmd_arg_
,std::string
> cmd_arg
;
58 operator()( T
* ) const
67 s
<< std::setw(2) << std::setfill('0') << std::hex
<< std::uppercase
;
69 for( int i
=0; i
!=16; ++i
)
76 to_string( T
const & x
)
87 std::string output_directory
;
89 command_line_options():
98 output_file( output_file
const & );
99 output_file
& operator=( output_file
const & );
101 std::string
const output_directory
;
103 std::ostringstream out_
;
104 std::set
<std::string
> includes_
;
109 output_file( command_line_options
const & opt
):
110 output_directory(opt
.output_directory
),
116 require_include( std::string
const & fn
)
118 assert(!strchr(fn
.c_str(),'%'));
119 includes_
.insert(fn
);
129 dump( std::string
const & name
) const
131 std::ostream
* out
= &std::cout
;
132 boost::shared_ptr
<std::ofstream
> f
;
136 if( !output_directory
.empty() )
138 path
+=output_directory
;
142 boost::shared_ptr
<std::ofstream
>(new std::ofstream(path
.c_str())).swap(f
);
144 std::cout
<< "Writing " << path
<< "..." << std::endl
;
146 out
->exceptions(std::ofstream::eofbit
|std::ofstream::failbit
|std::ofstream::badbit
);
147 std::string include_guard
=get_include_guard();
149 "//Copyright (c) 2008-2017 Emil Dotchevski and Reverge Studios, Inc." NL
151 "//Distributed under the Boost Software License, Version 1.0. (See accompanying" NL
152 "//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)" NL
154 "#ifndef " << include_guard
<< NL
155 "#define " << include_guard
<< NL
157 "//This file was generated by a program. Do not edit manually." NL
160 for( std::set
<std::string
>::const_iterator i
=includes_
.begin(),e
=includes_
.end(); i
!=e
; ++i
)
161 *out
<< "#include <" << *i
<< ">" NL
;
180 replace( std::string
& s
, char const * substr
, char const * newstr
)
182 assert(substr
&& *substr
);
183 assert(newstr
&& *newstr
);
184 std::string::size_type f
=s
.find(substr
);
186 s
.replace(f
,f
+strlen(substr
),newstr
);
190 deduce_name( std::string
const & fn
, char const * suffix
)
193 replace(s
,"operator==","eq");
194 replace(s
,"operator!=","neq");
195 replace(s
,"operator+=","plus_eq");
196 replace(s
,"operator-=","minus_eq");
197 replace(s
,"operator*=","mul_eq");
198 replace(s
,"operator/=","div_eq");
199 replace(s
,"operator+","plus");
200 replace(s
,"operator-","minus");
201 replace(s
,"operator*","mul");
202 replace(s
,"operator/","div");
212 header_mr_ma_mb_same_size( output_file
& out
, int r
, int c
, std::string
const & name
)
216 assert(!name
.empty());
217 out
.require_include(INCLUDE_DEDUCE_M
);
219 TAB2
"template <class A,class B>" NL
220 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
221 TAB2
"typename lazy_enable_if_c<" NL
222 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<B>::rows=="<<r
<<" &&" NL
223 TAB3
"mat_traits<A>::cols=="<<c
<<" && mat_traits<B>::cols=="<<c
<<"," NL
224 TAB3
"deduce_mat2<A,B,"<<r
<<','<<c
<<"> >::type" NL
225 TAB2
<<name
<<"( A const & a, B const & b )" NL
230 header_mr_ma_mb_mult( output_file
& out
, int m
, int n
, int p
, std::string
const & name
)
235 assert(!name
.empty());
236 out
.require_include(INCLUDE_DEDUCE_M
);
238 TAB2
"template <class A,class B>" NL
239 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
240 TAB2
"typename lazy_enable_if_c<" NL
241 TAB3
"mat_traits<A>::rows=="<<m
<<" && mat_traits<B>::rows=="<<n
<<" &&" NL
242 TAB3
"mat_traits<A>::cols=="<<n
<<" && mat_traits<B>::cols=="<<p
<<"," NL
243 TAB3
"deduce_mat2<A,B,"<<m
<<','<<p
<<"> >::type" NL
244 TAB2
<<name
<<"( A const & a, B const & b )" NL
249 header_vr_ma_vb_mult( output_file
& out
, int r
, int c
, std::string
const & name
)
253 assert(!name
.empty());
254 out
.require_include(INCLUDE_DEDUCE_V
);
256 TAB2
"template <class A,class B>" NL
257 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
258 TAB2
"typename lazy_enable_if_c<" NL
259 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<A>::cols=="<<c
<<" &&" NL
260 TAB3
"vec_traits<B>::dim=="<<c
<<"," NL
261 TAB3
"deduce_vec2<A,B,"<<c
<<"> >::type" NL
262 TAB2
<<name
<<"( A const & a, B const & b )" NL
267 header_vr_va_mb_mult( output_file
& out
, int r
, int c
, std::string
const & name
)
271 assert(!name
.empty());
272 out
.require_include(INCLUDE_DEDUCE_V
);
274 TAB2
"template <class A,class B>" NL
275 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
276 TAB2
"typename lazy_enable_if_c<" NL
277 TAB3
"mat_traits<B>::rows=="<<r
<<" && mat_traits<B>::cols=="<<c
<<" &&" NL
278 TAB3
"vec_traits<A>::dim=="<<c
<<"," NL
279 TAB3
"deduce_vec2<A,B,"<<r
<<"> >::type" NL
280 TAB2
<<name
<<"( A const & a, B const & b )" NL
285 header_vr_va_vb_same_size( output_file
& out
, int d
, std::string
const & name
)
288 assert(!name
.empty());
289 out
.require_include(INCLUDE_DEDUCE_V
);
291 TAB2
"template <class A,class B>" NL
292 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
293 TAB2
"typename lazy_enable_if_c<" NL
294 TAB3
"vec_traits<A>::dim=="<<d
<<" && vec_traits<B>::dim=="<<d
<<"," NL
295 TAB3
"deduce_vec2<A,B,"<<d
<<"> >::type" NL
296 TAB2
<<name
<<"( A const & a, B const & b )" NL
301 header_bool_ma_mb_same_size( output_file
& out
, int r
, int c
, std::string
const & name
)
305 assert(!name
.empty());
307 TAB2
"template <class A,class B>" NL
308 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
309 TAB2
"typename enable_if_c<" NL
310 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<B>::rows=="<<r
<<" &&" NL
311 TAB3
"mat_traits<A>::cols=="<<c
<<" && mat_traits<B>::cols=="<<c
<<"," NL
312 TAB3
"bool>::type" NL
313 TAB2
<<name
<<"( A const & a, B const & b )" NL
318 header_bool_va_vb_same_size( output_file
& out
, int d
, std::string
const & name
)
321 assert(!name
.empty());
323 TAB2
"template <class A,class B>" NL
324 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
325 TAB2
"typename enable_if_c<" NL
326 TAB3
"vec_traits<A>::dim=="<<d
<<" && vec_traits<B>::dim=="<<d
<<"," NL
327 TAB2
"bool>::type" NL
328 TAB2
<<name
<<"( A const & a, B const & b )" NL
333 header_ma_mb_same_size( output_file
& out
, int r
, int c
, std::string
const & name
)
337 assert(!name
.empty());
339 TAB2
"template <class A,class B>" NL
340 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
341 TAB2
"typename enable_if_c<" NL
342 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<B>::rows=="<<r
<<" &&" NL
343 TAB3
"mat_traits<A>::cols=="<<c
<<" && mat_traits<B>::cols=="<<c
<<"," NL
345 TAB2
<<name
<<"( A & a, B const & b )" NL
350 header_va_vb_same_size( output_file
& out
, int d
, std::string
const & name
)
353 assert(!name
.empty());
355 TAB2
"template <class A,class B>" NL
356 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
357 TAB2
"typename enable_if_c<" NL
358 TAB3
"vec_traits<A>::dim=="<<d
<<" && vec_traits<B>::dim=="<<d
<<"," NL
360 TAB2
<<name
<<"( A & a, B const & b )" NL
365 header_sr_ma( output_file
& out
, int r
, int c
, std::string
const & name
)
369 assert(!name
.empty());
371 TAB2
"template <class A>" NL
372 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
373 TAB2
"typename enable_if_c<" NL
374 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<A>::cols=="<<c
<<"," NL
375 TAB3
"typename mat_traits<A>::scalar_type>::type" NL
376 TAB2
<<name
<<"( A const & a )" NL
381 header_sr_va_vb( output_file
& out
, int d
, std::string
const & name
)
384 assert(!name
.empty());
385 out
.require_include(INCLUDE_DEDUCE_S
);
387 TAB2
"template <class A,class B>" NL
388 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
389 TAB2
"typename lazy_enable_if_c<" NL
390 TAB3
"vec_traits<A>::dim=="<<d
<<" && vec_traits<B>::dim=="<<d
<<"," NL
391 TAB3
"deduce_scalar<typename vec_traits<A>::scalar_type,typename vec_traits<B>::scalar_type> >::type" NL
392 TAB2
<<name
<<"( A const & a, B const & b )" NL
397 header_sr_va( output_file
& out
, int d
, std::string
const & name
)
400 assert(!name
.empty());
402 TAB2
"template <class A>" NL
403 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
404 TAB2
"typename enable_if_c<" NL
405 TAB3
"is_vec<A>::value && vec_traits<A>::dim=="<<d
<<"," NL
406 TAB3
"typename vec_traits<A>::scalar_type>::type" NL
407 TAB2
<<name
<<"( A const & a )" NL
412 header_mr_ma( output_file
& out
, int r
, int c
, std::string
const & name
)
416 assert(!name
.empty());
417 out
.require_include(INCLUDE_DEDUCE_M
);
419 TAB2
"template <class A>" NL
420 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
421 TAB2
"typename lazy_enable_if_c<" NL
422 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<A>::cols=="<<c
<<"," NL
423 TAB3
"deduce_mat<A> >::type" NL
424 TAB2
<<name
<<"( A const & a )" NL
429 header_vr_va( output_file
& out
, int d
, std::string
const & name
)
432 assert(!name
.empty());
433 out
.require_include(INCLUDE_DEDUCE_V
);
435 TAB2
"template <class A>" NL
436 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
437 TAB2
"typename lazy_enable_if_c<" NL
438 TAB3
"vec_traits<A>::dim=="<<d
<<"," NL
439 TAB3
"deduce_vec<A> >::type" NL
440 TAB2
<<name
<<"( A const & a )" NL
445 header_vr_va_same_size( output_file
& out
, int d
, std::string
const & name
)
448 assert(!name
.empty());
450 TAB2
"template <class R,class A>" NL
451 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
452 TAB2
"typename enable_if_c<" NL
453 TAB3
"is_vec<A>::value &&" NL
454 TAB3
"vec_traits<R>::dim=="<<d
<<" && vec_traits<A>::dim=="<<d
<<"," NL
456 TAB2
<<name
<<"( A const & a )" NL
461 header_mr_ma_sb( output_file
& out
, int r
, int c
, std::string
const & name
)
465 assert(!name
.empty());
466 out
.require_include(INCLUDE_DEDUCE_M
);
468 TAB2
"template <class A,class B>" NL
469 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
470 TAB2
"typename lazy_enable_if_c<" NL
471 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<A>::cols=="<<c
<<" && is_scalar<B>::value," NL
472 TAB3
"deduce_mat<A> >::type" NL
473 TAB2
<<name
<<"( A const & a, B b )" NL
478 header_mr_sa_mb( output_file
& out
, int r
, int c
, std::string
const & name
)
482 assert(!name
.empty());
483 out
.require_include(INCLUDE_DEDUCE_M
);
485 TAB2
"template <class A,class B>" NL
486 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
487 TAB2
"typename lazy_enable_if_c<" NL
488 TAB3
"is_scalar<A>::value && mat_traits<B>::rows=="<<r
<<" && mat_traits<B>::cols=="<<c
<<"," NL
489 TAB3
"deduce_mat<B> >::type" NL
490 TAB2
<<name
<<"( A a, B const & b )" NL
495 header_vr_va_sb( output_file
& out
, int d
, std::string
const & name
)
498 assert(!name
.empty());
499 out
.require_include(INCLUDE_DEDUCE_V
);
501 TAB2
"template <class A,class B>" NL
502 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
503 TAB2
"typename lazy_enable_if_c<" NL
504 TAB3
"vec_traits<A>::dim=="<<d
<<" && is_scalar<B>::value," NL
505 TAB3
"deduce_vec<A> >::type" NL
506 TAB2
<<name
<<"( A const & a, B b )" NL
511 header_vr_sa_vb( output_file
& out
, int d
, std::string
const & name
)
514 assert(!name
.empty());
515 out
.require_include(INCLUDE_DEDUCE_V
);
517 TAB2
"template <class A,class B>" NL
518 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
519 TAB2
"typename lazy_enable_if_c<" NL
520 TAB3
"is_scalar<A>::value && vec_traits<B>::dim=="<<d
<<"," NL
521 TAB3
"deduce_vec<B> >::type" NL
522 TAB2
<<name
<<"( A a, B const & b )" NL
527 header_ma_sb( output_file
& out
, int r
, int c
, std::string
const & name
)
531 assert(!name
.empty());
533 TAB2
"template <class A,class B>" NL
534 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
535 TAB2
"typename enable_if_c<" NL
536 TAB3
"mat_traits<A>::rows=="<<r
<<" && mat_traits<A>::cols=="<<c
<<" && is_scalar<B>::value," NL
538 TAB2
<<name
<<"( A & a, B b )" NL
543 header_va_sb( output_file
& out
, int d
, std::string
const & name
)
546 assert(!name
.empty());
548 TAB2
"template <class A,class B>" NL
549 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
550 TAB2
"typename enable_if_c<" NL
551 TAB3
"vec_traits<A>::dim=="<<d
<<" && is_scalar<B>::value," NL
553 TAB2
<<name
<<"( A & a, B b )" NL
558 defined( std::ostream
& g
, int r
, int cr
, int c
, std::string fn
, char const * suffix
)
564 std::string dn
=deduce_name(fn
,suffix
);
565 std::string name
=dn
+"_defined";
571 TAB3
"using ::boost::qvm::"<<fn
<<";" NL
577 TAB3
"template <int R,int CR,int C>" NL
578 TAB3
"struct "<<name
<<";" NL
580 TAB3
"template <>" NL
582 TAB3
<<name
<<'<'<<r
<<','<<cr
<<','<<c
<<">" NL
584 TAB4
"static bool const value=true;" NL
592 defined( std::ostream
& g
, int r
, int c
, std::string
const & fn
, char const * suffix
)
597 std::string dn
=deduce_name(fn
,suffix
);
598 std::string name
=dn
+"_defined";
604 TAB3
"using ::boost::qvm::"<<fn
<<";" NL
610 TAB3
"template <int R,int C>" NL
611 TAB3
"struct "<<name
<<";" NL
613 TAB3
"template <>" NL
615 TAB3
<<name
<<"<"<<r
<<","<<c
<<">" NL
617 TAB4
"static bool const value=true;" NL
625 defined( std::ostream
& g
, int d
, std::string
const & fn
, char const * suffix
)
629 std::string dn
=deduce_name(fn
,suffix
);
630 std::string name
=dn
+"_defined";
636 TAB3
"using ::boost::qvm::"<<fn
<<";" NL
642 TAB3
"template <int D>" NL
643 TAB3
"struct "<<name
<<";" NL
645 TAB3
"template <>" NL
647 TAB3
<<name
<<"<"<<d
<<">" NL
649 TAB4
"static bool const value=true;" NL
657 mr_mult_ma_mb( output_file
& out
, int m
, int n
, int p
, char const * suffix
)
662 header_mr_ma_mb_mult(out
,m
,n
,p
,"operator*");
663 out
.require_include(INCLUDE_DEDUCE_M
);
664 std::ostream
& g
=out
.stream();
667 TAB3
"typedef typename mat_traits<A>::scalar_type Ta;" NL
668 TAB3
"typedef typename mat_traits<B>::scalar_type Tb;" NL
670 for( int i
=0; i
!=m
; ++i
)
671 for( int j
=0; j
!=n
; ++j
)
672 g
<<TAB3
"Ta const a"<<i
<<j
<<" = mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a);" NL
;
673 for( int i
=0; i
!=n
; ++i
)
674 for( int j
=0; j
!=p
; ++j
)
675 g
<<TAB3
"Tb const b"<<i
<<j
<<" = mat_traits<B>::template read_element<"<<i
<<','<<j
<<">(b);" NL
;
677 TAB3
"typedef typename deduce_mat2<A,B,"<<m
<<','<<p
<<">::type R;" NL
678 TAB3
"BOOST_QVM_STATIC_ASSERT(mat_traits<R>::rows=="<<m
<<");" NL
679 TAB3
"BOOST_QVM_STATIC_ASSERT(mat_traits<R>::cols=="<<p
<<");" NL
682 for( int i
=0; i
!=m
; ++i
)
683 for( int j
=0; j
!=p
; ++j
)
685 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r)=";
686 for( int k
=0; k
!=n
; ++k
)
690 g
<<'a'<<i
<<k
<<"*b"<<k
<<j
;
698 defined(g
,m
,n
,p
,"operator*",suffix
);
702 ma_mult_ma_mb( output_file
& out
, int d
, char const * suffix
)
705 header_ma_mb_same_size(out
,d
,d
,"operator*=");
706 std::ostream
& g
=out
.stream();
709 TAB3
"typedef typename mat_traits<A>::scalar_type Ta;" NL
710 TAB3
"typedef typename mat_traits<B>::scalar_type Tb;" NL
712 for( int i
=0; i
!=d
; ++i
)
713 for( int j
=0; j
!=d
; ++j
)
714 g
<<TAB3
"Ta const a"<<i
<<j
<<" = mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a);" NL
;
715 for( int i
=0; i
!=d
; ++i
)
716 for( int j
=0; j
!=d
; ++j
)
717 g
<<TAB3
"Tb const b"<<i
<<j
<<" = mat_traits<B>::template read_element<"<<i
<<','<<j
<<">(b);" NL
;
718 for( int i
=0; i
!=d
; ++i
)
719 for( int j
=0; j
!=d
; ++j
)
721 g
<<TAB3
"mat_traits<A>::template write_element<"<<i
<<","<<j
<<">(a)=";
722 for( int k
=0; k
!=d
; ++k
)
726 g
<<'a'<<i
<<k
<<"*b"<<k
<<j
;
734 defined(g
,d
,"operator*=",suffix
);
738 vr_mult_ma_vb( output_file
& out
, int r
, int c
, char const * suffix
)
742 header_vr_ma_vb_mult(out
,r
,c
,"operator*");
743 out
.require_include(INCLUDE_INLINE
);
744 out
.require_include(INCLUDE_V_TRAITS
);
745 out
.require_include(INCLUDE_M_TRAITS
);
746 out
.require_include(INCLUDE_ENABLE_IF
);
747 out
.require_include(INCLUDE_DEDUCE_V
);
748 std::ostream
& g
=out
.stream();
751 TAB3
"typedef typename mat_traits<A>::scalar_type Ta;" NL
752 TAB3
"typedef typename vec_traits<B>::scalar_type Tb;" NL
754 for( int i
=0; i
!=r
; ++i
)
755 for( int j
=0; j
!=c
; ++j
)
756 g
<<TAB3
"Ta const a"<<i
<<j
<<" = mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a);" NL
;
757 for( int i
=0; i
!=c
; ++i
)
758 g
<<TAB3
"Tb const b"<<i
<<" = vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
760 TAB3
"typedef typename deduce_vec2<A,B,"<<c
<<">::type R;" NL
761 TAB3
"BOOST_QVM_STATIC_ASSERT(vec_traits<R>::dim=="<<c
<<");" NL
764 for( int i
=0; i
!=r
; ++i
)
766 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=";
767 for( int j
=0; j
!=c
; ++j
)
771 g
<<'a'<<i
<<j
<<"*b"<<j
;
779 defined(g
,r
,c
,"operator*",suffix
);
783 vr_mult_va_mb( output_file
& out
, int r
, int c
, char const * suffix
)
787 header_vr_va_mb_mult(out
,r
,c
,"operator*");
788 out
.require_include(INCLUDE_INLINE
);
789 out
.require_include(INCLUDE_V_TRAITS
);
790 out
.require_include(INCLUDE_M_TRAITS
);
791 out
.require_include(INCLUDE_ENABLE_IF
);
792 out
.require_include(INCLUDE_DEDUCE_V
);
793 std::ostream
& g
=out
.stream();
796 TAB3
"typedef typename vec_traits<A>::scalar_type Ta;" NL
797 TAB3
"typedef typename mat_traits<B>::scalar_type Tb;" NL
799 for( int i
=0; i
!=r
; ++i
)
800 g
<<TAB3
"Ta const a"<<i
<<" = vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
801 for( int i
=0; i
!=r
; ++i
)
802 for( int j
=0; j
!=c
; ++j
)
803 g
<<TAB3
"Tb const b"<<i
<<j
<<" = mat_traits<B>::template read_element<"<<i
<<','<<j
<<">(b);" NL
;
805 TAB3
"typedef typename deduce_vec2<A,B,"<<r
<<">::type R;" NL
806 TAB3
"BOOST_QVM_STATIC_ASSERT(vec_traits<R>::dim=="<<r
<<");" NL
809 for( int i
=0; i
!=c
; ++i
)
811 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=";
812 for( int j
=0; j
!=r
; ++j
)
816 g
<<'a'<<j
<<"*b"<<j
<<i
;
824 defined(g
,r
,c
,"operator*",suffix
);
828 vr_op_va_vb_same_size( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
831 header_vr_va_vb_same_size(out
,d
,fn
);
832 out
.require_include(INCLUDE_DEDUCE_V
);
833 std::ostream
& g
=out
.stream();
836 TAB3
"typedef typename deduce_vec2<A,B,"<<d
<<">::type R;" NL
837 TAB3
"BOOST_QVM_STATIC_ASSERT(vec_traits<R>::dim=="<<d
<<");" NL
840 for( int i
=0; i
!=d
; ++i
)
841 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=vec_traits<A>::template read_element<"<<i
<<">(a)"<<op
<<"vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
846 defined(g
,d
,fn
,suffix
);
850 bool_eq_ma_mb( output_file
& out
, int r
, int c
, char const * suffix
)
852 header_bool_ma_mb_same_size(out
,r
,c
,"operator==");
853 std::ostream
& g
=out
.stream();
858 for( int i
=0; i
!=r
; ++i
)
859 for( int j
=0; j
!=c
; ++j
)
861 TAB4
"mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a)==mat_traits<B>::template read_element<"<<i
<<','<<j
<<">(b)"<<(i
!=r
-1||j
!=c
-1?" &&":";")<<NL
;
866 defined(g
,r
,c
,"operator==",suffix
);
870 bool_eq_va_vb( output_file
& out
, int d
, char const * suffix
)
872 header_bool_va_vb_same_size(out
,d
,"operator==");
873 std::ostream
& g
=out
.stream();
878 for( int i
=0; i
!=d
; ++i
)
880 TAB4
"vec_traits<A>::template read_element<"<<i
<<">(a)==vec_traits<B>::template read_element<"<<i
<<">(b)"<<(i
!=d
-1?" &&":";")<<NL
;
885 defined(g
,d
,"operator==",suffix
);
889 bool_neq_ma_mb( output_file
& out
, int r
, int c
, char const * suffix
)
891 header_bool_ma_mb_same_size(out
,r
,c
,"operator!=");
892 std::ostream
& g
=out
.stream();
897 for( int i
=0; i
!=r
; ++i
)
898 for( int j
=0; j
!=c
; ++j
)
900 TAB4
"!(mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a)==mat_traits<B>::template read_element<"<<i
<<','<<j
<<">(b))"<<(i
!=r
-1||j
!=c
-1?" ||":";")<<NL
;
905 defined(g
,r
,c
,"operator!=",suffix
);
909 bool_neq_va_vb( output_file
& out
, int d
, char const * suffix
)
911 header_bool_va_vb_same_size(out
,d
,"operator!=");
912 std::ostream
& g
=out
.stream();
917 for( int i
=0; i
!=d
; ++i
)
919 TAB4
"!(vec_traits<A>::template read_element<"<<i
<<">(a)==vec_traits<B>::template read_element<"<<i
<<">(b))"<<(i
!=d
-1?" ||":";")<<NL
;
924 defined(g
,d
,"operator!=",suffix
);
928 mr_op_ma_mb_same_size( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
933 header_mr_ma_mb_same_size(out
,r
,c
,fn
);
934 out
.require_include(INCLUDE_DEDUCE_M
);
935 std::ostream
& g
=out
.stream();
938 TAB3
"typedef typename deduce_mat2<A,B,"<<r
<<','<<c
<<">::type R;" NL
939 TAB3
"BOOST_QVM_STATIC_ASSERT(mat_traits<R>::rows=="<<r
<<");" NL
940 TAB3
"BOOST_QVM_STATIC_ASSERT(mat_traits<R>::cols=="<<c
<<");" NL
943 for( int i
=0; i
!=r
; ++i
)
944 for( int j
=0; j
!=c
; ++j
)
945 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r)=mat_traits<A>::template read_element<"<<i
<<","<<j
<<">(a)"<<op
<<"mat_traits<B>::template read_element<"<<i
<<","<<j
<<">(b);" NL
;
950 defined(g
,r
,c
,fn
,suffix
);
954 ma_op_ma_mb_same_size( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
957 header_ma_mb_same_size(out
,r
,c
,fn
);
958 std::ostream
& g
=out
.stream();
960 for( int i
=0; i
!=r
; ++i
)
961 for( int j
=0; j
!=c
; ++j
)
962 g
<<TAB3
"mat_traits<A>::template write_element<"<<i
<<","<<j
<<">(a)"<<op
<<"mat_traits<B>::template read_element<"<<i
<<","<<j
<<">(b);" NL
;
967 defined(g
,r
,c
,fn
,suffix
);
971 va_op_va_vb_same_size( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
974 header_va_vb_same_size(out
,d
,fn
);
975 std::ostream
& g
=out
.stream();
977 for( int i
=0; i
!=d
; ++i
)
978 g
<<TAB3
"vec_traits<A>::template write_element<"<<i
<<">(a)"<<op
<<"vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
983 defined(g
,d
,fn
,suffix
);
987 mr_op_ma( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
990 header_mr_ma(out
,r
,c
,fn
);
991 out
.require_include(INCLUDE_DEDUCE_M
);
992 std::ostream
& g
=out
.stream();
995 TAB3
"typedef typename deduce_mat<A>::type R;" NL
998 for( int i
=0; i
!=r
; ++i
)
999 for( int j
=0; j
!=c
; ++j
)
1000 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r)="<<op
<<"mat_traits<A>::template read_element<"<<i
<<","<<j
<<">(a);" NL
;
1005 defined(g
,r
,c
,fn
,suffix
);
1009 vr_op_va( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1011 assert(!op
.empty());
1012 header_vr_va(out
,d
,fn
);
1013 out
.require_include(INCLUDE_DEDUCE_V
);
1014 std::ostream
& g
=out
.stream();
1017 TAB3
"typedef typename deduce_vec<A>::type R;" NL
1020 for( int i
=0; i
!=d
; ++i
)
1021 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)="<<op
<<"vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1026 defined(g
,d
,fn
,suffix
);
1030 mr_op_ma_sb( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1032 assert(!op
.empty());
1033 header_mr_ma_sb(out
,r
,c
,fn
);
1034 out
.require_include(INCLUDE_DEDUCE_M
);
1035 out
.require_include(INCLUDE_DEDUCE_V
);
1036 std::ostream
& g
=out
.stream();
1039 TAB3
"typedef typename deduce_mat<A>::type R;" NL
1042 for( int i
=0; i
!=r
; ++i
)
1043 for( int j
=0; j
!=c
; ++j
)
1044 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r)=mat_traits<A>::template read_element<"<<i
<<","<<j
<<">(a)"<<op
<<"b;" NL
;
1049 defined(g
,r
,c
,fn
,suffix
);
1053 mr_op_sa_mb( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1055 assert(!op
.empty());
1056 header_mr_sa_mb(out
,r
,c
,fn
);
1057 out
.require_include(INCLUDE_DEDUCE_M
);
1058 out
.require_include(INCLUDE_DEDUCE_V
);
1059 std::ostream
& g
=out
.stream();
1062 TAB3
"typedef typename deduce_mat<B>::type R;" NL
1065 for( int i
=0; i
!=r
; ++i
)
1066 for( int j
=0; j
!=c
; ++j
)
1067 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r)=a"<<op
<<"mat_traits<B>::template read_element<"<<i
<<","<<j
<<">(b);" NL
;
1072 defined(g
,r
,c
,fn
,suffix
);
1076 vr_op_va_sb( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1078 assert(!op
.empty());
1079 header_vr_va_sb(out
,d
,fn
);
1080 out
.require_include(INCLUDE_DEDUCE_V
);
1081 std::ostream
& g
=out
.stream();
1084 TAB3
"typedef typename deduce_vec<A>::type R;" NL
1087 for( int i
=0; i
!=d
; ++i
)
1088 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=vec_traits<A>::template read_element<"<<i
<<">(a)"<<op
<<"b;" NL
;
1093 defined(g
,d
,fn
,suffix
);
1097 vr_op_sa_vb( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1099 assert(!op
.empty());
1100 header_vr_sa_vb(out
,d
,fn
);
1101 out
.require_include(INCLUDE_DEDUCE_V
);
1102 std::ostream
& g
=out
.stream();
1105 TAB3
"typedef typename deduce_vec<B>::type R;" NL
1108 for( int i
=0; i
!=d
; ++i
)
1109 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=a"<<op
<<"vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
1114 defined(g
,d
,fn
,suffix
);
1118 ma_op_ma_sb( output_file
& out
, int r
, int c
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1120 assert(!op
.empty());
1121 header_ma_sb(out
,r
,c
,fn
);
1122 std::ostream
& g
=out
.stream();
1126 for( int i
=0; i
!=r
; ++i
)
1127 for( int j
=0; j
!=c
; ++j
)
1128 g
<<TAB3
"mat_traits<A>::template write_element<"<<i
<<","<<j
<<">(a)"<<op
<<"b;" NL
;
1133 defined(g
,r
,c
,fn
,suffix
);
1137 va_op_va_sb( output_file
& out
, int d
, std::string
const & fn
, std::string
const & op
, char const * suffix
)
1139 assert(!op
.empty());
1140 header_va_sb(out
,d
,fn
);
1141 std::ostream
& g
=out
.stream();
1145 for( int i
=0; i
!=d
; ++i
)
1146 g
<<TAB3
"vec_traits<A>::template write_element<"<<i
<<">(a)"<<op
<<"b;" NL
;
1151 defined(g
,d
,fn
,suffix
);
1155 ma_assign_ma_mb( output_file
& out
, int r
, int c
, char const * suffix
)
1157 header_ma_mb_same_size(out
,r
,c
,"assign");
1158 out
.require_include(INCLUDE_M_TRAITS
);
1159 out
.require_include(INCLUDE_INLINE
);
1160 out
.require_include(INCLUDE_ENABLE_IF
);
1161 std::ostream
& g
=out
.stream();
1163 for( int i
=0; i
!=r
; ++i
)
1164 for( int j
=0; j
!=c
; ++j
)
1165 g
<<TAB3
"mat_traits<A>::template write_element<"<<i
<<","<<j
<<">(a)=mat_traits<B>::template read_element<"<<i
<<","<<j
<<">(b);" NL
;
1170 defined(g
,r
,c
,"assign",suffix
);
1174 va_assign_va_vb( output_file
& out
, int d
, char const * suffix
)
1176 header_va_vb_same_size(out
,d
,"assign");
1177 out
.require_include(INCLUDE_V_TRAITS
);
1178 out
.require_include(INCLUDE_INLINE
);
1179 out
.require_include(INCLUDE_ENABLE_IF
);
1180 std::ostream
& g
=out
.stream();
1182 for( int i
=0; i
!=d
; ++i
)
1183 g
<<TAB3
"vec_traits<A>::template write_element<"<<i
<<">(a)=vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
1188 defined(g
,d
,"assign",suffix
);
1192 mr_convert_to_ma( output_file
& out
, int r
, int c
, char const * suffix
)
1196 out
.require_include(INCLUDE_Q_TRAITS
);
1197 out
.require_include(INCLUDE_S_TRAITS
);
1199 std::ostream
& g
=out
.stream();
1201 TAB2
"template <class R,class A>" NL
1202 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
1203 TAB2
"typename enable_if_c<" NL
1204 TAB3
"mat_traits<R>::rows=="<<r
<<" && mat_traits<A>::rows=="<<r
<<" &&" NL
1205 TAB3
"mat_traits<R>::cols=="<<c
<<" && mat_traits<A>::cols=="<<c
<<"," NL
1207 TAB2
<<"convert_to( A const & a )" NL
1211 for( int i
=0; i
!=r
; ++i
)
1212 for( int j
=0; j
!=c
; ++j
)
1213 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r) = mat_traits<A>::template read_element<"<<i
<<","<<j
<<">(a);" NL
;
1222 TAB2
"template <class R,class A>" NL
1223 TAB2
"BOOST_QVM_INLINE" NL
1224 TAB2
"typename enable_if_c<" NL
1225 TAB3
"is_mat<R>::value && is_quat<A>::value &&" NL
1226 TAB3
"mat_traits<R>::rows=="<<r
<<" && mat_traits<R>::cols=="<<c
<<"," NL
1228 TAB2
"convert_to( A const & q )" NL
1230 TAB3
"typedef typename mat_traits<R>::scalar_type T;" NL
1231 TAB3
"T const a=quat_traits<A>::template read_element<0>(q);" NL
1232 TAB3
"T const b=quat_traits<A>::template read_element<1>(q);" NL
1233 TAB3
"T const c=quat_traits<A>::template read_element<2>(q);" NL
1234 TAB3
"T const d=quat_traits<A>::template read_element<3>(q);" NL
1235 TAB3
"T const bb = b*b;" NL
1236 TAB3
"T const cc = c*c;" NL
1237 TAB3
"T const dd = d*d;" NL
1238 TAB3
"T const bc = b*c;" NL
1239 TAB3
"T const bd = b*d;" NL
1240 TAB3
"T const cd = c*d;" NL
1241 TAB3
"T const ab = a*b;" NL
1242 TAB3
"T const ac = a*c;" NL
1243 TAB3
"T const ad = a*d;" NL
<<
1244 (r
>3?TAB3
"T const zero = scalar_traits<T>::value(0);" NL
:"")<<
1245 TAB3
"T const one = scalar_traits<T>::value(1);" NL
1246 TAB3
"T const two = one+one;" NL
1248 TAB3
"mat_traits<R>::template write_element<0,0>(r) = one - two*(cc+dd);" NL
1249 TAB3
"mat_traits<R>::template write_element<0,1>(r) = two*(bc-ad);" NL
1250 TAB3
"mat_traits<R>::template write_element<0,2>(r) = two*(bd+ac);" NL
1252 for( int i
=3; i
!=c
; ++i
)
1253 g
<<TAB3
"mat_traits<R>::template write_element<0,"<<i
<<">(r) = zero;" NL
;
1255 TAB3
"mat_traits<R>::template write_element<1,0>(r) = two*(bc+ad);" NL
1256 TAB3
"mat_traits<R>::template write_element<1,1>(r) = one - two*(bb+dd);" NL
1257 TAB3
"mat_traits<R>::template write_element<1,2>(r) = two*(cd-ab);" NL
1259 for( int i
=3; i
!=c
; ++i
)
1260 g
<<TAB3
"mat_traits<R>::template write_element<1,"<<i
<<">(r) = zero;" NL
;
1262 TAB3
"mat_traits<R>::template write_element<2,0>(r) = two*(bd-ac);" NL
1263 TAB3
"mat_traits<R>::template write_element<2,1>(r) = two*(cd+ab);" NL
1264 TAB3
"mat_traits<R>::template write_element<2,2>(r) = one - two*(bb+cc);" NL
1266 for( int i
=3; i
!=c
; ++i
)
1267 g
<<TAB3
"mat_traits<R>::template write_element<2,"<<i
<<">(r) = zero;" NL
;
1268 for( int i
=3; i
!=r
; ++i
)
1269 for( int j
=0; j
!=c
; ++j
)
1270 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<","<<j
<<">(r) = "<<(i
==j
?"one":"zero")<<";" NL
;
1276 defined(g
,r
,c
,"convert_to",suffix
);
1280 vr_convert_to_va( output_file
& out
, int d
, char const * suffix
)
1282 header_vr_va_same_size(out
,d
,"convert_to");
1283 std::ostream
& g
=out
.stream();
1287 for( int i
=0; i
!=d
; ++i
)
1288 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1293 defined(g
,d
,"convert_to",suffix
);
1299 del_row_col
const * next
;
1303 del_row_col( char var
):
1305 i(std::numeric_limits
<int>::max()),
1306 j(std::numeric_limits
<int>::max()),
1310 del_row_col( del_row_col
const & next
, int i
, int j
):
1318 idx( std::pair
<int,int> const & x
) const
1320 std::pair
<int,int> r(x
.first
+(x
.first
>=i
),x
.second
+(x
.second
>=j
));
1322 return next
->idx(r
);
1328 operator()( std::ostream
& g
, int r
, int c
) const
1330 std::pair
<int,int> p
=idx(std::make_pair(r
,c
));
1331 g
<< var
<< p
.first
<< p
.second
;
1336 determinant_impl( std::ostream
& g
, int n
, del_row_col
const & a
)
1341 char const * plus
="";
1342 for( int i
=0; i
!=n
; ++i
,plus
="+" )
1344 g
<<((i
&1)?"-":plus
);
1347 determinant_impl(g
,n
-1,del_row_col(a
,0,i
));
1353 determinant( output_file
& out
, int d
, char const * suffix
)
1355 header_sr_ma(out
,d
,d
,"determinant");
1356 std::ostream
& g
=out
.stream();
1359 TAB3
"typedef typename mat_traits<A>::scalar_type T;" NL
1361 for( int i
=0; i
!=d
; ++i
)
1362 for( int j
=0; j
!=d
; ++j
)
1363 g
<<TAB3
<<"T const a"<<i
<<j
<<"=mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a);" NL
;
1365 determinant_impl(g
,d
,del_row_col('a'));
1368 TAB3
"return det;" NL
1371 defined(g
,d
,"determinant",suffix
);
1375 inverse_ma( output_file
& out
, int d
, char const * suffix
)
1378 out
.require_include(INCLUDE_DEDUCE_M
);
1379 out
.require_include(INCLUDE_ASSERT
);
1380 out
.require_include(INCLUDE_THROW_EXCEPTION
);
1381 out
.require_include(INCLUDE_ERROR
);
1382 std::ostream
& g
=out
.stream();
1384 TAB2
"template <class A,class B>" NL
1385 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
1386 TAB2
"typename lazy_enable_if_c<" NL
1387 TAB3
"mat_traits<A>::rows=="<<d
<<" && mat_traits<A>::cols=="<<d
<<" && is_scalar<B>::value," NL
1388 TAB3
"deduce_mat<A> >::type" NL
1389 TAB2
"inverse( A const & a, B det )" NL
1391 TAB3
"typedef typename mat_traits<A>::scalar_type T;" NL
1392 TAB3
"BOOST_QVM_ASSERT(det!=scalar_traits<B>::value(0));" NL
1394 for( int i
=0; i
!=d
; ++i
)
1395 for( int j
=0; j
!=d
; ++j
)
1396 g
<<TAB3
"T const a"<<i
<<j
<<"=mat_traits<A>::template read_element<"<<i
<<','<<j
<<">(a);" NL
;
1398 TAB3
"T const f=scalar_traits<T>::value(1)/det;" NL
1399 TAB3
"typedef typename deduce_mat<A>::type R;" NL
1402 for( int i
=0; i
!=d
; ++i
)
1403 for( int j
=0; j
!=d
; ++j
)
1405 g
<<TAB3
"mat_traits<R>::template write_element<"<<i
<<','<<j
<<">(r)="<<(((i
+j
)&1)?'-':' ')<<"f*";
1406 determinant_impl(g
,d
-1,del_row_col(del_row_col('a'),j
,i
));
1413 TAB2
"template <class A>" NL
1414 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
1415 TAB2
"typename lazy_enable_if_c<" NL
1416 TAB3
"mat_traits<A>::rows=="<<d
<<" && mat_traits<A>::cols=="<<d
<<"," NL
1417 TAB3
"deduce_mat<A> >::type" NL
1418 TAB2
"inverse( A const & a )" NL
1420 TAB3
"typedef typename mat_traits<A>::scalar_type T;" NL
1421 TAB3
"T det=determinant(a);" NL
1422 TAB3
"if( det==scalar_traits<T>::value(0) )" NL
1423 TAB4
"BOOST_QVM_THROW_EXCEPTION(zero_determinant_error());" NL
1424 TAB3
"return inverse(a,det);" NL
1427 defined(g
,d
,"inverse",suffix
);
1431 mag_sqr( output_file
& out
, int d
, char const * suffix
)
1433 header_sr_va(out
,d
,"mag_sqr");
1434 out
.require_include(INCLUDE_MATH
);
1435 std::ostream
& g
=out
.stream();
1438 TAB3
"typedef typename vec_traits<A>::scalar_type T;" NL
1440 for( int i
=0; i
!=d
; ++i
)
1441 g
<<TAB3
"T const a"<<i
<<"=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1442 g
<<TAB3
"T const m2=";
1443 for( int i
=0; i
!=d
; ++i
)
1451 TAB3
"return m2;" NL
1454 defined(g
,d
,"mag_sqr",suffix
);
1458 mag( output_file
& out
, int d
, char const * suffix
)
1460 header_sr_va(out
,d
,"mag");
1461 out
.require_include(INCLUDE_MATH
);
1462 std::ostream
& g
=out
.stream();
1465 TAB3
"typedef typename vec_traits<A>::scalar_type T;" NL
1467 for( int i
=0; i
!=d
; ++i
)
1468 g
<<TAB3
"T const a"<<i
<<"=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1469 g
<<TAB3
"T const m2=";
1470 for( int i
=0; i
!=d
; ++i
)
1478 TAB3
"T const mag=sqrt<T>(m2);" NL
1479 TAB3
"return mag;" NL
1482 defined(g
,d
,"mag",suffix
);
1486 normalize( output_file
& out
, int d
, char const * suffix
)
1488 header_vr_va(out
,d
,"normalized");
1489 out
.require_include(INCLUDE_MATH
);
1490 out
.require_include(INCLUDE_THROW_EXCEPTION
);
1491 out
.require_include(INCLUDE_ERROR
);
1492 out
.require_include(INCLUDE_DEDUCE_V
);
1493 std::ostream
& g
=out
.stream();
1496 TAB3
"typedef typename vec_traits<A>::scalar_type T;" NL
1498 for( int i
=0; i
!=d
; ++i
)
1499 g
<<TAB3
"T const a"<<i
<<"=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1500 g
<<TAB3
"T const m2=";
1501 for( int i
=0; i
!=d
; ++i
)
1509 TAB3
"if( m2==scalar_traits<typename vec_traits<A>::scalar_type>::value(0) )" NL
1510 TAB4
"BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());" NL
1511 TAB3
"T const rm=scalar_traits<T>::value(1)/sqrt<T>(m2);" NL
1512 TAB3
"typedef typename deduce_vec<A>::type R;" NL
1515 for( int i
=0; i
!=d
; ++i
)
1516 g
<<TAB3
"vec_traits<R>::template write_element<"<<i
<<">(r)=a"<<i
<<"*rm;" NL
;
1524 TAB3
"using ::boost::qvm::normalized;" NL
1527 TAB2
"template <class A>" NL
1528 TAB2
"BOOST_QVM_INLINE_OPERATIONS" NL
1529 TAB2
"typename enable_if_c<" NL
1530 TAB3
"vec_traits<A>::dim=="<<d
<<"," NL
1531 TAB3
"void>::type" NL
1532 TAB2
<<"normalize( A & a )" NL
1534 TAB3
"typedef typename vec_traits<A>::scalar_type T;" NL
1536 for( int i
=0; i
!=d
; ++i
)
1537 g
<<TAB3
"T const a"<<i
<<"=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1538 g
<<TAB3
"T const m2=";
1539 for( int i
=0; i
!=d
; ++i
)
1547 TAB3
"if( m2==scalar_traits<typename vec_traits<A>::scalar_type>::value(0) )" NL
1548 TAB4
"BOOST_QVM_THROW_EXCEPTION(zero_magnitude_error());" NL
1549 TAB3
"T const rm=scalar_traits<T>::value(1)/sqrt<T>(m2);" NL
1551 for( int i
=0; i
!=d
; ++i
)
1552 g
<<TAB3
"vec_traits<A>::template write_element<"<<i
<<">(a)*=rm;" NL
;
1554 defined(g
,d
,"normalize",suffix
);
1558 dot( output_file
& out
, int d
, char const * suffix
)
1560 header_sr_va_vb(out
,d
,"dot");
1561 out
.require_include(INCLUDE_DEDUCE_S
);
1562 out
.require_include(INCLUDE_STATIC_ASSERT
);
1563 std::ostream
& g
=out
.stream();
1566 TAB3
"typedef typename vec_traits<A>::scalar_type Ta;" NL
1567 TAB3
"typedef typename vec_traits<B>::scalar_type Tb;" NL
1568 TAB3
"typedef typename deduce_scalar<Ta,Tb>::type Tr;" NL
1570 for( int i
=0; i
!=d
; ++i
)
1571 g
<<TAB3
"Ta const a"<<i
<<"=vec_traits<A>::template read_element<"<<i
<<">(a);" NL
;
1572 for( int i
=0; i
!=d
; ++i
)
1573 g
<<TAB3
"Tb const b"<<i
<<"=vec_traits<B>::template read_element<"<<i
<<">(b);" NL
;
1574 g
<<TAB3
"Tr const dot=";
1575 for( int i
=0; i
!=d
; ++i
)
1583 TAB3
"return dot;" NL
1586 defined(g
,d
,"dot",suffix
);
1598 swizzle_impl( std::ostream
& g
, int d
, swizzle_pair
const (&ids
)[N
], std::vector
<int> const & initial_count
)
1601 std::vector
<int> count(initial_count
);
1602 for( char const * const ref_id
[2] = { " const &", " &" };; )
1605 for( int i
=0; i
!=d
; ++i
)
1606 max_dim
=std::max(max_dim
,ids
[count
[i
]-1].idx
);
1610 TAB2
"BOOST_QVM_INLINE_TRIVIAL" NL
1611 TAB2
"qvm_detail::sw01_<";
1612 for( int k
=0; k
!=d
; ++k
)
1613 g
<<(k
?",":"")<<"qvm_detail::swizzle_idx<"<<ids
[count
[k
]-1].idx
;
1614 for( int k
=0; k
!=d
; ++k
)
1619 for( int k
=0; k
!=d
; ++k
)
1621 char f
=ids
[count
[k
]-1].ch
;
1622 assert(f
>='0' && f
<='9');
1628 TAB3
"return *reinterpret_cast<qvm_detail::sw01_<";
1629 for( int k
=0; k
!=d
; ++k
)
1630 g
<<(k
?",":"")<<"qvm_detail::swizzle_idx<"<<ids
[count
[k
]-1].idx
;
1631 for( int k
=0; k
!=d
; ++k
)
1634 " > const *>(qvm_detail::get_null());" NL
1638 for( int rfid
=0; rfid
<2; ++rfid
)
1640 for( int scalar
=0; scalar
!=2; ++scalar
)
1642 if( scalar
&& max_dim
>0 )
1646 TAB2
"template <class S>" NL
1647 TAB2
"BOOST_QVM_INLINE_TRIVIAL" NL
1648 TAB2
"typename enable_if_c<" NL
1649 TAB3
"is_scalar<S>::value," NL
1650 TAB3
"qvm_detail::sws_<S,";
1653 TAB2
"template <class V>" NL
1654 TAB2
"BOOST_QVM_INLINE_TRIVIAL" NL
1655 TAB2
"typename enable_if_c<" NL
1656 TAB3
"is_vec<V>::value && vec_traits<V>::dim>="<<max_dim
+1<<"," NL
1657 TAB3
"qvm_detail::sw_<V,";
1658 for( int k
=0; k
!=d
; ++k
)
1659 g
<<(k
?",":"")<<"qvm_detail::swizzle_idx<"<<ids
[count
[k
]-1].idx
;
1660 for( int k
=0; k
!=d
; ++k
)
1662 g
<<" >"<<ref_id
[rfid
]<<">::type" NL TAB2
;
1663 for( int k
=0; k
!=d
; ++k
)
1665 char f
=ids
[count
[k
]-1].ch
;
1666 if( !k
&& f
>='0' && f
<='9' )
1672 "( S"<<ref_id
[rfid
]<<" a )" NL
1674 TAB3
"return reinterpret_cast<qvm_detail::sws_<S,";
1677 "( V"<<ref_id
[rfid
]<<" a )" NL
1679 TAB3
"return reinterpret_cast<qvm_detail::sw_<V,";
1680 for( int k
=0; k
!=d
; ++k
)
1681 g
<<(k
?",":"")<<"qvm_detail::swizzle_idx<"<<ids
[count
[k
]-1].idx
;
1682 for( int k
=0; k
!=d
; ++k
)
1685 " >"<<ref_id
[rfid
]<<">(a);" NL
1690 for( j
=0; j
!=d
; ++j
)
1694 count
[j
]=initial_count
[j
];
1701 swizzle( output_file
& out
, int d
)
1704 out
.require_include(INCLUDE_INLINE
);
1705 out
.require_include(INCLUDE_SWIZZLE_TRAITS
);
1706 out
.require_include(INCLUDE_ENABLE_IF
);
1707 std::ostream
& g
=out
.stream();
1708 swizzle_pair
const swizzle_ids
[6] =
1717 std::vector
<int> initial_count(d
,6);
1718 swizzle_impl(g
,d
,swizzle_ids
,initial_count
);
1721 command_line_options
1722 parse_command_line( int argc
, char const * argv
[] )
1727 char const * const * const argv
;
1730 next( int argc
, char const * argv
[] ):
1736 operator()( int & i
) const
1740 BOOST_THROW_EXCEPTION(bad_command_line() << cmd_arg(argv
[i
-1]));
1743 } next_token(argc
,argv
);
1744 command_line_options r
;
1745 for( int i
=1; i
!=argc
; ++i
)
1746 if( argv
[i
][0]=='-' )
1748 char const * arg
=argv
[i
];
1749 if( arg
==std::string("-od") )
1750 r
.output_directory
=next_token(i
);
1751 else if( arg
==std::string("-con") )
1754 BOOST_THROW_EXCEPTION(bad_command_line() << cmd_arg(arg
));
1760 gen( int argc
, char const * argv
[] )
1762 command_line_options opt
=parse_command_line(argc
,argv
);
1763 for( int d
=2; d
!=5; ++d
)
1768 sprintf(buf
,INCLUDE_MAT_ASSIGN
,d
);
1769 f
.require_include(buf
);
1771 mr_op_ma_mb_same_size(f
,d
,d
,"operator+","+","mm");
1772 mr_op_ma_mb_same_size(f
,d
,1,"operator+","+","mm");
1773 mr_op_ma_mb_same_size(f
,1,d
,"operator+","+","mm");
1774 mr_op_ma_mb_same_size(f
,d
,d
,"operator-","-","mm");
1775 mr_op_ma_mb_same_size(f
,d
,1,"operator-","-","mm");
1776 mr_op_ma_mb_same_size(f
,1,d
,"operator-","-","mm");
1777 ma_op_ma_mb_same_size(f
,d
,d
,"operator+=","+=","mm");
1778 ma_op_ma_mb_same_size(f
,d
,1,"operator+=","+=","mm");
1779 ma_op_ma_mb_same_size(f
,1,d
,"operator+=","+=","mm");
1780 ma_op_ma_mb_same_size(f
,d
,d
,"operator-=","-=","mm");
1781 ma_op_ma_mb_same_size(f
,d
,1,"operator-=","-=","mm");
1782 ma_op_ma_mb_same_size(f
,1,d
,"operator-=","-=","mm");
1783 mr_op_ma_sb(f
,d
,d
,"operator*","*","ms");
1784 mr_op_sa_mb(f
,d
,d
,"operator*","*","sm");
1785 mr_op_ma_sb(f
,d
,1,"operator*","*","ms");
1786 mr_op_sa_mb(f
,d
,1,"operator*","*","sm");
1787 mr_op_ma_sb(f
,1,d
,"operator*","*","ms");
1788 mr_op_sa_mb(f
,1,d
,"operator*","*","sm");
1789 ma_op_ma_sb(f
,d
,d
,"operator*=","*=","ms");
1790 ma_op_ma_sb(f
,d
,1,"operator*=","*=","ms");
1791 ma_op_ma_sb(f
,1,d
,"operator*=","*=","ms");
1792 mr_op_ma_sb(f
,d
,d
,"operator/","/","ms");
1793 mr_op_sa_mb(f
,d
,d
,"operator/","/","sm");
1794 mr_op_ma_sb(f
,d
,1,"operator/","/","ms");
1795 mr_op_sa_mb(f
,d
,1,"operator/","/","sm");
1796 mr_op_ma_sb(f
,1,d
,"operator/","/","ms");
1797 ma_op_ma_sb(f
,d
,d
,"operator/=","/=","ms");
1798 ma_op_ma_sb(f
,d
,1,"operator/=","/=","ms");
1799 ma_op_ma_sb(f
,1,d
,"operator/=","/=","ms");
1800 mr_convert_to_ma(f
,d
,d
,"m");
1801 mr_convert_to_ma(f
,d
,1,"m");
1802 mr_convert_to_ma(f
,1,d
,"m");
1803 bool_eq_ma_mb(f
,d
,d
,"mm");
1804 bool_eq_ma_mb(f
,d
,1,"mm");
1805 bool_eq_ma_mb(f
,1,d
,"mm");
1806 bool_neq_ma_mb(f
,d
,d
,"mm");
1807 bool_neq_ma_mb(f
,d
,1,"mm");
1808 bool_neq_ma_mb(f
,1,d
,"mm");
1809 mr_op_ma(f
,d
,d
,"operator-","-","m");
1810 mr_op_ma(f
,d
,1,"operator-","-","m");
1811 mr_op_ma(f
,1,d
,"operator-","-","m");
1813 inverse_ma(f
,d
,"m");
1814 mr_mult_ma_mb(f
,d
,d
,d
,"mm");
1815 ma_mult_ma_mb(f
,d
,"mm");
1816 mr_mult_ma_mb(f
,d
,d
,1,"mm");
1817 mr_mult_ma_mb(f
,1,d
,d
,"mm");
1818 f
.dump("mat_operations"+to_string(d
)+".hpp");
1821 for( int d
=2; d
!=5; ++d
)
1824 ma_assign_ma_mb(f
,d
,d
,"mm");
1825 ma_assign_ma_mb(f
,d
,1,"mm");
1826 ma_assign_ma_mb(f
,1,d
,"mm");
1827 f
.dump("mat_assign"+to_string(d
)+".hpp");
1830 for( int d
=2; d
!=5; ++d
)
1835 sprintf(buf
,INCLUDE_VEC_ASSIGN
,d
);
1836 f
.require_include(buf
);
1838 vr_op_va_vb_same_size(f
,d
,"operator+","+","vv");
1839 vr_op_va_vb_same_size(f
,d
,"operator-","-","vv");
1840 va_op_va_vb_same_size(f
,d
,"operator+=","+=","vv");
1841 va_op_va_vb_same_size(f
,d
,"operator-=","-=","vv");
1842 vr_op_va_sb(f
,d
,"operator*","*","vs");
1843 vr_op_sa_vb(f
,d
,"operator*","*","sv");
1844 va_op_va_sb(f
,d
,"operator*=","*=","vs");
1845 vr_op_va_sb(f
,d
,"operator/","/","vs");
1846 va_op_va_sb(f
,d
,"operator/=","/=","vs");
1847 vr_convert_to_va(f
,d
,"v");
1848 bool_eq_va_vb(f
,d
,"vv");
1849 bool_neq_va_vb(f
,d
,"vv");
1850 vr_op_va(f
,d
,"operator-","-","v");
1855 f
.dump("vec_operations"+to_string(d
)+".hpp");
1858 for( int d
=2; d
!=5; ++d
)
1861 va_assign_va_vb(f
,d
,"vv");
1862 f
.dump("vec_assign"+to_string(d
)+".hpp");
1865 for( int d
=2; d
!=5; ++d
)
1868 vr_mult_ma_vb(f
,d
,d
,"mv");
1869 vr_mult_va_mb(f
,d
,d
,"vm");
1870 f
.dump("vec_mat_operations"+to_string(d
)+".hpp");
1876 f
.dump("swizzle2.hpp");
1881 f
.dump("swizzle3.hpp");
1886 f
.dump("swizzle4.hpp");
1892 main( int argc
, char const * argv
[] )
1899 std::ifstream::failure
& )
1901 std::cerr
<< "Failed to write generated output file" << std::endl
;
1906 std::cerr
<< "Unexpected exception" << std::endl
<< boost::current_exception_diagnostic_information();