NeuroTIC 0.0
Loading...
Searching...
No Matches
ntfile.c File Reference

Implementation of network saving and loading for NeuroTIC. More...

#include "ntfile.h"
#include "ntbuilder.h"
#include "ntmemory.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
Include dependency graph for ntfile.c:

Go to the source code of this file.

Macros

#define NAME_LENGTH   30
 
#define MAGIC   "NeuroTIC"
 
#define VERSION   0x0
 

Functions

Endianness and Floating-Point Handling

The STRICT_LE16 and STRICT_LE32 macros ensure that data is written in little-endian format, regardless of the host system's endianness.
The checkendian function detects the system's endianness, while bswap16 and bswap32 perform byte swapping for 16-bit and 32-bit integers, respectively.
The isieee754 function checks if the system uses IEEE 754 floating-point representation, and the float32 and floatsys functions convert between float and uint32_t representations based on the IEEE 754 standard or a custom format if not supported.

/
#define STRICT_LE16( x ) &( uint16_t ){ little_endian ? ( x ) : bswap16( x )}
#define STRICT_LE32( x ) &( uint32_t ){ little_endian ? ( x ) : bswap32( x )}
uint8_t checkendian( void ){
uint16_t x= 1;
return *(uint8_t *)&x;
}
uint16_t bswap16( uint16_t x ){
return ( x >> 8 ) | ( x << 8 );
}
uint32_t bswap32( uint32_t x ){
return
( ( x >> 24 ) & 0xFF ) |
( ( x >> 8 ) & 0xFF00 ) |
( ( x << 8 ) & 0xFF0000 ) |
( ( x << 24 ) & 0xFF000000 );
}
uint8_t isieee754( void ){
return
( sizeof( float ) == 4) &
( FLT_RADIX == 2 ) &
( FLT_MANT_DIG == 24 ) &
( FLT_MAX_EXP == 128 );
}
typedef struct{
uint8_t sign;
int32_t e;
float m;
} float_parts_s;
uint32_t float32( float x , uint8_t ieee754 ){
uint32_t result;
float_parts_s float_parts= {0};
if( ieee754 ){
memcpy( &result , &x , sizeof( float ) );
return result;
}
float_parts.sign= x < 0.0f;
if( ( x= fabsf( x ) ) == 0.0f ) return 0;
float_parts.m= frexpf( x , &float_parts.e );
if( float_parts.e <= -126 ) return ( float_parts.sign << 31 );
if( float_parts.e >= 129 ) return ( float_parts.sign << 31 ) | ( 0xFF << 23 );
return ( float_parts.sign << 31 ) |
( ( uint32_t )( float_parts.e + 126 ) << 23 ) |
( ( uint32_t )( ( float_parts.m * 2.0f - 1.0f ) * ( 1u << 23 ) + 0.5f ) & 0x7FFFFF );
}
float floatsys( int32_t x , uint8_t ieee754 ){
float result;
if( ieee754 ){
memcpy( &result , &x , sizeof( float ) );
return result;
}
float_parts_s float_parts= { .sign= ( x >> 31 ) & 1 , .e= ( x >> 23 ) & 0xFF , .m= ( float )( x & 0x7FFFFF ) / ( 1u << 23 ) };
if( float_parts.e == 0 && ( x & 0x7FFFFF ) == 0 ) return 0.0f;
if( float_parts.e == 0 ) float_parts.e= -126;
else {
float_parts.m+= 1.0f;
float_parts.e-= 127;
}
result= ldexpf( float_parts.m , float_parts.e );
return float_parts.sign ? -result : result;
}
unsigned char savenet (net_s *net, const char *name)
 Saves a network to a binary file with extension .ntic.
 
struct net_s loadnet (char *name)
 Loads a network from a binary file with extension .ntic.
 

Detailed Description

Implementation of network saving and loading for NeuroTIC.

Provides functions to persist network structures and weights, allowing training to be stored and reloaded.
The file format is binary with a custom structure, including endianness and floating-point representation handling for cross-platform compatibility.
Currently, the implementation focuses on core data serialization and deserialization, with future plans for validation and standardization of loaded data.

Todo:

Implement file validation and data standardization during loading to ensure compatibility and integrity of loaded networks.

Consider adding support for versioning in the file format to allow for future extensions and backward compatibility.

Explore options for compressing the saved network files to reduce disk space usage, especially for larger networks.

Author
Oscar Sotomayor
Date
2026

Definition in file ntfile.c.

Macro Definition Documentation

◆ MAGIC

#define MAGIC   "NeuroTIC"

◆ NAME_LENGTH

#define NAME_LENGTH   30

◆ VERSION

#define VERSION   0x0

Function Documentation

◆ loadnet()

struct net_s loadnet ( char *  name)

Loads a network from a binary file with extension .ntic.

Reconstructs the network structure, weights, biases, and buffer wiring from a binary file.
The function reads the file header to validate the format and version, then proceeds to read the core network data while handling endianness and floating-point representation.

Todo:

Implement file validation and data standardization during loading to ensure compatibility and integrity of loaded networks.

Consider adding support for versioning in the file format to allow for future extensions and backward compatibility.

Explore options for compressing the saved network files to reduce disk space usage, especially for larger networks.

References wiring_s::array_type, wiring_s::arrays, neuron_s::b, neuron_s::bff_idx, buildnet(), neuron_s::fn, neuron_s::inputs, net_s::inputs, net_s::layers, net_s::neurons, newnet(), net_s::nn, wiring_s::size, wiring_s::src_index, wiring_s::src_layer, wiring_s::src_type, neuron_s::w, and net_s::wiring.

◆ savenet()

unsigned char savenet ( net_s net,
const char *  name 
)

Saves a network to a binary file with extension .ntic.

Saves the network's layers, neurons, weights, biases, and buffer wiring to a binary file.
The file is structured with a header containing a magic string and version byte, followed by the network's core data.
The function handles endianness and floating-point representation to ensure cross-platform compatibility.

Todo:

Add validation checks for the input network structure before attempting to save, ensuring that all necessary data is present and correctly formatted.

Implement error handling for file operations, such as checking for write permissions and handling disk space issues.

Consider adding metadata to the file format, such as timestamps or training information, to provide more context when loading networks in the future.

Explore options for compressing the saved network files to reduce disk space usage, especially for larger networks.

Implement file validation and data standardization during loading to ensure compatibility and integrity of loaded networks.

References wiring_s::array_type, wiring_s::arrays, neuron_s::bff_idx, neuron_s::fn, neuron_s::inputs, net_s::layers, net_s::neurons, net_s::nn, wiring_s::size, wiring_s::src_type, and net_s::wiring.