• Welcome to Jose's Read Only Forum 2023.

TCLib update for Patrice

Started by James C. Fuller, March 08, 2017, 12:04:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris

The ISO standard declares a compatible C rand() function's exact range to be entirely compiler implementation dependent but states that it be at least 0 thru 32767 inclusive. As it happens, both VC and GCC define their RAND_MAX value as 0x7FFF, which is exactly 32767 decimal.

That's the number I thought RAND_MAX was equal to for VC and GCC Mike, but I didn't want to state it without checking.  At the time I needed it (rand()), I needed numbers from about a million to 50,000,000, so I had to devise something to produce them.  I'm not really sure what's in the C++ Standard Library (if anything), relating to a C++ version of rand().

Charles Pegge

This is a satisfactory, though home-made 32-bit pseudo-randomizer. It churns integers but scales them to return floats -1.0 to +1.0. To return 32bit integer ranges, omit the float scaler and return the raw integer value in eax instead.

  int seed=0x12345678
  function Rnd() as float
  Static As float f, d=1/0x7fffffff
  mov eax,seed
  inc eax
  rol eax,13
  xor eax,0xdab5ca3a
  mov seed,eax
  push eax
  fild dword [esp]
  pop eax
  fmul dword d
  fstp dword f
  return f
  end function

You are probably familiar with this one from the web, based on mul overflows into edx.
The 2 inputs are the lower and upper range limits:

  function irnd(int z1, z2) as int
  mov    eax,z2
  sub    eax,z1
  inc    eax
  imul   edx,Seed,0x8088405
  inc    edx
  mov    Seed,edx 'store new seed
  mul    edx 'multiply eax by edx
  return edx+z1
  end Function

Patrice Terrier

The TCLib version of the Visual Studio community 2015 has been attached to this post

Thank you all, for having helped me to produce these amazing small 64-bit DLL(s), without using any compressor !
Patrice Terrier
GDImage (advanced graphic addon)

Patrice Terrier

So far i have been able to create my smallest OpenGL 64-bit plugin, only 13 Kb.

And i was able to reduce the size of my PNGanim project from 140 Kb down to 60 Kb, using GetprocAdress to access directly to MSVCRT, the only thing that is causing me havoc, is when there are multiple optional parameters like for swprintf, and i don't know the syntax to use to create the prototype to use the "...".

Patrice Terrier
GDImage (advanced graphic addon)

James C. Fuller

  Take a look at the TCLib\stdio.h file.


Patrice Terrier


I have seen it already, but what i am looking for is a replacement for the PowerBASIC statement "AS ANY"
without using va_list

Currently i am doing this to use swprintf with long, and i must have another function for float, double, etc.

#define long_proc_c typedef long (__cdecl *zProc)

    static HMODULE hModule;
    if (hModule == 0) { hModule = LoadLibrary(L"MSVCRT"); }
    return hModule;

long long_swprintf(OUT WCHAR* buffer, IN WCHAR* format, IN long N) {
    long nRet = -1; // Error
    HMODULE hModule = MSVCRT();
    if (hModule) {
        long_proc_c (WCHAR*, WCHAR*, long);
        zProc hProc = (zProc)GetProcAddress(hModule, "swprintf");
        if (hProc) { nRet = hProc(buffer, format, N); }
    return nRet;
Patrice Terrier
GDImage (advanced graphic addon)

Patrice Terrier

James, Fred,

I have attached the latest Visual Studio 2015/2017 TCLIB version of my 64-bit OpenGL plugins
to this post

The smallest dll is only 13 Kb.
Patrice Terrier
GDImage (advanced graphic addon)

James C. Fuller

Thanks Patrice,
  I realize now why you were having problems with rand and srand as I found issues with my own work.
I assume one of your included files:
  #include <gl/GL.h>
  #include <gl/GLU.h>
is pulling in <stdlib.h> or one of the other msvcrt include files.

I had done a lot of successful testing originally with my port of José's CWindow and Afx framework but when I tried with my latest version of TCLib I had a number of redefinition errors.
I was including:
   #include <psapi.h>
   #include <Shlobj.h>
   #include <KnownFolders.h>
   #include <shlwapi.h>
   #include <commctrl.h>
   #include <uxtheme.h>
and at least one of them is pulling in msvcrt include files.

I write all my bc9Basic code as ansi. I then parse the c++ translated code with the ULEX utility which does all the transformations. I can name the library function tcl_rand and translate the source "rand" call to tcl_rand on the fly.
I am scratching my head though as only a few are a causing problems.