question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SLEIGH: How to Support Sin, Cos, and Sqrt?

See original GitHub issue

I would like to close https://github.com/NationalSecurityAgency/ghidra/issues/1730. I believe I have disassembly for fsca and fsrra working. I have no clue to how to implement the pseudo code (http://www.shared-ptr.com/sh_insns.html):

fsca:

void FSCA (int n)
{
  if (FPSCR_PR != 0)
    undefined_operation ();
  else
  {
    float angle;
    long offset = 0x00010000;
    long fraction = 0x0000FFFF;

    set_I ();
    fraction &= FPUL;  // extract sub-rotation (fraction) part
    angle = fraction;  // convert to float
    angle = 2 * M_PI * angle / offset;  // convert to radian
    FR[n] = sin (angle);
    FR[n+1] = cos (angle);
    PC += 2;
  }
}

fsrra:

void FSRRA (int n)
{
  if (FPSCR_PR != 0)
    undefined_operation ();
  else
  {
    PC += 2;
    clear_cause();

    switch (data_type_of (n))
    {
    case NORM:
      if (sign_of (n) == 0)
      {
        set_I ();
        FR[n] = 1 / sqrt (FR[n]);
      }
      else
        invalid (n);
      break;

    case DENORM:
      if (sign_of (n) == 0)
        fpu_error ();
      else
        invalid (n);
      break;

    case PZERO:
    case NZERO:
      dz (n, sign_of (n));
      break;

    case PINF:
      FR[n] = 0;
      break;

    case NINF:
      invalid (n);
      break;

    case qNAN:
      qnan (n);
      break;

    case sNAN:
      invalid (n);
      break;
    }
  }
}

Any advice on how to tackle this? I found references in ia.sinc for pcodeops for cos. I don’t quite understand it. Thanks in advance.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nneonneocommented, Apr 26, 2022

You could implement it as a pcodeop, which is effectively just an opaque “intrinsic” operation. It gets decompiled as simply a function call.

Declare the pcodeop somewhere in your slaspec as

define pcodeop sin;

Then in your constructor just do

r0 = sin(r0);

The “functions” thus declared by pcodeop can take any arguments and return anything.

See section “7.7.1.8. User-Defined Operations” in the SLEIGH manual, which says the following:

7.7.1.8. User-Defined Operations

Any identifier that has been defined as a new p-code operation, using the define pcodeop statement, can be invoked as an operator using functional syntax. The SLEIGH compiler assumes that the operator can take an arbitrary number of inputs, and if used in an expression, the compiler assumes the operation returns an output. Using this syntax of course generates the particular p-code operation reserved for the identifier.

define pcodeop arctan; ... :atan r1,r2 is opcode=0xa3 & r1 & r2 { r1 = arctan(r2); }

1reaction
GhidorahRexcommented, Apr 26, 2022

You also need to take care with how you handle the Fr[N+1] for the cos.

Your best bet here would be to add an additional token and attach statement: FRN_1 = (8,11) ... attach variables [FRN_1] [fr1 fr2 fr3 fr4 fr5 fr6 fr7 fr8 fr9 fr10 fr11 fr12 fr13 fr14 fr15 _ ]; FRN_1t: FRN_1 is FRN_1 { export FRN_1; } :fsca fpul_t, FRN_0t is fpul_t & FRN_0t & FRN_1t ...

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Circle Trigonometry - Sin Cos Tan - Radians & Degrees
This trigonometry / precalculus video tutorial review explains the unit circle and the basics of how to memorize it. It provides the angles ......
Read more >
Easy trick to remember sin, cos and tan values! - YouTube
Learn a quick and easy way to remember key sin, cos and tan values. I've found this super helpful in lots of exams...
Read more >
Sine Cosine Tangent Explained - sin cos tan sec csc cot
This trigonometry video tutorials explains how to use the sine cosine and tangent function as it relates to right triangles and SOHCAHTOA.
Read more >
Which is more efficient for sines and cosines? Sin and Cos or ...
If I want to calculate a sin and a cos, is it cheaper to calculate a sin and a cos, or calculate a...
Read more >
Cosine, sine and tangent of π/6 and π/3 (video) - Khan Academy
But to help us there, I'm going to give us a little bit of a ... So sine of pi over three is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found