Investigate GMP integrationBRL-CAD
Status: ClosedTime to complete: 72 hrs Mentors: Sean, Matt S.Tags: C, C++, GMP, precision, math

BRL-CAD uses a fastf_t typedef for most all math operations that is usually a "double" floating point type. We would like to provide the option for resorting to exact arithmetic if possible by merely redefining fastf_t to a C++ type sufficiently overloaded to behave the same. You should be proficient with C++ operator overloading to take this task on.

This task involves testing compilation with a C++ class with overloaded operators such that vmath macro calls still work as well as a sampling of LIBBN API function calls without major changes to the original code. A perfect example case study would be creating the class then testing whether bn_dist_pt3_pt3() and bn_mat_determinant() compute correctly for values that cannot be exactly represented with floating point arithmetic.

References:

Code:

  • include/vmath.h
  • include/bn.h
Uploaded Work
File name/URLFile sizeDate submitted
mpf_class2.tar.7z47.4 KBNovember 29 2012 23:40 UTC
Comments
reikazeon November 26 2012 22:55 UTCTask Claimed

I would like to work on this task.

Sean on November 26 2012 23:04 UTCTask Assigned

This task has been assigned to reikaze. You have 72 hours to complete this task, good luck!

reikazeon November 28 2012 04:19 UTCCmake

Sorry that it's a bit late in the game, but all day I've been stumped over how to add a new target and files and link GMP in the Makefile (through creating Cmake files or whatever)... would it be too much trouble to give me a tip?

reikazeon November 28 2012 04:36 UTCWriting a C++ wrapper

But even before that, the code is predominatly C, so I'm having trouble including the header with my C++ class...

Sean on November 28 2012 05:07 UTCit's not late

It's not late in the game.  We'll gladly add more time if you need it.  You're not expected to do this completely alone.  If you join our IRC channel, you might even get some interactive help.


So this task is all about proof of concept, not full-blown integration with our project.


First compile and install BRL-CAD.  Then, make a simple test.cpp that looks something like this:


#include "bu.h"


#include "bn.h"


int main(int ac, char *av[])


{


  vect_t v = VINIT_ZERO;


  bu_log("hello vector: %lf, %lf, %lf\n", V3ARGS(v));


  return 0;


}


Then compile that manually.  You'll need to specify where to find the headers and libraries, but it'll be something like this:  g++ test.cpp -I/usr/brlcad/dev-7.22.1/include -L/usr/brlcad/dev-7.22.1/lib -lbu -lbn


Get that to work and make sure you understand everything.  Then try making it call bn_dist_pt3_pt3() .. then try changing the fastf_t typedef to your type.  Be as minimal as possible.

Sean on November 28 2012 05:07 UTCDeadline extended

The deadline of the task has been extended with 2 days and 0 hours.

reikazeon November 29 2012 03:01 UTCSo far...

I've written a wrapper that does basic arithmetic but afils bn_dist_pt3_pt3.


 


The wrapper inherits from GMP's mpf_class. But since mpf_class has a nontrivial copy constructor, fastf_t becomes unusuable with printf (and bu_log).


 


Would it be better that I instead went for a wrapper around a member mpf_t and the C functions?

Sean on November 29 2012 03:59 UTCshow?

Can you show what you have so far?  Were you able to get bn_dist_pt3_pt3() working without your wrapper?


No worries about not being compatible with printf() -- that's what macros like V3ARGS() are for.  We'd change the guts or supply a [] operator to return a compatible type.


 

reikazeon November 29 2012 20:45 UTCThe wrapper so far

Well, I already changed my wrappers so it uses mpf_t instead of mpf_class so it works with the existing code fine.


bn_dist_pt3_pt3 worked before with a result of 11.1933 but now I get a result of -nan.


Here is my code:


http://pastebin.com/phx6qgNB


Note I used the USEGMP macro to toggle use of my wrapper as fastf_t.


 

reikazeon November 29 2012 20:53 UTCA clue

Here is the output of main:
****************************************


double constructor with operand 0.000000


double constructor with operand 0.000000


double constructor with operand 0.000000


hello vector: 0.000000, 0.000000, -nan


double constructor with operand 1.999000


double constructor with operand 13.515660


mpf constructor with operand 1821066134


1.999/13.51566=0.147903


double constructor with operand 0.023000


mpf constructor with operand 1821066134


1.999 + 0.023=2.022


double constructor with operand 0.023500


mpf constructor with operand 1821066134


1.999 - 0.0235=1.9755


double constructor with operand 13.515660


mpf constructor with operand 1821066134


1.999* 13.51566=27.0178


mpf constructor with operand 1821066134


1.999² = 3.996


double constructor with operand 3.555560


b = 5.55456; a = 5.55456


double b = 5.55456


double constructor with operand 1.200000


double constructor with operand 1.500000


si constructor with operand -1


si constructor with operand -9


si constructor with operand -3


si constructor with operand -2


Distance betewen v1 and v2: -nan


******************************** ****************


I only just noticed that the hu_log statement prints 0,0,-nan....

reikazeon November 29 2012 21:32 UTCI remember

I did something and now it's no longer 0,0,-nan...


I remember now that the main reason that I used mpf_class2 in the first place was to have a copy constructor. However, I then wouldn't be able to use the type with printf.


Which do you think is more important: printf or a copy constructor?

reikazeon November 29 2012 21:37 UTCGoing with mpf_class

Sorry for being so indecisive. I think I'll just forge on with extending mpf_class. 


 


Still not sure what to do about bn_point3d etc. though.

Sean on November 29 2012 23:16 UTCone problem at a time...

My inclination is as expressed earlier, that we won't be feeding fastf_t to printf() so that's not an issue.  They'll be expanded with V3ARGS() which could be changed to extract each field in floating point format, or have a [] operator defined that extracts them.


Since you're putting substantial fantastic effort into this, we should break the task up into multiple tasks.  If you can get it to print something other than 0,0,-nan then submit that and we'll create another task so you can keep going on the next "exploration", whether with mpf_class or mpf_class2.

reikazeon November 29 2012 23:26 UTCIt does 0,0,0 right now

I don't think I would have had the chance to put this much effort into this if you hadn't extended the deadline, hahaha.


Here is what I have so far:


http://pastebin.com/3jiDDCSA

Sean on November 29 2012 23:30 UTCupload

Upload what you have and we can call this done since you did give a proof of concept.  The next step would be setting the vector to 1/3, 1/3, 1/3 and/or 0.1, 0.1, 0.1, but we can set up another task to explore that.

reikazeon November 29 2012 23:41 UTCThanks for your help and guidance!

I guess I overdid it, but at least I learnt about the GMP!

reikazeon November 29 2012 23:58 UTCReady for review

The work on this task is ready to be reviewed.

Sean on November 30 2012 07:15 UTCTask Closed

Congratulations, this task has been completed successfully.

Sean on December 11 2012 04:42 UTCcontinuation

A continuation of this task as just posted: http://www.google-melange.com/gci/task/view/google/gci2012/8013222