Recent Posts

Pages: 1 ... 5 6 [7] 8 9 10
Transcendental functions actually can be computed (i.e. approximated up to an arbitrary desired precision) using only those basic operations; efficient algorithms to do so are not particularly simple though, and are best left to be implemented by experts in that area.  As such, you should probably just use a library that has implementations for these.  A library written (in C) to compute these using SIMD operations can be found at
Using NASM / How do you overide the address size in NASM when using movsb?
« Last post by ben321 on December 16, 2023, 08:25:18 AM »
For example, in 16 bit mode, the movsb command looks only at the si and di registers (first 16 bits of the esi and edi registers). However I can use "db 0x67" to manually insert the address size override byte in front of the movsb opcode. This will force it to use the entire 32bits of the esi and edi registers. However, I'm wondering if there's a proper instruction for this. I've tried "movsb long", and "movsb far". And so far I can't find any instruction that will cause NASM to write the 0x67 byte in front of the opcode for movsb.
There are no math instructions other then the four basic arithmetic functions (add, substraction, multiplication and division) with SSE (or almost all SIMD in other processors as well).

This is not a bad thing, since fsin and fcos are very slow (50-120 cycles).

Then how does Intel expect you to run any useful algorithms (which often require transcendental functions like sine or cosine) with the XMM registers (what they want you to use when in 64bit mode)? You can't do sine and cosine simply by using a combination of add, subtract, multiply, and divide.
There are no math instructions other then the four basic arithmetic functions (add, substraction, multiplication and division) with SSE (or almost all SIMD in other processors as well).

This is not a bad thing, since fsin and fcos are very slow (50-120 cycles).
Programming with NASM / What are the SIMD equivalents to x87 transcendental functions?
« Last post by ben321 on December 13, 2023, 03:22:55 PM »
For example, with x87 floating point registers, I can use fpsin to calculate the sine of an angle. Now with 64bit CPUs, the recommendation is to stop using the x87 instructions, and use the SIMD instructions instead, which operate on the XMM registers. But I have a problem. What are the SIMD instructions for transcendental functions like sine? Do they even exist? I can't find any proof of their existence. One thing is certain though, is that even if they exist, I can't find anything on the internet about them.
Example Code / Re: Fibonacci, with bigger numbers
« Last post by andyz74 on November 28, 2023, 06:45:16 PM »
I am thankful for your informations and ideas! :-)

Nevertheless, due to my historic age, I teached myself ages ago Pascal and a bit of Assembler. I cannot learn new programing languages, my brain is full. ;-)
And in addition, my primary goal is teaching myself, how to handle (in Assembler and Pascal) bigger numbers as normally possible.   The above seems to be a good way for myself to understand all this.

Nevertheless, many thanks for your thoughts.  :)
Example Code / Re: Fibonacci, with bigger numbers
« Last post by fredericopissarra on November 28, 2023, 05:30:49 PM »
To be fair, using 'bc' this way isn't correct as well... Here's a simple code using 'libgmp' to illustrate:

Code: [Select]
// fib.c
//    cc -O2 -o fib fib.c -lgmp
#include <gmp.h>

// Prints all fibonacci numbers from the first to the 5000th.
int main(void)
  mpz_t n;
  mpz_init( n );

  for ( unsigned int i = 1; i <= 5000; i++ )
    mpz_fib_ui( n, i );
    gmp_printf( "%u: %Zd\n", i, n );

  mpz_clear( n );
The 5000th number is  3878968454388325633701916308325905312082127714646245106160597214895550139044037097010822916462210669479293452858882973813483102008954982940361430156911478938364216563944106910214505634133706558656238254656700712525929903854933813928836378347518908762970712033337052923107693008518093849801803847813996748881765554653788291644268912980384613778969021502293082475666346224923071883324803280375039130352903304505842701147635242270210934637699104006714174883298422891491273104054328753298044273676822977244987749874555691907703880637046832794811358973739993110106219308149018570815397854379195305617510761053075688783766033667355445258844886241619210553457493675897849027988234351023599844663934853256411952221859563060475364645470760330902420806382584929156452876291575759142343809142302917491088984155209854432486594079793571316841692868039545309545388698114665082066862897420639323438488465240988742395873801976993820317174208932265468879364002630797780058759129671389634214252579116872755600360311370547754724604639987588046985178408674382863125.
Example Code / Re: Fibonacci, with bigger numbers
« Last post by fredericopissarra on November 28, 2023, 12:27:51 PM »
fib(5000) is 1060 digits long.

Yep, You can calculate individual fibonacci numbers and count them until you get the nth one, but this is SLOW! The method @debs3759 shown is way faster (but imprecise for big n's using floating point).

Here's what debs shown us using 'bc':
Code: [Select]
/* mylib.bc */

define floor(x) {
  auto savescale;
  savescale = scale;
  scale = 0;
  if (x>0) {
    result = x-(x%1);
  } else result = -1*ceil(-1*x);

  scale = savescale;
  return result;

define ceil(x) {
  auto savescale;
  savescale = scale;
  scale = 0;
  if (x>0) {
    if (x%1>0) {
      result = x+(1-(x%1));
    } else result = x;
  } else result = -1*floor(-1*x);

  scale = savescale;
  return result;

// Assumes n>=1.
define fib(n) {
  auto phi, mphi;


  return ceil((phi^n - mphi^n)/sqrt(5));
Code: [Select]
$ bc -l ~/mylib.bc
'bc' uses multi precision arithmetic.

Anyway... if you'll use add/adc approach, do it with DWORDs or QWORDs instead of bytes...
Example Code / Re: Fibonacci, with bigger numbers
« Last post by andyz74 on November 27, 2023, 08:11:07 PM »
The real problem is that fib(399) results in a 84 algarisms number:


Therefor, I use in my code  this :
Code: [Select]
 f1array  :  resb 80
 f2array  :  resb 80
 f3array  :  resb 80
Each digit is stored in a separate byte inside this array. So I can add for example  f1array[5] + f2array[5] and store it in f3array[5] . If I do this for all array-elements, then I can afterwards calculate with DIV  and MOD that the f3array-elements are not bigger than 9, and in this way I get in f3array the new fibonacci with every digit in one byte stored.  The size of the array by now 80 bytes is hardcoded, but can easily made bigger. I tested numbers with up to 160 digits, and I am sure, there can be calculated even bigger ones.

/edit :
Just for the fun of it, I modified the arrays to 10000 and the fibonaccis to 5000 and found out, that fibonacci around 5000 is :


(I'm not sure if my indexing is totally correct.maybe it is fib(4999) or fib(5001).
Example Code / Re: Fibonacci, with bigger numbers
« Last post by debs3759 on November 27, 2023, 05:47:26 PM »
Just checked again, and it is actually "algorism", searching "algarism" gives results for "algorism", which mostly describe it as a digit. With an "O" it is valid in Scrabble, my reference for valid words :)
Pages: 1 ... 5 6 [7] 8 9 10