home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 634 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.6 KB  |  244 lines

  1. Path: nntp.teleport.com!sschaem
  2. From: sschaem@teleport.com (Stephan Schaem)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: PPC compilers
  5. Date: 9 Jan 1996 22:18:47 GMT
  6. Organization: Teleport - Portland's Public Access (503) 220-1016
  7. Message-ID: <4cupk7$dmn@maureen.teleport.com>
  8. References: <john.hendrikx.40ka@grafix.xs4all.nl> <jasonb.820051107@cs.uwa.edu.au> <4c9i2l$h3i@sunsystem5.informatik.tu-muenchen.de> <4ck47h$g07@maureen.teleport.com> <19960106.4EE928.CF59@sisyphus.demon.co.uk> <4cokkg$415@maureen.teleport.com> <19960107.533250.14585@sisyphus.demon.co.uk> <4cqrti$f6u@maureen.teleport.com> <jasonb.821173879@cs.uwa.edu.au>
  9. NNTP-Posting-Host: kelly.teleport.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jason S Birch (jasonb@cs.uwa.edu.au) wrote:
  13. : sschaem@teleport.com (Stephan Schaem) writes:
  14.  
  15. : > polymorphism?
  16.  
  17. : > int a,b,c;
  18.  
  19. : > ...
  20. : > a = b /c;
  21. :         ^
  22. : > a *= result;
  23. :     ^
  24. : The "/" and "*" are polymorphic - you can use them in any mathematical
  25. : expression, without caring whether the left and right sides are chars,
  26. : shorts, longs, floats, doubles, or any equivalent type. If they were
  27. : not polymorphic, you might have, for example:
  28. : a int= b int/ c;
  29. : a int*= result;
  30.  
  31.  I wanted to show that if you dont know your variable type range you
  32.  might write code that wont compile (here, the user go back and cast.. or
  33.  realize he was using the wrong type for his kind of operation, or is waistfull)
  34.  
  35. : This is essentially what you have in assembler. Note that with C++,
  36. : you can overload "+", "-", "*", "/", "=", etc, to work with any custom
  37. : classes as well, so you could have, for example:
  38.  
  39.  At that level in asm, you will probably use an 'object' structure that 
  40.  include those function.
  41.  
  42. : vector a,b,c;
  43. : real r;
  44. : ...
  45. : a = b + c;
  46. : r = a * b;
  47. : cout << "Values are: " << a << ", " << r << ".\n";
  48.  
  49.     movea.l    (GLOBAL_VECTOR,a4),a6    ;the type we are working on
  50.  
  51. ;with macro
  52.     SET    GLOBAL_a_VECTOR, GLOGAL_b_VECTOR, GLOGAL_c_VECTOR
  53.     CALL    VECTOR_Add
  54.  
  55. ;without (variable type extenstion omited)
  56.     lea    (GLOBAL_result,a4),a0
  57.     lea    (GLOBAL_a,a4),a1
  58.     lea    (GLOBAL_b,a4),a2
  59.     jsr    (VECTOR_Mul,a6)
  60. ;or
  61.     pea    (GLOBAL_r_REAL,a4)
  62.     pea    (GLOBAL_result_VECTOR,a4)
  63.     jsr    (VECTOR_ToReal,a6)
  64.  
  65.  I will skyp the output.
  66.  
  67.  Not very elegant I must say... but I would code something like this
  68.  if you asked me: I have 3 VECTOR, a, b, c , I want a to be equal to b + c
  69.  and the real r to hold the result of a * b.
  70.  
  71.  C++ is way more elegant and usable at this level. But I would still know
  72.  r is a real, and that I'm working with vector.
  73.  
  74. : and have, say:
  75.  
  76. : Values are: [3,7,3], 67.
  77.  
  78. : as the output.
  79.  
  80. : > struct.float = a;
  81.  
  82. : This is polymorphism of "=", but it's also taking advantage of the
  83. : fact that C can convert an integer into a float.
  84.  
  85.  It doesn't hurt to tell explicitly the compiler that you actually
  86.  want to convert an int into a float by using the cast operator. I
  87.  assume you know at the time of writting that you are converting
  88.  an int into a float since this step is a logical part of the program. 
  89.  
  90. : > I dunno , I just find it puzzling to declar variable without giving
  91. : > a care of what its usage will be. You programing practice is VERY unwise...
  92.  
  93. : Your misunderstanding is that you think he means he has no idea of
  94. : what types each variable is, whereas what he's saying is he need have
  95. : no idea of how each type is actually implemented on a particular
  96. : machine/OS - all he has to do is know how those types are defined to
  97. : behave under ANSI C. In fact, if you want to write portable code, you
  98. : have to restrict your knowledge of behaviour of the types to that
  99. : which is guaranteed under ANSI C. Taking advantage of any other
  100. : information you might have (eg. endian-ness, number of bits) limits 
  101. : your portability.
  102.  
  103.  My understanding was has follow... After I declare my variable I can
  104.  forget their type (The type here beying a 16bit word). Ok, he can
  105.  forget its a 16bit word... but he cant forget that its a CHIPREG type
  106.  and that the CHIPREG type is 16bit, unless he never mix type operation.
  107.  
  108. : >: I know better than to declare a value as unsigned long when I'm
  109. : >: going to assign a clock_t to it.  And if I did, I'd lose the
  110. : >: fractional part of a floating-point clock_t, not the upper 32 bits.
  111.  
  112. : > You said yourself after peeking at the .h that clock_t was a ulong.
  113. : > But you know better then not using a ulong... ? Should I assume
  114. : > you alway use the largest data type available,double ? (What a waist)
  115.  
  116. : No, you should use clock_t! If you want a variable to use in functions
  117. : that expect a clock_t, you declare it thus:
  118.  
  119.  I had the idea clock_T was a structure member of type ulong.
  120.  
  121. : clock_t Stephens_var;
  122.  
  123. : You do *not* declare it as:
  124.  
  125. : ulong Stephens_var;
  126.  
  127. : even if you *know* that on your particular implementation, that's what
  128. : it really is, because that restricts your portability.
  129.  
  130.  I agree... you shouldn't even know clock_t is a ulong, just its
  131.  type description. 
  132.  
  133.  
  134. : > And you mean you would loose the fractional part of clock_t if you
  135. : > used ulong , not the upper 32bits? This make absolutly no sense since
  136. : > we are talking about interger types.(BTW copy 0x00000001 into a 2byte int,
  137. : > you will loose the upper 16bit... )
  138.  
  139. : You lose nothing if you declare it as clock_t.
  140.  
  141.  I didn't see clokc_t has a type, but a structure member of type ulong.
  142.  
  143. : > If the header file was programmed correctly it would not use ANSI data
  144. : > type directly.... 
  145.  
  146. : Why not? You should just know better than to look at it and take
  147. : advantage of that information.
  148.  
  149. : Incidentally, when I create a custom data type I want to keep private,
  150. : I provide a public header file similar to:
  151.  
  152. : typedef void *MyDataType;
  153.  
  154. : MyDataType *MDT_Create(...);
  155. : void MDT_Dispose(MyDataType *);
  156.  
  157.  If you look at my C projects you will find 
  158.  
  159.  *_PRV.h (function group name), *_PUB.h  , *_*.h (here the second * is the 
  160.  platform name)
  161.  
  162.  The PRV usually hold library private, PUB what the user is
  163.  allowed to know (mainly the function tags, flags, etc and prototyping),
  164.  the platform header define the private structure of the private header (PRG.h)
  165.  
  166.  but what I wanted to say is _PUB function prototyping is simple:
  167.  
  168. pvoid    GroupName_Function(TAGS);
  169.  
  170.  true for all...
  171.  
  172. : and inside the private header file, I actually define what MyDataType
  173. : is. This way, no-one can "accidentally" use the internal definition,
  174. : and I am free to change it at will, without having to worry about
  175. : those who *don't* know better than to look at it and take advantage of
  176. : the information.
  177.  
  178.  for me pvoid is a pointer to a struture with this alway true
  179.  
  180.  structure    STRUCTURE {
  181.     link lnk;
  182.     ...
  183.  
  184.  lnk hold an ID element...
  185.  
  186.  The ID hold the type of the structure... it can be of type
  187.  ID_ERROR, or whatever... probably a structure that hold
  188.  private, public and os element.
  189.  
  190. : > The way you get optimal speed is optimizing the optimal algorithim.
  191. : > If you stop at the algorithm you loose... Optimizing at this stage
  192. : > will give an exponential result on the work you done on the algorithm.
  193. : > And usually this stage is done in assembler.
  194.  
  195. : The problem is, do you want portable code? If yes, then you should be
  196.  
  197.  Yes, I want portable code.. and other time I want the fastest code
  198.  the machine can provide (To the point where it suffient and doesn't
  199.  overcome the effort).
  200.  
  201. : aware that what might be optimal for one particular CPU using one
  202. : particular compiler is not necessarily the same for another. For
  203.  
  204.  Very true... ex: I do not use self incrementing pointer on a mips box.
  205.  
  206. : example, scanning through an array and adding the contents using array
  207. : dereferencing and a moving pointer: On an 040 Amiga, the latter was
  208. : faster; using gcc on a Sun, both produced the same assembler code; and
  209. : using Metrowerks on a PowerMac, the former was actually quicker
  210. : because the pointer method used post-incrementing (ie. sum += *ptr++)
  211.  
  212.  Now, what I would probably write on <= R4K box or CPU that has free
  213.  immediate index addresing mode:
  214.  
  215.  sum += ptr[0];
  216.  sum += ptr[1];
  217.  sum += ptr[2];
  218.  sum += ptr[3];
  219.  sum += 4;
  220.  
  221.  On a 680x0 amiga:
  222.  
  223.  sum += *ptr++;
  224.  sum += *ptr++;
  225.  sum += *ptr++;
  226.  sum += *ptr++;
  227.  
  228. : and the compiler wasn't smart enough to realize it didn't need to
  229. : store the pre-incremented value and used an extra register to store
  230. : it. The moral? Don't make assumptions about what is an optimization,
  231. : and what isn't, unless you test it on every combination you plan to
  232. : run it on.
  233.  
  234.  I do not assum... But if I do and care about portability of the 
  235.  non OS specific code I would make sure to include SYSTEM flag.
  236.  and compile the right version of the code I tryed to optimize at the C 
  237.  level.
  238.  
  239.  For obvious resaon I fall back to asm for the optimum optimized part,
  240.  usually a mirror or the 'optmized' C code.
  241.  
  242.  Stephan
  243.  
  244.