// File: /workspace/github_c_makefile_star2_O0/salextpuru[P]speccy-toochain/src/zmakebas/zmakebas.c
// Function: dbl2spec
int dbl2spec(double num,int *pexp,unsigned long *pman)
{
int exp;
unsigned long man;

/* check for small integers */
if(num==(long)num && num>=-65535.0 && num<=65535.0)
  {
  /* ignores sign - see below, which applies to ints too. */
  long tmp=(long)fabs(num);
  
  exp=0;
  man=((tmp%256)<<16)|((tmp>>8)<<8);
  }
else
  {
  int f;
  
  /* It appears that the sign bit is always left as 0 when floating-point
   * numbers are embedded in programs, and the speccy appears to use the
   * '-' character to detemine negativity - tests confirm this.
   * As such, we *completely ignore* the sign of the number.
   * exp is 0x80+exponent.
   */
  num=fabs(num);
  
  /* binary standard form goes from 0.50000... to 0.9999...(dec), like
   * decimal which goes from        0.10000... to 0.9999....
   */
  
  /* as such, if the number is >=1, it gets divided by 2, and exp++.
   * And if the number is <0.5, it gets multiplied by 2, and exp--.
   */
  exp=0;
  while(num>=1.0)
    {
    num/=2.0; exp++;
    }

  while(num<0.5)
    {
    num*=2.0; exp--;
    }

  /* so now the number is in binary standard form in exp and num.
   * we check the range of exp... -128 <= exp <= 127.
   * (if outside, we return error (i.e. 0))
   */
   
  if(exp<-128 || exp>127) return(0);
    
  exp=128+exp;
  
  /* so now all we need to do is roll the bits off the mantissa in `num'.
   * we start at the 0.5ths bit at bit 0, and shift left 1 each time
   * round the loop.
   */
  
  num*=2.0;  /* make it so that the 0.5ths bit is the integer part, */
	     /* and the rest is the fractional (0.xxx) part. */
  
  man=0;
  for(f=0;f<32;f++)
    {
    man<<=1;
    man|=(int)num;
    num-=(int)num;
    num*=2.0;
    }

  /* Now, if (int)num is non-zero (well, 1) then we should generally
   * round up 1. We don't do this if it would cause an overflow in the
   * mantissa, though.
   */
   
  if((int)num && man!=0xFFFFFFFF) man++;
    
  /* finally, zero out the top bit */
  man&=0x7FFFFFFF;
  }

/* done */
*pexp=exp; *pman=man;
return(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movsd %xmm0, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movsd -0x10(%rbp), %xmm0
cvttsd2si -0x10(%rbp), %rax
cvtsi2sd %rax, %xmm1
ucomisd %xmm1, %xmm0
jne 0x34c8
jp 0x34c8
movsd -0x10(%rbp), %xmm0
movsd 0x2ba2(%rip), %xmm1     # 0x6008
ucomisd %xmm1, %xmm0
jb 0x34c8
movsd 0x2b9c(%rip), %xmm0     # 0x6010
ucomisd -0x10(%rbp), %xmm0
jb 0x34c8
movsd -0x10(%rbp), %xmm0
movaps 0x2ba9(%rip), %xmm1     # 0x6030
pand %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x38(%rbp), %rax
movl $0x100, %ecx            # imm = 0x100
cqto
idivq %rcx
movq %rdx, %rax
shlq $0x10, %rax
movq -0x38(%rbp), %rcx
sarq $0x8, %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0x360d
movsd -0x10(%rbp), %xmm0
movaps 0x2b5c(%rip), %xmm1     # 0x6030
pand %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movl $0x0, -0x24(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x2b27(%rip), %xmm1     # 0x6018
ucomisd %xmm1, %xmm0
jb 0x3518
movsd -0x10(%rbp), %xmm0
movsd 0x2b24(%rip), %xmm1     # 0x6028
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x34e4
jmp 0x351a
movsd 0x2afe(%rip), %xmm0     # 0x6020
ucomisd -0x10(%rbp), %xmm0
jbe 0x3546
movsd 0x2af7(%rip), %xmm0     # 0x6028
mulsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x351a
cmpl $-0x80, -0x24(%rbp)
jl 0x3552
cmpl $0x7f, -0x24(%rbp)
jle 0x355e
movl $0x0, -0x4(%rbp)
jmp 0x3628
movl -0x24(%rbp), %eax
addl $0x80, %eax
movl %eax, -0x24(%rbp)
movsd 0x2ab7(%rip), %xmm0     # 0x6028
mulsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x20, -0x3c(%rbp)
jge 0x35de
movq -0x30(%rbp), %rax
shlq %rax
movq %rax, -0x30(%rbp)
cvttsd2si -0x10(%rbp), %eax
cltq
orq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cvttsd2si -0x10(%rbp), %eax
cvtsi2sd %eax, %xmm1
movsd -0x10(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movsd 0x2a5f(%rip), %xmm0     # 0x6028
mulsd -0x10(%rbp), %xmm0
movsd %xmm0, -0x10(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x358a
cvttsd2si -0x10(%rbp), %eax
cmpl $0x0, %eax
je 0x35ff
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x30(%rbp)
je 0x35ff
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax)