problem with C math lib.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

problem with C math lib.

9,315 Views
hotdog
Contributor I
I am using codewarrior development studio for HC(S)08 special editon(V3.1) to develop a proj. used by  MC68HC908JL3.
when i use #include <math.h>  and make it, the errors & warrnings window indicates that link error:
      symbol  sqrt              in file ...\main.c.o is undefined and
       symbol  _DUFLOAT in file ...\main.c.o is undefined.
i wonder i can use C math lib.? and if i want to calcaulate sqrt(x), how to do it?
 

Message Edited by hotdog on 04-13-200602:25 AM

Labels (1)
0 Kudos
11 Replies

1,205 Views
hotdog
Contributor I
thanks for CrasyCat,Technoman64 and rocco's help!
as your directions, i just created a project by wizard to use floating point arithmetic and worte a prog. as follows:

#include <hidef.h>
#include <MC68HC908JL3.h>
#include <math.h>
 unsigned char c;
 float y;
void main(void) {
  y=sqrt(16);
  c=16;
...
}
after passed compile and make,  i run "start" or run to "c=16;" line, True-Time Simulaor & Real-Time Debugger window indicates that program stops at float frexpf (float x, int *i){...} in source window and appears "attempt to push or pop with sp out of allowed range" in command window.

i wonder how i can get it through?additionally i want to calcaulate an integer square-root in the proj., i would be very appreciated if rocco would like to send the 24-bit integer square-root routine to my emailbox (xiaojunj_sh@yahoo.com.cn).

Message Edited by hotdog on 04-14-200601:25 AM

Message Edited by hotdog on 04-14-200601:26 AM

0 Kudos

1,205 Views
CompilerGuru
NXP Employee
NXP Employee
Did you try to increase the stack size (in the prm file)?
My guess: you did run out of stack space.

If you only need an integral sqare root, then dont use the ANSI floaing point sqrt function. It is way too expensive, especially for a small chip like a JL3.
0 Kudos

1,205 Views
rocco
Senior Contributor II
Hi, Hotdog:

I sent you two files. One of them contains the square-root subroutine, but it uses some macros and definitions from my 8/16/24/32-bit integer math library. So I included the library as well.

You probably don't need much from the library, but all of my math routines use a "pseudo-accumulator", which is actually just four bytes of ram, defined as ACCUM3, ACCUM2, ACCUM1 and ACCUM0. These are defined at the beginning of the math library.

The square-root routine is not an approximation, it produces an exact root with truncation (any fractional portion of the root is not computed). It uses no multiplies or divides, just shift and subtract (but a LOT of shifts). I could easily be converted to 32-bit or 16-bit, as well as optimized for HC08.

Hope it helps.
0 Kudos

1,205 Views
khtan
Contributor I
Hi Sir,
 
     I had the same problem that can't compile the file using math.h as i didn't set the project to floating point. I tried using the wizard and enable the floating point support, it is working. However, when i try to do the same things by enabling the floating point option in one of the SMAC project. It doesn't work. I double check the Target Setting Panel > Compiler for HC08 the '-Fd' is included in the setting already but i still can't compile the project. Am i doing the right way to enable the floating point support for my existing project that doesn't have floating point support initially when i create it. Hope to hear some advice from you all.
 
Regards,
 
0 Kudos

1,205 Views
Capitao_Caverna
Contributor I

Hello Sir,

 

I've tried to use the function exp and log but so far the simulator gives a message "Error: Attempt to push or pop with SP out of allowed range". I've changed the SP initial value, then there is an error in the calculation. Also defined _MSL_FLOATING_POINT 1 but not worked yet. Any help would be appreciated. Thanks.

0 Kudos

1,205 Views
Technoman64
Contributor III
I believe you will need to change the C libarary file that is linked with the project. To use floating point math you must use the correct library file. This is automatically selected when you use the project wizard to setup a new project.
 
 
0 Kudos

1,205 Views
rocco
Senior Contributor II
Hi, Hotdog:

Do you need integer Square-root or floating point Square-root?

I have an old 24-bit integer square-root routine that was originally written for HC05. It will run on HC08 as is, but there is probably room to optimize it for HC08. Let me know if you need it.

Message Edited by rocco on 04-13-200603:30 PM

0 Kudos

1,205 Views
ultrasniper
Contributor I

Can You send me the square root routine? Thanks in advance info@vn-amps.com.ar 

0 Kudos

1,205 Views
rocco
Senior Contributor II
Hi Ultrasniper,

I emailed some files that should get you a square-root routine for the S08.

The square-root routine, "SQUAROOT.SA5", was written for the HC05 many years ago. I never optimized it for the S08, because I didn't need it anymore.

It uses macros from my math library, so I included the original HC05 version "MATH.SA5", and my updated S08 version "Math.sa8". You should be able to get a working square-root from there.

You may have to change the file extensions (Codewarrior may insist on '.asm') and update the syntax (Motorola used to modify the assembler syntax every few years) to get is to assemble. It was originally written for Motorola's PASM assembler.

Good Luck.
Message Edited by rocco on 2009-12-29 12:40 PM
0 Kudos

1,205 Views
bigmac
Specialist III
Hello,
 
I pulled this C function from an earlier thread.  It is capable of handling a 16-bit integer value, and returning the square root to the nearest integer.  It uses the Newton algorithm where each calculation within the do-while loop provides a closer approximation to the square root value.
 
word sqrt( word val)
{
   word temp, x = 4096;
 
   do {
      temp = x;
      x = (val/x + x + 1)/2;
   } while (x != temp);
   return x;
}
 
Regards,
Mac
 
0 Kudos

1,205 Views
CrasyCat
Specialist III

Hello

Did you specify you intend to use math library when you created the project?
Did you create your project using the project wizard?

Default settings for HC08 doe not include floating point arithmetic support as this generate a big code overhead.

I would recommend you to create a project from the wizard and specify in the C/C++ Panel (the last panel when you create a project) that you want to use floating point arithmetic.
Then add your source file to the project and it should be working.

CrasyCat

0 Kudos